- An XML file that contains the code below is available to download
- kintone-record-add.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
const FIELD_NUM = 7; // 扱えるフィールドの数
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 recordIdDef = configs.getObject("conf_recordId");
const apiToken = httpClient.getOAuth2Token( auth );
const recordObj = retrieveRecordContent();
//// == 演算 / Calculating ==
const apiUri = determineApiUri( domain, guestSpaceId );
const recordId = addRecord( apiUri, apiToken, basic, appId, recordObj );
//// == ワークフローデータへの代入 / Data Updating ==
setData( recordIdDef, recordId );
}
/**
* configからレコード情報(フィールドコードとフィールドの値)を読み出し、JSON オブジェクトを返す
* @return {Object} recordObj レコード情報の JSON オブジェクト
*/
function retrieveRecordContent() {
const recordObj = {};
for (let i = 0; i < FIELD_NUM; i++) {
const fieldCodeConfigName = `conf_fieldCode${i+1}`;
const fieldValueConfigName = `conf_fieldValue${i+1}`;
const fieldCode = configs.get( fieldCodeConfigName );
if ( fieldCode === "" || fieldCode === null ) { // フィールドコードが空
continue;
}
if ( recordObj[fieldCode] !== undefined ) { // フィールドコードの指定が重複
throw "The same Field Code is set multiple times.";
}
let fieldValue = configs.get( fieldValueConfigName );
if ( fieldValue === "" || fieldValue === null ) { // 値が空
fieldValue = null;
}
recordObj[fieldCode] = {
"value": fieldValue
};
}
return recordObj;
}
/**
* 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/record.json`;
} else {
if ( !isValidId(guestSpaceId) ) {
throw "Invalid Guest Space ID.";
}
apiUri = `https://${domain}/k/guest/${guestSpaceId}/v1/record.json`;
}
return apiUri;
}
/**
* kintone REST API にレコード追加の POST リクエストを送信する
* アプリ ID が空、または不正な文字列であればエラーとする
* @param {String} apiUri API の URI
* @param {String} apiToken API トークン
* @param {String} basic Basic 認証設定
* @param {String} appId アプリ ID
* @param {Object} recordObj レコード情報の JSON オブジェクト
* @return {String} recordId レコード ID
*/
function addRecord( apiUri, apiToken, basic, appId, recordObj ) {
if ( appId === "" || appId === null ) {
throw "App ID is empty.";
}
if ( !isValidId(appId) ) {
throw "Invalid App ID.";
}
const body = {
"app": appId,
"record": recordObj
};
engine.log(`API URI: ${apiUri}`);
engine.log(`Request Body: ${JSON.stringify(body)}`);
let request = httpClient.begin()
.header( "X-Cybozu-API-Token", apiToken )
.body(JSON.stringify(body), "application/json; charset=UTF-8");
if ( basic !== "" && basic !== null ) {
request = request.authSetting(basic);
}
const response = request.post( apiUri );
const responseStr = response.getResponseAsString();
const status = response.getStatusCode();
if (status >= 300) { // when error thrown
engine.log(`---POST request--- ${status}\n${responseStr}`);
throw `Failed to add record. status: ${status}`;
}
const json = JSON.parse(responseStr);
return json.id;
}
/**
* ID が有効か(自然数か)を判定する
* @param {String} idString ID の文字列
* @return {Boolean} 有効な ID かどうか
*/
function isValidId( idString ) {
const idReg = new RegExp( '^[1-9][0-9]*$' );
return idReg.test( idString );
}
/**
* データ項目に出力する
* @param {ProcessDataDefinitionView} dataDef データ項目の ProcessDataDefinitionView
* @param {String} dataString 出力する文字列
*/
function setData( dataDef, dataString ){
if(dataDef !== null){
engine.setData( dataDef, dataString );
}
}
Pingback: kintone: Update Record – Questetra Support
Pingback: Using kintone from Workflow – Questetra Support
Pingback: Seminar Registration 4 – Questetra Support
Pingback: Starting Processes According to Records Shown in kintone Calendar – Questetra Support