Count user names in tables

This sample code works similarly to the Count table rows (conditional) sample, but instead of counting the number of drop downs, this sample counts the number of users listed inside tables.

Sample Image

countusernames.gif

Sample Code

//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;
    });
}());

Notes

Refer to the Tutorial page, on how to add scripts to your app.

Was this article helpful?
0 out of 0 found this helpful
Comments
Please sign in to leave a comment.