Count the Number of Users in Tables

Contents

Overview

This article introduces how to count the number of specific users in a table.

Sample Image

An event is triggered when the Save button is clicked. The number of each user listed in the User selection field of the table is aggregated. The results are then inserted into Number fields. The sample in this article counts two types of users, and inserts the aggregated result into two different Number fields.

Animated GIF: A record with a table being saved and the user name count being automatically input into Number fields.

Prepare the App

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

Field Type Field Name Field Code Notes
Table User Table TableField See the table below for the fields to place inside.
Number User 1 Count UserCountField1
Number User 2 Count UserCountField2

Set the following fields and settings for the User Table table.

Field Type Field Name Field Code Notes
Date Date date
User selection Staff user_selection

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
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Count individual users in a table, and set those counts into other fields

(function() {
  'use strict';

  var TABLEDATA = 'TableField'; // field code of the table
  var USERFIELD = 'user_selection'; // field code of user field in table
  var USERCOUNT1 = 'UserCountField1'; // field code of number field to count User 1
  var USERCOUNT2 = 'UserCountField2'; // field code of number field to count User 2
  var USER1 = 'Administrator'; // the log in name of User 1
  var USER2 = 'Krispy'; // the log in name of User 2

  // Set events to run when the save button is clicked on the record create or edit page
  kintone.events.on(['app.record.create.submit', 'app.record.edit.submit'], function(eventobj) {
    // Count the number of rows in the table
    var num_of_rows = eventobj.record[TABLEDATA].value;
    var user1_count = 0;
    var user2_count = 0;

    for (var i = 0; num_of_rows.length > i; i++) {
      var num_of_users = num_of_rows[i].value[USERFIELD].value;
      for (var cnt = 0; num_of_users.length > cnt; cnt++) {
        var user = num_of_users[cnt].code;
        if (user === USER1) {
          user1_count++;
        } else if (user === USER2) {
          user2_count++;
        }
      }
    }

    // Set a new value in a field, listed in the event object
    eventobj.record[USERCOUNT1].value = user1_count;
    eventobj.record[USERCOUNT2].value = user2_count;

    // Return the event object, so that kintone will use this new data
    return eventobj;
  });
}());

After saving the settings and clicking on Update App, create a new record. Add several rows in the table, filling in the User selection fields with users defined in the code. After saving the record, the number of specific users listed in the table should be inserted into the designated Number fields.