Async/Await error on event Kintone - app.record.create.change.{fieldcode} is not allowed to return "Thenable" object

Situtation

Hi, I am trying to catch the event change on a Dropdown and then get the value from the reference App.
After that, I need to update the table row with the API call’s results.

Script

This is my code below:

const tableStaff = 'カット対応人数';
const appStaff = 3516;
kintone.events.on('app.record.create.change.店舗', function (event) {
  let record = event.record;
  getRecords('店舗 = "' + record['店舗']['value'] + '"', appStaff).then(result => {
    event.record[tableStaff].value = [];
    result.records.forEach(function (staff) {
      let staffData = {
        id: null,
        value: {
          "スタッフ": {
            type: 'SINGLE_LINE_TEXT',
            value: staff['スタッフ']['value']
          },
          "人数": {
            type: 'NUMBER',
            value: 0
          },
        }
      };
      event.record[tableStaff].value.push(staffData);
    });
    console.log(event);
    return event;
  });
});

Error

My Problem is this function always returns the event before getRecords() completes.
I’ve tried to use Async/await on the getRecords() function. Usually, it works, but this time, I got the following error:

Uncaught Error: app.record.create.change.店舗 is not allowed to return "Thenable" object.

I do not know why I got this error.
Could you explain what happens when I use Async/Await in a Kintone customization?

Hello @tuankiet116

The error you see is likely due to using an event not supported by Promise(Async/Await).
A list of events that are supported is noted on the following page:

Async/await uses Promise objects.
Using async/await has the following pros:

  • Easier and more intuitive to write than Promise
  • No need to use then() to connect multiple asynchronous operations
  • You can also use Promise variables such as then() or all()

Comparison Example | Obtaining another record upon opening record details

In the case of Promise

kintone.events.on('app.record.detail.show', () => {
return kintone.api('/k/v1/record', 'GET', {app: 1, id: 1}).then((resp) => {
console.log(resp); // Display obtained data
});
}

In the case of Async/Await

kintone.events.on('app.record.detail.show', async () => {
const resp = await kintone.api('/k/v1/record', 'GET', {app: 1, id: 1});
console.log(resp); // Display obtained data
};

*Above information is based on the page available only on the Japanese Kintone Developer page.

1 Like