Get all records from an app

Introduction

This article shows how to retrieve information of all records from an app using JavaScript.

Limitation of getting record information from apps

When working with kintone customizations, there are many scenarios where you would need to retrieve information of every record in your app.
When doing this, it's important to understand what methods you can take, and what the limitations are.

Using the Record List Event (app.record.index.show)

kintone has a record list event.
This event runs when the record list is displayed, and provides the user with an event object containing information of all of the records in the current record list.

kintone.events.on("app.record.index.show", function(eventobj) {
    //display record list details
    console.log(eventobj);
});

The number of records that are given back here though are limited to the number of records that are displayed on the page. kintone shows from 20 to 100 records in the record list page (this can be tweaked on an option listed on the record list page), meaning that you will only be able to get information of a maximum of 100 records in the event object. This becomes an issue obviously, when you need to deal with more than 100 records.

Using the Get Records API

In your JavaScript code, you can use the kintone REST API request to run kintone's Get Records API.
By default, this API retrieves up to 100 records, but by setting the "limit" option in the query parameter, this limit can be raised to a maximum of 500 records. We still have the same issue here though - what do we do when we have over 500 records in our app?

How to get all records from an app

The answer to this, is to use the Get Records API recursively, using promises.
If you are not familiar with the JavaScript terms of recursive functions and promises, here is a brief explanation.

Recursive functions are functions that make a call to itself - basically meaning that we can call the Get Records API, and once we hit the limit of the records we can retrieve, we can make the function call itself again to retrieve even more records. This will continue until we have retrieved every record from the app.

Promises, which may be a harder concept to understand, allow the functions to be called one after the other. You may think that placing the Get Records API in a standard for loop would do the job, but since the Get Records API is an asynchronous operation, the loop tends to end before we even get our first set of results back. What promises do, is that it waits for the results of the first Get Records API to come back before sending its request for the second Get Records API, and the same for the third, forth etc.

Sample Code

function fetchRecords(appId, opt_offset, opt_limit, opt_records) {
  var offset = opt_offset || 0;
  var limit = opt_limit || 100;
  var allRecords = opt_records || [];
  var params = {app: appId, query: 'limit ' + limit + ' offset ' + offset};
  return kintone.api('/k/v1/records', 'GET', params).then(function(resp) {
    allRecords = allRecords.concat(resp.records);
    if (resp.records.length === limit) {
      return fetchRecords(appId, offset + limit, limit, allRecords);
    }
    return allRecords;
  });
}
 
fetchRecords(kintone.app.getId()).then(function(records) {
  console.log(records);
});

The Result

This code will give back information of all records of an app, and display it all in your developer console of your browser.
You can try out using this code right now by opening the record list of your kintone app, and copy and pasting the code above.

To finish

This code will come in handy for many types of kintone customizations - for example, you can use it in conjunction with kintone's "Customized View" (a feature that displays custom HTML on the record list view) to display more than 100 records.

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