How to define priorities (order) of handlers in UI Builder component's event ?

ALEXANDER LYUBA
Kilo Explorer

Hi,

I added 2 event handlers to "Click button" event in UI builder - the first handler should create new record by script, the second one is "link to destination" and it should open external URL, where parameter is "sys_id" of newly created record. But during run time  the "link to destination" handler is invoked first always (tested by debugger). How is possible to manage the order of execution ?  Thanks in advance

4 REPLIES 4

Mark Manders
Mega Patron

Can you share the script on the first event? It should be possible to redirect directly instead of via a second event.

If my answer helped you in any way, please then mark it as helpful.

Mark


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

ALEXANDER LYUBA
Kilo Explorer

Hi Mark,

The following is my script :

function handler({api, helpers, event, imports}) {
  debugger;
  helpers
    .snHttp("/api/now/table/x_728919_productor_logical", 
      {
        method: "POST",
        body: {
          lagname: api.state.lagSys_id
        }
    })
    .then(({ response }) => {
      // handle POST request response
      api.setState('logicalSys_id', response.result.sys_id);
      console.log("Sys_id =" + api.state.logicalSys_id );
    })
    .catch(({ error }) => {
      // handle POST request errors
      console.log("error" );
    });
}

............

I need to redirect to (full link with parameter) :

"https://dev117836.service-now.com/x_728919_productor_logical.do?sys_id=" 
        + api.state.logicalSys_id
        + "&sys_is_list=true&sys_target=x_728919_productor_logical&sysparm_checked_items=&sysparm_fixed_query=&sysparm_group_sort=&sysparm_list_css=&sysparm_query=&sysparm_referring_url=x_728919_productor_logical_list.do&sysparm_target=&sysparm_view=";
 
 
 
Thanks for your help.

Tadz
Tera Guru
Tera Guru

Hi did you resolved this? I encountered the same issue. In my case i have 2 events, one is calling a client script and the other is setting a form field value. however there is some delay on the setting form field value, its getting the previous value not the latest one.

Ravi Gaurav
Giga Sage
Giga Sage

Hi @ALEXANDER LYUBA 

when multiple event handlers are attached to the same event, the order of execution isn't guaranteed unless explicitly controlled.

Create a single event handler that performs both actions—creating the record via the API and then navigating to the desired URL after receiving the sys_id.

function handler({ api, helpers }) {
debugger;

// Make the POST request to create the record
helpers
.snHttp("/api/now/table/x_728919_productor_logical", {
method: "POST",
body: {
lagname: api.state.lagSys_id,
},
})
.then(({ response }) => {
// Extract the sys_id from the response
const logicalSysId = response.result.sys_id;
api.setState('logicalSys_id', logicalSysId);
console.log("Sys_id = " + logicalSysId);

// Redirect to the external URL
const url = `https://dev117836.service-now.com/x_728919_productor_logical.do?sys_id=${logicalSysId}&sys_is_list=t...`;

// Use window.location.href for redirection
window.location.href = url;
})
.catch(({ error }) => {
// Handle errors
console.error("Error creating record:", error);
});
}

--------------------------------------------------------------------------------------------------------------------------


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

ļ”— YouTube: https://www.youtube.com/@learnservicenowwithravi
ļ”— LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/