Use Kintone REST APIs to create Access Logs

Introduction

This article introduces how to add records into an App using the Kintone REST API. As a prerequisite, an Access Log App will be created that will record the following:

  • The App ID of the App that a user accessed
  • The Record ID of the record that a user accessed
  • The user that accessed the record
  • The date and time the user accessed the record

A JavaScript customization will also be created, which will be uploaded to a different App. When users access the App with the uploaded JavaScript file, their access data will be logged into the Access Log App.

Preparing the App

Prepare an App to store access logs with the below fields:

Image_01.png

Field type

Field name

Field code

Number

App ID

App_ID

Number

Record ID

Record_ID

Created by

User

User

Created datetime

Datetime

Datetime

 

The "Created by" field is used for the User field, that will record which user accessed the record. When a user accesses an App with the uploaded JavaScript file, the user is treated as the initiator of the API, meaning that the User field will be automatically filled in with that user's details. The "Datetime" field is similar in setup.
After creating the App, make note of the App ID for later usage. See the help document "How to find an App ID" for more details. 

Retrieving Record Details

The following values will be inserted automatically by Kintone when a new record is created:

  • User (Created by)
  • Datetime (Created datetime)

The remaining "App ID" and "Record ID" can be retrieved from the property of the Record Details Event.

Image_02.png

Refer to Retrieve Record Data (Record Details) for a refresher on the properties of the Event Object.

Upload the following code into the JavaScript settings of the App or Apps you'd like to record access information of (do not upload it into the App that was created in the Preparing the App section), and then navigate the browser to the record details page of any record.

Image_03.png

An alert dialog will appear, showing the values of the App ID and Record ID.

Using the REST API

Next, the scripts needs to be able to add the retrieved data into the Access Log App that was created in the Preparing the App section. To do this, the Add Record (POST) REST API will be used, initiated by the Kintone REST API Request wrapper. The details of the Kintone REST API Request wrapper are shown below:

Image_04.png

 

Parameter: pathOrUrl

As suggested in the description of this parameter, set this parameter as 

kintone.api.url('/k/v1/record', true) 

which is the best practice for using the Kintone REST API Request so that it can be used in both guest and non-guest spaces (although, note that if this customization is to be used inside guest spaces, the Access Log App must also be inside the same guest space, due to permission issues of the users initiating the API).

Parameter: method

Depending on the HTTP method, the URL of the API used in this article (/k/v1/record.json) has different behavior:

Since data will be inserted into the access log, 'POST' is specified.

Parameter: params

Next, pass in parameters in JSON format based on the request parameters of the Add Record API.

Image_05.png

The completed script

The completed script for this customization is shown below.
Replace the App ID of the App that will store the access logs (in the below example, it is '108'), with the App that was created in the Preparing the App section. Upload this script to the JavaScript customization settings of each App you would like to get access logs of.

After navigating to those Apps on the browser, the Access Log data will fill up inside the Access Log App like below:

Image_06.png

Finally

This article introduced how to use the Kintone REST API Request wrapper to run the Add Record REST API. This wrapper makes it easier to run REST APIs via JavaScript, as well being able to make use of promises to run requests after retrieving the REST API's response.

 

Was this article helpful?
0 out of 0 found this helpful
Do you have any questions or issues related to this article?
Please share your views with us in the Community forums!