Update Records (Multiple)

Maintained on

kintone offers a rich set of APIs that allow you to directly manipulate application data programmatically.

In particular, the REST API is a powerful tool for manipulating kintone data through HTTP requests.

This page provides a detailed explanation of how to update a specific record using the kintone REST API.

When updating a single record, the endpoint differs. Please refer to the following page for details.

When Using from kintone

When executing the REST API as a JavaScript customization in kintone, you can easily execute the REST API using the provided JavaScript API.

By using the kintone.api.url method of the JavaScript API, you can obtain the REST API endpoint without worrying about the execution space.

Generic
/**
 * Update multiple records in an app using the kintone REST API
 *
 * @param { { app: string | number; records: ({ revision?: string | number; record?: Record<string, any> } & ({ id: string | number } | { updateKey: { field: string; value: string } }))[] } } params
 * @return { Promise<{ records: { id: string; revision: string } }> } - IDs and revision numbers of the updated records
 */
const updateRecords = (params) => {
  return kintone.api(kintone.api.url('/k/v1/records.json', true), 'PUT', params);
};

Note that when updating a single record, specify record.json, but when updating multiple records, specify records.json.

For the arguments, specify the app ID to which the records will be added, the information to specify the target for the update, and the information of the records to be updated.

You can specify the target by specifying the id property to specify the record ID.

Alternatively, you can specify the target by specifying the field code set to unique and its value in the updateKey property.

If the update is successful, an object with the revision property will be returned.

Bulk
(() => {
  const updateRecords = (params) => {
    /** Omitted */
  };

  kintone.events.on(['app.record.detail.show'], async (event) => {
    const appId = kintone.app.getId();
    const { record: currentRecord } = kintone.app.record.get();
    const recordIds = currentRecord['Record ID List'].value;

    const records = recordIds.map((id) => ({
      id,
      record: {
        FieldCode: { value: 'Updated Value' },
      },
    }));

    await updateRecords({ app: appId, records });

    return event;
  });
})();
Bulk
(() => {
  const updateRecords = (params) => {
    /** Omitted */
  };

  kintone.events.on(['app.record.index.show'], async (event) => {
    const recordId = prompt('Enter the IDs of the records to be updated, separated by commas');
    const appId = kintone.app.getId();

    const records = recordId.split(',').map((id) => ({
      id,
      record: {
        FieldCode: { value: 'Updated Value' },
      },
    }));

    await updateRecords({ app: appId, records });

    return event;
  });
})();

When Using from Outside kintone

The kintone REST API can be used from various environments such as Excel, GAS, and Node.js.

By using the REST API, you can operate applications using kintone data from environments other than kintone.

Use API Tokens

When obtaining kintone data from environments other than kintone, you need to include authentication information in the request.

There are several types of authentication information, but by choosing an API token, you can easily obtain authentication information while ensuring security.

The simplest method is to use an ID and password, but due to security risks, it is recommended to use an API token with the minimum necessary permissions.

When Using Node.js

When using the kintone REST API from Node.js, you can send HTTP requests with almost the same code as executing JavaScript in the browser.

/**
 * Update multiple records in an app using the kintone REST API
 *
 * @param { { app: string | number; records: ({ revision?: string | number; record?: Record<string, any> } & ({ id: string | number } | { updateKey: { field: string; value: string } }))[] } } params
 * @return { Promise<{ records: { id: string; revision: string } }> } - IDs and revision numbers of the updated records
 */
const updateRecords = (params) => {
  const url = `https://__your_subdomain__.cybozu.com/k/v1/records.json`;
  const options = {
    method: 'PUT',
    headers: {
      'Content-Type': 'application/json',
      'X-Cybozu-API-Token': 'YOUR_API_TOKEN',
    },
    body: JSON.stringify(params),
  };
  return fetch(url, options).then((response) => response.json());
};

Replace __your_subdomain__ and YOUR_API_TOKEN with your own environment.

You can set these as arguments, but it is also fine to set them directly within the function.

Unlike the code used from kintone mentioned earlier, if the referenced app is in a guest space, you need to change the URL.

When Using GAS

When using the kintone REST API from Google Apps Script (GAS), you can send HTTP requests using the UrlFetchApp class.

/**
 * Update multiple records in an app using the kintone REST API
 *
 * @param { { app: string | number; records: ({ revision?: string | number; record?: Record<string, any> } & ({ id: string | number } | { updateKey: { field: string; value: string } }))[] } } params
 * @return { Promise<{ records: { id: string; revision: string } }> } - IDs and revision numbers of the updated records
 */
const updateRecords = (params) => {
  const url = `https://__your_subdomain__.cybozu.com/k/v1/records.json`;
  const options = {
    method: 'PUT',
    headers: {
      'Content-Type': 'application/json',
      'X-Cybozu-API-Token': 'YOUR_API_TOKEN',
    },
    payload: JSON.stringify(params),
  };
  const response = UrlFetchApp.fetch(url, options);
  return JSON.parse(response.getContentText());
};

Limitations

When updating multiple records in kintone, the following limitations apply:

  • The maximum number of records that can be updated in a single request is 100.

For methods to add without being aware of this limitation, please refer to the following page.

#kintone #JavaScript #TypeScript