kintoneでは、複数のユーザーが同時に1つのレコードを編集した場合、保存時にエラーが出るようになっています。
この機能によってレコードの先祖返りは防げますが、せっかく編集したレコードが保存できないのは残念ですよね。 今回は、複数のユーザーが同時に1つのレコード編集画面を開くこと自体を防ぐカスタマイズを紹介します。
サンプル
あるユーザーがレコード編集画面を開いているときには、ほかのユーザーがレコード編集画面を開けないようにします。
フォーム設定
アプリに以下のフィールドを追加します。
コード
下記「sample.js」を読み込みます。
・sample.js
(function() {"use strict";varrevisionCheck=function(event) {returnkintone.api(kintone.api.url('/k/v1/record',true),'GET', {
app:kintone.app.getId(),
id:kintone.app.record.getId(),
}).then(function(response) {returnevent.record.$revision.value===response.record.$revision.value;
});
};varchangeState=function(editing) {returnkintone.api(kintone.api.url('/k/v1/record',true),'PUT', {
app:kintone.app.getId(),
id:kintone.app.record.getId(),
record:{
編集中:{
value:editing?kintone.getLoginUser().id:0}
}
});
};kintone.events.on(['app.record.detail.show',
],function(event) {revisionCheck(event).then(function(revisionMatch) {if(!revisionMatch)location.reload();
});
});kintone.events.on(['app.record.edit.show',
],function(event) {revisionCheck(event).then(function(revisionMatch) {if(!revisionMatch) {location.reload();return;
}if(Number(event.record.編集中.value)) {if(event.record.編集中.value!==kintone.getLoginUser().id) {alert('ほかのユーザーが編集中です.');history.back();return;
}document.getElementsByClassName('gaia-ui-actionmenu-cancel')[0].addEventListener('click',function() {changeState(false).then(function() {location.reload();
})
})window.addEventListener('beforeunload',function(e) {changeState(false);
});window.addEventListener('unload',function(e) {changeState(false);
});
}else{changeState(true).then(function() {location.reload();
});
}
});
});kintone.events.on(['app.record.edit.submit.success',
],function(event) {changeState(false);
});
})();
※Chrome、Firefoxで動作確認しております。
※Chromeでは「beforeunload」イベントで編集中のフラグを除去しているため、ページ離脱をキャンセルした場合は正しく動作しません。(解決方法をご存じでしたら、ぜひ教えていただきたいです。)
※DOMのclass名を利用しているため、kintoneの仕様変更により動作しなくなる可能性があります。