何を実現したいのかを書きましょう
ゲストスペースのアプリ使用時にメインのスペースのアプリへ番号(key)(文字列フィールドです)を元にゲストスペースのアプリ内の名前フィールドの内容をメインスペースのアプリ内の名前フィールドに更新したいです
発生した問題やエラーメッセージを具体的に書きましょう
apiトークンやユーザーのそもそもの権限、メインとゲスト側ともにアプリの権限があるのですが何故か権限がないとエラーが出てしまいます。
実行したコードをコピー&ペーストしましょう
アプリidやapiトークンはわざとxx~にしております。
(() => {
'use strict';
// アプリIDとフィールドコードの定義
const TO_APP_ID = xxx; // メインスペースのアプリIDに変更してください
const NUMBER_FIELD_CODE = 'number_field_code'; // 番号フィールドのフィールドコードに変更してください
const FROM_FIELD_CODE = 'from_field'; // ゲストスペースのフィールドコードに変更してください
const TO_FIELD_CODE = 'to_field'; // メインスペースのフィールドコードに変更してください
const API_TOKEN = 'xxxxxxxxxxxxxxxxxxx'; // 実際のAPIトークンに置き換えてください
// レコード更新関数
const updateRecord = (recordId, fieldValue) => {
const body = {
app: TO_APP_ID,
id: recordId,
record: {
[TO_FIELD_CODE]: {
value: fieldValue
}
}
};
const params = {
method: 'PUT',
headers: {'X-Cybozu-API-Token': API_TOKEN},
body: JSON.stringify(body)
};
kintone.api(kintone.api.url('/k/v1/record', true), 'PUT', body, () => {
alert('レコード更新に成功しました');
}, (error) => {
console.error(error);
alert('レコード更新に失敗しました');
});
};
// レコード検索および更新関数
const searchAndUpdateRecord = (numberValue, fieldValue) => {
const query = `${NUMBER_FIELD_CODE} = "${numberValue}" limit 1`;
const body = {
app: TO_APP_ID,
query: query
};
const params = {
method: 'GET',
headers: {'X-Cybozu-API-Token': API_TOKEN}
};
kintone.api(kintone.api.url('/k/v1/records', true), 'GET', body, (resp) => {
if (resp.records.length > 0) {
const recordId = resp.records[0].$id.value;
updateRecord(recordId, fieldValue);
} else {
alert('一致するレコードが見つかりませんでした');
}
}, (error) => {
console.error(error);
alert('レコード検索に失敗しました');
});
};
// ボタン追加イベント
kintone.events.on('app.record.detail.show', (event) => {
if (document.getElementById('update_button') !== null) {
return;
}
const updateButton = document.createElement('button');
updateButton.id = 'update_button';
updateButton.innerText = '更新する';
updateButton.onclick = () => {
const numberValue = event.record[NUMBER_FIELD_CODE].value;
const fieldValue = event.record[FROM_FIELD_CODE].value;
searchAndUpdateRecord(numberValue, fieldValue);
};
kintone.app.record.getHeaderMenuSpaceElement().appendChild(updateButton);
});
})();