Count the Number of Rows in Tables

Contents

Overview

This article introduces how to count the number of rows in a table of a Kintone App, and insert the result into a Number field.

Sample Image

An event is triggered when the Save button is clicked. The number of rows are counted, and are inserted into a Number field.

Prepare the App

Create an App (External link) with the following fields and settings.

Field Type Field Name Field Code Notes
Text Test Name testname
Table Test Results mytable See the table below for the fields to place inside.
Number Number of rows in table number_of_rows
Calculated Average Result avr_result Set the following formula
  • SUM(testresult)/number_of_rows

Set the following fields and settings for the Test Results table.

Field Type Field Name Field Code Notes
Text Name name
Number Test Result testresult

Sample Code

Prepare the following JavaScript code in a text editor and navigate to the Kintone App's settings. Upload the file into the Upload JavaScript for PC option of the JavaScript and CSS Customization settings (External link) .

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
(function() {
  'use strict';

  var TABLE = 'mytable'; // Field code of the Table field
  var ROWCOUNT = 'number_of_rows'; // Field code of the Number field

  kintone.events.on(['app.record.create.submit', 'app.record.edit.submit'], function(eventobj) {
    var num_of_rows = eventobj.record[TABLE].value.length;
    eventobj.record[ROWCOUNT].value = num_of_rows;
    return eventobj;
  });
})();

After saving the settings and clicking on Update App, create a new record. Add several rows in the table and fill them with data. After saving the record, the number of rows should be inserted into the Number field.