お世話になります。
REST API で DELETEした後の画面遷移について困っていることがあります。
解決方法ご存じの方がいらっしゃいましたらご教授お願いします。
■背景・実現したいこと
- レコード保存成功後にそのレコード自体を削除。(ここまでは出来ています)
- レコード保存成功後の画面遷移時にすでにそのレコードが存在しないのでエラーメッセージ(GAIA_RE01 MYWMfRIdXt9fhzLVaglM)が出現するのでこれを回避したい。
■試したこと
- DELETE後にeventにurlプロパティを指定してreturn
参照:https://developer.cybozu.io/hc/ja/articles/201941984#success
なんでこんなことをするのかは割愛しますが、一応コード全文貼っておきます。
未完成のコードですが、319行目以降が問題の箇所です。
宜しくお願い致します。
(function() {
'use strict';
kintone.events.on(['app.record.index.edit.submit.success','app.record.create.submit.success','mobile.app.record.create.submit.success','app.record.edit.submit.success','mobile.app.record.edit.submit.success'], function(event) {
const getUniqueValues = ([...array]) => {
return array.filter((value, index, self) => self.indexOf(value) === self.lastIndexOf(value));
}//配列の重複しない値を取得 参照:https://www.nxworld.net/js-array-filter-snippets.html
let existingTableId = [];//既存テーブルID
let projectTableId = event.record.案件管理テーブルID.value;
let ID = event.record.ID.value;
let table = event.record.工事日程テーブル.value;
let originalCarendarId = event.record.オリジナルカレンダーID.value;
let thisId = event.record.カレンダーID.value;
let start = event.record.開始.value;
let end = event.record.終了.value;
let outsourcing = event.record.外注.value;
let manager = event.record.当日担当者.value;
let project = event.record.案件名.value;
let shortProject = event.record.短縮案件名.value;
let type = event.record.種類.value;
let clerk = event.record.事務.value;
let managerJson = '[';
let managerList = [];
if(!(projectTableId)){
if(table.length){
if(ID){
if(!(originalCarendarId)){
table.forEach(row => {
let start = moment(row.value.開始複数日程.value);
let end = moment(row.value.終了複数日程.value);
// let endTime = moment(row.value.工事終了日時.value).hour();
let diff = end.diff(start, 'hours');
let startDate = moment(row.value.工事開始日時.value).date();
end = end.date(startDate);
if(diff >= 24){
let loopTimes = Math.floor(diff / 24);
for(let i = 0; i <= loopTimes; i++){
managerJson = '[';
let clerkJson = '[';
let startClone = start.clone();
let endClone = end.clone();
startClone = startClone.add(i, 'd');
endClone = endClone.add(i, 'd');
let recordJson = '{"開始複数日程":{"value":"'+startClone+'"},"終了複数日程":{"value":"'+endClone+'"},"外注":{"value":"'+outsourcing+'"},"オリジナルカレンダーID":{"value":"'+originalCarendarId+'"},"短縮案件名":{"value":"'+shortProject+'"},"案件名":{"value":"'+project+'"},"種類":{"value":"'+type+'"},';
if(manager.length){
manager.forEach(val => {
managerJson += '{"code":"'+val.code+'"},';
});
managerJson = managerJson.slice(0, -1);
managerJson += ']';
recordJson += '"当日担当者":{"value":'+managerJson+'},';
}
if(clerk.length){
clerk.forEach(val => {
clerkJson += '{"code":"'+val.code+'"},';
clerkJson = clerkJson.slice(0, -1);
clerkJson += ']';
recordJson += '"事務":{"value":'+clerkJson+'},';
});
}
recordJson = recordJson.slice(0, -1);
recordJson += '}';
let body0 = {
'app':76,
'record':JSON.parse(recordJson)
}
return kintone.api(kintone.api.url('/k/v1/record.json', true), 'POST', body0)
.then(function(resp) {
let body = {
'app':75,
'id':ID
}
return kintone.api(kintone.api.url('/k/v1/record.json', true), 'GET', body)
.then(function(resp) {
existingTableId = [];//既存テーブルID
managerJson = '[';
let managerList = [];
let projectTable = resp.record.工事日程テーブル.value;
let recordJson = '{"工事日程テーブル":{"value":[';
projectTable.forEach(row => {
recordJson += '{"id":"'+row.id+'"},';
existingTableId.push(row.id);
if(row.value.当日担当者.value.length){
row.value.当日担当者.value.forEach(val =>{
managerList.push(val.code);
});
}
});
recordJson += '{"value":{"工事開始日時":{"value":"'+startClone+'"},"工事終了日時":{"value":"'+endClone+'"},"外注":{"value":"'+outsourcing+'"},"カレンダーID":{"value":"'+thisId+'"},';
if(manager.length){
manager.forEach(val => {
managerJson += '{"code":"'+val+'"},';
managerList.push(val.code);
});
managerJson = managerJson.slice(0, -1);
managerJson += ']';
recordJson += '"当日担当者":{"value":'+managerJson+'},';
}
recordJson = recordJson.slice(0, -1);
recordJson += '}}]}';
let newManagerList = Array.from(new Set(managerList));
if(newManagerList){
recordJson += ',"当日担当者一覧":{"value":[';
newManagerList.forEach(val => {
recordJson += '{"code":"'+val+'"},';
});
recordJson = recordJson.slice(0, -1);
recordJson += ']}';
}
recordJson += '}';
let body2 = {
'app':75,
'id':ID,
'record':JSON.parse(recordJson)
}
return kintone.api(kintone.api.url('/k/v1/record.json', true), 'PUT', body2)
.then(function(resp) {
let body3 = {
'app':75,
'id':ID
}
return kintone.api(kintone.api.url('/k/v1/record.json', true), 'GET', body3)
.then(function(resp) {
let newProjectTable = resp.record.工事日程テーブル.value;
newProjectTable.forEach(row => {
existingTableId.push(row.id);
});
let newTableId = getUniqueValues(existingTableId);
let body4 = {
'app':76,
'id':thisId,
'record':{
'案件管理テーブルID':{
"value":newTableId[0]
}
}
}
return kintone.api(kintone.api.url('/k/v1/record.json', true), 'PUT', body4)
.then(function(resp) {
}).catch(function() {
event.error = '予期せぬエラーが発生しました!';
return event;
});
}).catch(function() {
event.error = '予期せぬエラーが発生しました!';
return event;
});
}).catch(function() {
event.error = '予期せぬエラーが発生しました!';
return event;
});
}).catch(function() {
event.error = '予期せぬエラーが発生しました!';
return event;
});
}).catch(function() {
event.error = '予期せぬエラーが発生しました!';
return event;
});
}
}else{
managerJson = '[';
let clerkJson = '[';
let recordJson = '{"開始複数日程":{"value":"'+start+'"},"終了複数日程":{"value":"'+end+'"},"外注":{"value":"'+outsourcing+'"},"オリジナルカレンダーID":{"value":"'+originalCarendarId+'"},"短縮案件名":{"value":"'+shortProject+'"},"案件名":{"value":"'+project+'"},"種類":{"value":"'+type+'"},';
if(manager){
manager.forEach(val => {
managerJson += '{"code":"'+val.code+'"},';
});
managerJson = managerJson.slice(0, -1);
managerJson += ']';
recordJson += '"当日担当者":{"value":'+managerJson+'},';
}
if(clerk){
clerk.forEach(val => {
clerkJson += '{"code":"'+val.code+'"},';
clerkJson = clerkJson.slice(0, -1);
clerkJson += ']';
recordJson += '"事務":{"value":'+clerkJson+'},';
});
}
recordJson = recordJson.slice(0, -1);
recordJson += '}';
let body = {
'app':76,
'record':JSON.parse(recordJson)
}
return kintone.api(kintone.api.url('/k/v1/record.json', true), 'POST', body)
.then(function(resp) {
}).catch(function() {
event.error = '予期せぬエラーが発生しました!';
return event;
});
}
});
let deldeteBody = {
'app':76,
'ids':[thisId]
}
return kintone.api(kintone.api.url('/k/v1/records.json', true), 'DELETE', deldeteBody)
.then(function(resp) {
}).catch(function() {
event.error = '予期せぬエラーが発生しました!';
return event;
});
}
}else{
if(!(originalCarendarId)){
originalCarendarId = thisId;
}
let recordJsonList = [];
table.forEach(row => {
let start = moment(row.value.開始複数日程.value);
let end = moment(row.value.終了複数日程.value);
// let endTime = moment(row.value.工事終了日時.value).hour();
let diff = end.diff(start, 'hours');
let startDate = moment(row.value.開始複数日程.value).date();
end = end.date(startDate);
if(diff >= 24){
let loopTimes = Math.floor(diff / 24);
for(let i = 0; i <= loopTimes; i++){
managerJson = '[';
let clerkJson = '[';
let startClone = start.clone();
let endClone = end.clone();
startClone = startClone.add(i, 'd');
endClone = endClone.add(i, 'd');
let recordJson = '{"開始":{"value":"'+startClone.format("YYYY-MM-DDTHH:mm:ssZ")+'"},"終了":{"value":"'+endClone.format("YYYY-MM-DDTHH:mm:ssZ")+'"},"外注":{"value":"'+outsourcing+'"},"オリジナルカレンダーID":{"value":"'+originalCarendarId+'"},"短縮案件名":{"value":"'+shortProject+'"},"案件名":{"value":"'+project+'"},"種類":{"value":"'+type+'"},';
if(manager.length){
manager.forEach(val => {
managerJson += '{"code":"'+val.code+'"},';
});
managerJson = managerJson.slice(0, -1);
managerJson += ']';
recordJson += '"当日担当者":{"value":'+managerJson+'},';
}
if(clerk.length){
clerk.forEach(val => {
clerkJson += '{"code":"'+val.code+'"},';
clerkJson = clerkJson.slice(0, -1);
clerkJson += ']';
recordJson += '"事務":{"value":'+clerkJson+'},';
});
}
recordJson = recordJson.slice(0, -1);
recordJson += '}';
recordJsonList.push(JSON.parse(recordJson));
}
}else{
managerJson = '[';
let clerkJson = '[';
let recordJson = '{"開始複数日程":{"value":"'+start+'"},"終了複数日程":{"value":"'+end+'"},"外注":{"value":"'+outsourcing+'"},"オリジナルカレンダーID":{"value":"'+originalCarendarId+'"},"短縮案件名":{"value":"'+shortProject+'"},"案件名":{"value":"'+project+'"},"種類":{"value":"'+type+'"},';
if(manager.length){
manager.forEach(val => {
managerJson += '{"code":"'+val.code+'"},';
});
managerJson = managerJson.slice(0, -1);
managerJson += ']';
recordJson += '"当日担当者":{"value":'+managerJson+'},';
}
if(clerk.length){
clerk.forEach(val => {
clerkJson += '{"code":"'+val.code+'"},';
clerkJson = clerkJson.slice(0, -1);
clerkJson += ']';
recordJson += '"事務":{"value":"'+clerkJson+'"},';
});
}
recordJson = recordJson.slice(0, -1);
recordJson += '}';
recordJsonList.push(JSON.parse(recordJson));
}
});
let body = {
'app':76,
'records':recordJsonList
}
return kintone.api(kintone.api.url('/k/v1/records.json', true), 'POST', body)
.then(function(resp) {
let deldeteBody = {
'app':76,
'ids':[thisId]
}
return kintone.api(kintone.api.url('/k/v1/records.json', true), 'DELETE', deldeteBody)
.then(function(resp) {
}).catch(function() {
event.error = '予期せぬエラーが発生しました!';
return event;
});
}).catch(function() {
event.error = '予期せぬエラーが発生しました!';
return event;
});
}
}else{
if(ID){
if(originalCarendarId){
let body0 = {
'app':76,
'query': 'オリジナルカレンダーID = "'+originalCarendarId+'" order by 開始 asc'
}
return kintone.api(kintone.api.url('/k/v1/record.jsons', true), 'GET', body0)
.then(function(resp) {
resp.records.forEach(val => {
let existingTableId = [];//既存テーブルID
let ID = val.ID.value;
let table = val.工事日程テーブル.value;
let thisId = val.カレンダーID.value;
let start = val.開始.value;
let end = val.終了.value;
let outsourcing = val.外注.value;
let manager = val.当日担当者.value;
let managerJson = '[';
let managerList = [];
let body = {
'app':75,
'id':ID
}
return kintone.api(kintone.api.url('/k/v1/record.json', true), 'GET', body)
.then(function(resp) {
let projectTable = resp.record.工事日程テーブル.value;
let recordJson = '{"工事日程テーブル":{"value":[';
projectTable.forEach(row => {
recordJson += '{"id":"'+row.id+'"},';
existingTableId.push(row.id);
if(row.value.当日担当者.value.length){
row.value.当日担当者.value.forEach(val =>{
managerList.push(val.code);
});
}
});
recordJson += '{"value":{"工事終了日時":{"value":"'+start+'"},"工事終了日時":{"value":"'+end+'"},"外注":{"value":"'+outsourcing+'"},"カレンダーID":{"value":"'+thisId+'"},';
if(manager.length){
manager.forEach(val => {
managerJson += '{"code":"'+val+'"},';
managerList.push(val.code);
});
managerJson = managerJson.slice(0, -1);
managerJson += ']';
recordJson += '"当日担当者":{"value":'+managerJson+'},';
}
recordJson = recordJson.slice(0, -1);
recordJson += '}}]}';
let newManagerList = Array.from(new Set(managerList));
if(newManagerList){
recordJson += ',"当日担当者一覧":{"value":[';
newManagerList.forEach(val => {
recordJson += '{"code":"'+val+'"},';
});
recordJson = recordJson.slice(0, -1);
recordJson += ']}';
}
recordJson += '}';
let body2 = {
'app':75,
'id':ID,
'record':JSON.parse(recordJson)
}
return kintone.api(kintone.api.url('/k/v1/record.json', true), 'PUT', body2)
.then(function(resp) {
let body3 = {
'app':75,
'id':ID
}
return kintone.api(kintone.api.url('/k/v1/record.json', true), 'GET', body3)
.then(function(resp) {
let newProjectTable = resp.record.工事日程テーブル.value;
newProjectTable.forEach(row => {
existingTableId.push(row.id);
});
let newTableId = getUniqueValues(existingTableId);
let body4 = {
'app':76,
'id':thisId,
'record':{
'案件管理テーブルID':{
"value":newTableId[0]
}
}
}
return kintone.api(kintone.api.url('/k/v1/record.json', true), 'PUT', body4)
.then(function(resp) {
}).catch(function() {
event.error = '予期せぬエラーが発生しました!';
return event;
});
}).catch(function() {
event.error = '予期せぬエラーが発生しました!';
return event;
});
}).catch(function() {
event.error = '予期せぬエラーが発生しました!';
return event;
});
}).catch(function() {
event.error = '予期せぬエラーが発生しました!';
return event;
});
});//body0_finforEach_fin
}).catch(function() {
event.error = '予期せぬエラーが発生しました!';
return event;
});
}else{
let body = {
'app':75,
'id':ID
}
return kintone.api(kintone.api.url('/k/v1/record.json', true), 'GET', body)
.then(function(resp) {
let projectTable = resp.record.工事日程テーブル.value;
let recordJson = '{"工事日程テーブル":{"value":[';
projectTable.forEach(row => {
recordJson += '{"id":"'+row.id+'"},';
existingTableId.push(row.id);
if(row.value.当日担当者.value.length){
row.value.当日担当者.value.forEach(val =>{
managerList.push(val.code);
});
}
});
recordJson += '{"value":{"工事終了日時":{"value":"'+start+'"},"工事終了日時":{"value":"'+end+'"},"外注":{"value":"'+outsourcing+'"},"カレンダーID":{"value":"'+thisId+'"},';
if(manager.length){
manager.forEach(val => {
managerJson += '{"code":"'+val+'"},';
managerList.push(val.code);
});
managerJson = managerJson.slice(0, -1);
managerJson += ']';
recordJson += '"当日担当者":{"value":'+managerJson+'},';
}
recordJson = recordJson.slice(0, -1);
recordJson += '}}]}';
let newManagerList = Array.from(new Set(managerList));
if(newManagerList){
recordJson += ',"当日担当者一覧":{"value":[';
newManagerList.forEach(val => {
recordJson += '{"code":"'+val+'"},';
});
recordJson = recordJson.slice(0, -1);
recordJson += ']}';
}
recordJson += '}';
let body2 = {
'app':75,
'id':ID,
'record':JSON.parse(recordJson)
}
return kintone.api(kintone.api.url('/k/v1/record.json', true), 'PUT', body2)
.then(function(resp) {
let body3 = {
'app':75,
'id':ID
}
return kintone.api(kintone.api.url('/k/v1/record.json', true), 'GET', body3)
.then(function(resp) {
let newProjectTable = resp.record.工事日程テーブル.value;
newProjectTable.forEach(row => {
existingTableId.push(row.id);
});
let newTableId = getUniqueValues(existingTableId);
let body4 = {
'app':76,
'id':thisId,
'record':{
'案件管理テーブルID':{
"value":newTableId[0]
}
}
}
return kintone.api(kintone.api.url('/k/v1/record.json', true), 'PUT', body4)
.then(function(resp) {
}).catch(function() {
event.error = '予期せぬエラーが発生しました!';
return event;
});
}).catch(function() {
event.error = '予期せぬエラーが発生しました!';
return event;
});
}).catch(function() {
event.error = '予期せぬエラーが発生しました!';
return event;
});
}).catch(function() {
event.error = '予期せぬエラーが発生しました!';
return event;
});
}
}
}
}
});
})();
// return kintone.api(kintone.api.url('/k/v1/record.json', true), 'GET', body)
// .then(function(resp) {
// }).catch(function() {
// event.error = '予期せぬエラーが発生しました!';
// return event;
//});