kintone: Delete Record
Delete records in a Kintone App. You can delete multiple records at once. When you delete multiple ones, you should write one record ID on each line.
Configs
  • C1: Authorization Setting in which API Token is set *
  • C2: Domain (such as xxxxx.kintone.com or xxxxx.cybozu.com) *
  • C3: Guest Space ID (required if the App is in a Guest Space)
  • C4: App ID *
  • C5: String type data item with record IDs to delete *
Script (click to open)


main();
function main(){
//// == 工程コンフィグ・ワークフローデータの参照 / Config & Data Retrieving ==
const auth = configs.get("conf_auth");
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, 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} appId アプリ ID
* @param {Array<String>} recordIds レコード ID の配列
*/
function deleteRecords( apiUri, apiToken, 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 );
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}`;
}
}


Download

2020-11-26 (C) Questetra, Inc. (MIT License)
https://support.questetra.com/addons/kintone-record-delete/
The Addon-import feature is available with Professional or Enterprise edition.

Notes

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

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

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

Capture

See also

%d bloggers like this: