AWS Lambda(node.js)からkintone REST API(getRecords)を実行する方法

AWS Lambda(node.js)からkintone REST API(getRecords)を実行する方法

getRecordsメソッドでkintoneアプリに登録されている全レコードを取得します。

@kintone/rest-api-clientモジュールを使用して実現します。

npm init -y
npm i @kintone/rest-api-client
touch index.js

index.js

LambdaはESMで記述しています。必須パラメータはappのみです。

import { KintoneRestAPIClient } from '@kintone/rest-api-client'
export async function handler(event, context) {
  const client = new KintoneRestAPIClient({
    baseUrl: 'https://xxxxxxxxxx.cybozu.com',
    auth: {
      username: 'ログインID',
      password: 'パスワード'
    }
  })
  const results = await client.record.getRecords(
    {
      app: '4', // required
    }
  )
  return {
    statusCode: 200,
    body: `Hello`
  }
}

zip圧縮

簡単に試すために7zでzip圧縮し、Lambdaデプロイします。

AWS Lambda(node.js)からkintone REST API(getRecords)を実行する方法

テスト

顧客アプリです。

AWS Lambda(node.js)からkintone REST API(getRecords)を実行する方法

「Test」を押します。

AWS Lambda(node.js)からkintone REST API(getRecords)を実行する方法

顧客アプリのレコードが全件出力されていることが確認できます。

query(like)

queryプロパティを使えば条件を絞ることが可能です。

会社名でlike検索する例です。

import { KintoneRestAPIClient } from '@kintone/rest-api-client'
export async function handler(event, context) {
  const client = new KintoneRestAPIClient({
    baseUrl: 'https://xxxxxxxxxx.cybozu.com',
    auth: {
      username: 'ログインID',
      password: 'パスワード'
    }
  })
  const results = await client.record.getRecords(
    {
      app: '4',
      query: 'company like "大阪"'
    }
  )
  console.log(results.records)
  return {
    statusCode: 200,
    body: JSON.stringify()
  }
}

結果です。

AWS Lambda(node.js)からkintone REST API(getRecords)を実行する方法

query(=)

会社名で=検索する例です。

import { KintoneRestAPIClient } from '@kintone/rest-api-client'
export async function handler(event, context) {
  const client = new KintoneRestAPIClient({
    baseUrl: 'https://xxxxxxxxxx.cybozu.com',
    auth: {
      username: 'ログインID',
      password: 'パスワード'
    }
  })
  const results = await client.record.getRecords(
    {
      app: '4',
      query: 'company = "大阪"'
    }
  )
  console.log(results.records)
  return {
    statusCode: 200,
    body: JSON.stringify()
  }
}

0件の場合は、[]が返ります。

最大件数

一度に取得できる件数は500件です。

Retrieves details of multiple records from an app by specifying the app ID and a query string. The number of records that can be retrieved at once is 500.

500件以上取得したい場合はgetAllRecordsを使用します。

参考サイト

https://github.com/kintone/js-sdk/blob/master/packages/rest-api-client/docs/record.md#getRecords
Get Records
Retrieves details of multiple records from an App by specifying the App ID and a query string.
Query string
Filters multi records from an App by query string.

コメント

タイトルとURLをコピーしました