Dropdown method not working from Event Handler (VIDEO)

Please watch this video:

End goal:
After a new item is created for a Dropdown List,
I want to auto-select that newly created item.

It works in the debug window, but not in the event handler.

Hi @Tim_H Is it always selecting the record right before the one that you want selected?

It does seem like a timing issue since it's re-fetching the new list at the same time that it is selecting the latest. Instead of using a setTimeout, can we test this timing theory by moving the script to run on success of the VendorTypesList query instead of on success of the insert query?

1 Like

Hello,

In additional to what Tess was saying:

The first issue is you have multiple event handlers and they are running asynchronously. However, your operation is not...new data is needed before updating the dropdown to the last value. Hence, the attempt with setTimeout.

Second issue is <query>.data is not immediately available after .trigger() finished during runtime, you can take a look at this old post back in 2021 to understand: Await for a query to finish - #7 by minijohn

I was able to get it working using the solution in the old post. Note: replace the contents inside setvalue with your Math.max(...) but use the const variable instead of VendorTypesList.data.

modal1.close();

const ddData = await new Promise((resolve) => {
  loadData.trigger({
    onSuccess: (data) => {
      resolve(data);
    },
  });
});

select1.setValue(ddData.value[ddData.value.length-1]);

Hope that'll help.

2 Likes

Thanks for your help!

I solved it by putting the setValue() INSIDE The promise. For some reason it didn't work after the promise.

// inside the INSERT QUERY onSuccess event
NewVendorModal.close()

// auto-select the max ID (last inserted)
const ddData = await new Promise((resolve) => {
  VendorTypesList.trigger({
    onSuccess: (data) => {
     
VendorSelect_AddCostForm.setValue(Math.max(...data.vendor_id))

      resolve(data);
    },
  });
});


@Tess - To answer your question, I tried putting my code into the new query success... and that ALMOST worked, but it did as you guessed and selected the item BEFORE the actual correct item.

1 Like