kintone: Delete Record
This item deletes records in a Kintone App. It can delete multiple ones at once. It must contain one record ID per line.
Configs: Common
  • Step Name
  • Note
Configs
  • C1: Authorization Setting in which API Token is set *
  • C2: Basic Auth Setting (required if enabled on Kintone)
  • C3: Domain (such as xxxxx.kintone.com or xxxxx.cybozu.com) *
  • C4: Guest Space ID (required if the App is in a Guest Space)
  • C5: App ID *
  • C6: Record IDs to delete *

Notes

  • To get an API Token, open the Settings of the App and click “API Token” in the App Settings tab on Kintone.

    Click “Generate”, select Permissions (“Delete records” permission is required), and click “Save”.

    Do not forget to click “Update App” to apply the update.
  • Guest Space ID (only when the Kintone App is in a guest space) and App ID can be confirmed in the API Token settings on Kintone.
  • Up to 100 records can be deleted at once under the restrictions of Kintone’s REST API.

Capture

See also

Script (click to open)
  • An XML file that contains the code below is available to download
    • kintone-record-delete.xml (C) Questetra, Inc. (MIT License)
    • If you are using Professional, you can modify the contents of this file and use it as your own add-on


main();
function main(){
  //// == 工程コンフィグ・ワークフローデータの参照 / Config & Data Retrieving ==
  const auth = configs.get("conf_auth");
  const basic = configs.get("conf_basic");
  const domain = configs.get("conf_domain");
  const guestSpaceId = configs.get("conf_guestSpaceId");
  const appId = configs.get("conf_appId");
  const recordIds = retrieveRecordIds();

  const apiToken = httpClient.getOAuth2Token( auth );

  //// == 演算 / Calculating ==
  const apiUri = determineApiUri( domain, guestSpaceId );
  deleteRecords( apiUri, apiToken, basic, appId, recordIds );
}

/**
  * config からレコード ID の配列を読み出す
  * レコード ID が空または不正な文字列を含む場合はエラーとする
  * @return {Array<String>} idStrArray  レコード ID の配列
  */
function retrieveRecordIds() {
  const recordIdDef = configs.getObject( "conf_recordId" );
  let recordIds = configs.get( "conf_recordId" );
  if ( recordIdDef !== null ) {
    recordIds = engine.findData( recordIdDef );
  }
  if ( recordIds === "" || recordIds === null ) {
    throw "Record IDs aren't set.";
  }
  const idStrArray = recordIds.split("\n").filter( id => id !== "" ); // 空文字列を削除
  if ( idStrArray.length === 0 ) {
    throw "Record IDs aren't set.";
  }
  const isValid = idStrArray.every( id => isValidId(id) ); // すべての ID をチェック
  if ( !isValid ) {
    throw "Includes invalid Record ID.";
  }
  return idStrArray;
}

/**
  * ID が有効か(自然数か)を判定する
  * @param {String} idString  ID の文字列
  * @return {Boolean}  有効な ID かどうか
  */
function isValidId( idString ) {
  const idReg = new RegExp( '^[1-9][0-9]*$' );
  return idReg.test( idString );
}

/**
  * kintone REST API のレコード削除の URI を決定する
  * ドメインが空、または kintone のドメインとして不正な文字列であればエラーとする
  * ゲストスペース ID が不正な文字列であればエラーとする
  * @param {String} domain  ドメイン
  * @param {String} guestSpaceId  ゲストスペース ID
  * @return {String} apiUri  API の URI
  */
function determineApiUri( domain, guestSpaceId ) {
  if ( domain === "" || domain === null ) {
    throw "Domain is empty.";
  }
  const domainReg = new RegExp( '^[0-9a-zA-Z-]{3,32}.(?:kintone.com|cybozu.com)$' );
  if ( !domainReg.test(domain) ) {
    throw "Invalid Kintone domain.";
  }
  let apiUri = "";
  if ( guestSpaceId === "" || guestSpaceId === null ) {
    apiUri = `https://${domain}/k/v1/records.json`;
  } else {
    if ( !isValidId(guestSpaceId) ) {
      throw "Invalid Guest Space ID.";
    }
    apiUri = `https://${domain}/k/guest/${guestSpaceId}/v1/records.json`;
  }
  return apiUri;
}

/**
  * kintone REST API にレコード削除の DELETE リクエストを送信する
  * アプリ ID が空または不正な文字列であればエラーとする
  * @param {String} apiUri  API の URI
  * @param {String} apiToken  API トークン
  * @param {String} basic  Basic 認証設定
  * @param {String} appId  アプリ ID
  * @param {Array<String>} recordIds  レコード ID の配列
  */
function deleteRecords( apiUri, apiToken, basic, appId, recordIds ) {
  if ( appId === "" || appId === null ) {
    throw "App ID is empty.";
  }
  if ( !isValidId(appId) ) {
    throw "Invalid App ID.";
  }
  let request = httpClient.begin()
    .header( "X-Cybozu-API-Token", apiToken )
    .queryParam( "app", appId );
  if ( basic !== "" && basic !== null ) {
    request = request.authSetting(basic);
  }
  recordIds.forEach( (id, index) => request = request.queryParam( `ids[${index}]`, id ) );
  const response = request.delete( apiUri );
  // when error thrown
  const status = response.getStatusCode();
  if ( status !== 200 ) {
    engine.log(`API URI: ${apiUri}`);
    engine.log(`Record IDs: ${recordIds}`);
    const responseStr = response.getResponseAsString();
    const accessLog = `---DELETE request--- ${status}\n${responseStr}\n`;
    engine.log(accessLog);
    throw `Failed to delete records. status: ${status}`;
  }
}

  

1 thought on “kintone: Delete Record”

  1. Pingback: Using kintone from Workflow – Questetra Support

Comments are closed.

%d bloggers like this: