何を実現したいのかを書きましょう
ゲストユーザーがゲストスペース側のアプリにてレコードを編集し更新ボタンを詳細画面でおしたら、メイン側のスペースのアプリにて番号をkeyにしてレコードを更新したいです。
発生した問題やエラーメッセージを具体的に書きましょう
現在メイン側のユーザーであればゲストアプリでボタンを押したときに更新できたのですがゲストユーザーは更新に失敗します。
apiトークンの設定も編集や追加等すべて可能にしておりますができません。
実行したコードをコピー&ペーストしましょう
アプリidやapiトークンは「xxx」とあえて表記しております。
そもそものkintoneの仕様で今回のやりたいことが無理な場合はできないと教えてください。
宜しくお願い致します。
(() => {
'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 = 'xxxxxx'; // 実際の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', false), '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', false), '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);
});
})();