Have you ever written code like this in a kintone JavaScript customization?
kintone.events.on('app.record.create.show', (event) =>
event.record.対応状況.value = '未対応';
);The code runs. There are no errors in the console.
Yet, when you look at the screen, the value hasn't changed.
When this happens, this is the first thing you should check.
return event;
After fixing it, the code looks like this.
kintone.events.on('app.record.create.show', (event) =>
event.record.対応状況.value = '未対応';
return event;
);It's only one line, but it's a very important line in kintone customization.
Just changing the 'event' object does not complete the instruction to kintone

In kintone event handlers, an event object is passed as an argument.
(event) => // eventを使った処理
This 'event' contains information such as the type of event that occurred and the record data.
For example, if you want to rewrite a field value, you do it like this.
event.record.対応状況.value = '未対応';
However, this only rewrites the 'event' within the event handler.
To pass the processed result to kintone, you must return the event object as the return value.
return event;
In other words, the following two are a set.
Process the 'event'
Return the processed 'event'
'return event' is not just some magic spell you write at the end for no reason.
It is code used to return the processing result to kintone, saying, 'Please continue the event processing with this content.'
It is also necessary for things other than rewriting values
'return event' is not only needed when rewriting field values.
For example, even when displaying an error at the top of the screen, you set 'event.error' and return the event object.
kintone.events.on('app.record.create.submit', (event) =>
if (!event.record.会社名.value)
event.error = '会社名を入力してください。';
return event;
);The same applies when displaying errors at the field level.
kintone.events.on('app.record.create.submit', (event) =>
const companyName = event.record.会社名;
if (!companyName.value)
companyName.error = '会社名は必須です。';
return event;
);Additionally, depending on the event type, you return the event object for operations such as the following:
Rewriting field values
Setting field editability
Setting error messages
Instructing to fetch or clear lookups
Specifying the destination URL after a successful save
Which operations are available depends on the event type. Writing return event does not mean you can do anything in every event.
When there are multiple handlers for the same event, look until the final return
A case that is slightly difficult to spot is when multiple event handlers are registered for the same event type.
kintone.events.on('app.record.create.show', (event) =>
event.record.対応状況.value = '未対応';
return event;
);
kintone.events.on('app.record.create.show', (event) =>
console.log('画面を表示しました');
// return eventがない
);The official documentation explains that if there are multiple handlers for the same event type, the value returned by the last handler is used as the basis for reflection.
In other words, even if you return event in the handler you wrote, if a handler executed later returns nothing, the values may not be reflected.
If you find that it is not working even though you are properly executing return event, please check other handlers registered for the same event.
For asynchronous processing, return a Promise instead of an event
Sometimes you may want to set values to a record after fetching data from another app using the REST API.
In this case, you must not return event prematurely on the outside.
// うまくいかない例
kintone.events.on('app.record.create.submit', (event) =>
kintone.api(kintone.api.url('/k/v1/record.json', true), 'GET',
app: 10,
id: 1
).then((response) =>
event.record.価格.value = response.record.価格.value;
);
return event;
);Doing so may cause the event processing to proceed without waiting for the API response.
For events that support Promises, return a Promise. Within that Promise, return the event at the end.
kintone.events.on('app.record.create.submit', (event) =>
return kintone.api(
kintone.api.url('/k/v1/record.json', true),
'GET',
app: 10,
id: 1
).then((response) =>
event.record.価格.value = response.record.価格.value;
return event;
);
);If you use async / await, you can write it more readably.
kintone.events.on('app.record.create.submit', async (event) =>
const response = await kintone.api(
kintone.api.url('/k/v1/record.json', true),
'GET',
app: 10,
id: 1
);
event.record.価格.value = response.record.価格.value;
return event;
);An async function returns a Promise. Since that Promise ultimately returns the event, kintone can receive the processed event object after the asynchronous processing is complete.
However, not all events support Promises. When including asynchronous processing, check the official documentation for the target event to see if it is marked as 'Promise-supported'.
So, should you always 'return event' every time?
Strictly speaking, if you are not modifying the event object and do not need to reflect any results back to kintone, 'return event' is not necessarily mandatory.
For example, if you are just outputting information about the displayed record to the console, you are not using the return value.
kintone.events.on('app.record.detail.show', (event) =>
console.log(event.record);
);Even so, when writing kintone event handlers, it is safer to make it a standard practice to write 'return event' at the end.
kintone.events.on('イベントタイプ', (event) =>
// 必要な処理
return event;
);This prevents missing returns even if you add field rewrites or error settings later on.
The important thing is not to memorize it as an 'absolute rule'.
If you modify the event object, return the result to kintone.
If you need to wait for asynchronous processing, return the Promise representing that process, and finally return the modified 'event'.
If you understand these two points, you can reduce the time spent worrying about why 'the code is running but not reflected on the screen' due to a missing 'return event'.
Summary
Return modified 'event' content to kintone using 'return event'
This is necessary for tasks like rewriting values, displaying errors, and setting editability
If there are multiple handlers for the same event, check the return value of the final handler
For asynchronous processing, return a Promise and return the 'event' at the end of that process
Check the official documentation for each event to see if Promises can be used
While not strictly required for processes that don't change anything, writing it as a standard practice helps prevent missing returns
'return event' is not a magic spell; it is the exit point for passing processing results between the event handler and kintone.
Reference
Operations that can be performed with the event object cybozu developer network
Let's try using the kintone JavaScript API cybozu developer network
Basics of writing Promises in kintone cybozu developer network
...Um, I only mentioned in casual conversation with an AI that I wanted to write an article on this topic, and the content came out, so now I have nothing left to write. Is this what it means to be 'gyurareta' (scooped/beaten to the punch)?