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
Tera 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

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
Kilo Sage

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

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);
});
}