Log users who have viewed a record

This sample code logs users who have viewed the record. The name of the user who viewed the record is automatically placed in the User selection field.

Sample Image

viewlog.gif

Sample Code

// When a user views a record, they are added to the user selection list

(function() {
    'use strict';

    var VIEWEDUSERS = 'user_log'; //field code of user selection field
    var events = ['app.record.detail.show'];

    kintone.events.on(events, function(event) {
        var record = event.record;
        var isRead = false;
        var users = record[VIEWEDUSERS].value;
        var myUserCode = kintone.getLoginUser().code;

        for (var i in users) {
            if (!users.hasOwnProperty(i)) { continue; }
            if (users[i].code === myUserCode) {
                isRead = true;
            }
        }

        if (isRead) { return event; }

        var param = {
            "app": kintone.app.getId(),
            "id": kintone.app.record.getId(),
            "record": {
                [VIEWEDUSERS]: {
                    "value": users.concat({
                        "code": myUserCode
                    })
                }
            }
        };
        kintone.api(kintone.api.url('/k/v1/record', true), 'PUT', param).then(function(resp) {
            location.reload();
        }).catch(function(error) {
            alert(error.message);
        });
    });
}());

Notes

Make sure to match the Field code of your fields, with the Field code written in the code.

 

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.