Asynchronous REST Messages From the Client Side

austindoorlag
Tera Contributor

Hello ServiceNow enthusiasts,

Background:  I have a UI Formatter attached to the Incident table, this UI formatter specifies a UI Macro.  Rendering this UI Macro with the correct data is dependent me making a call to an external API.

When making server-side requests from the client-side, I initialize a GlideAjax and call a client callable script include. 
This script include instantiates a RESTMessageV2, which makes a request to my external API using the .execute() method.

So in essence:  whenever someone opens an incident I'm making several synchronous REST calls in order to retrieve, and display retrieved data in the UI.  Here's a pretty stripped down version of what's happening:

// UI Macro (MyUIMacro)
var ga = new GlideAjax('MyScriptInclude');
ga.addParam('sysparm_name', 'getDataFromRESTMessage');
ga.addParam('sysparm_domain', '');
ga.addParam('sysparm_suppressLogging', true);

ga.getXML(function (response) {
    // do some processing on the returned data
});

// Client Callable Script Include (MyScriptInclude)
getDataFromRESTMessage: function () {
    var request = new sn_ws.RESTMessageV2();
    var response = request.execute():
    return response;
}


Problem: Like I mentioned earlier, the content in my UI is dependent on the response I get from the RESTMessageV2.  Some of the requests I make can take a particularly long time to resolve, which has led several users complain about slowdowns on their ServiceNow instance 

I'm wondering if there is some way I can use the .executeAsync() method here, while still being able to retrieve the data returned from the RESTMessageV2 on the clientside (since rendering the UI is dependent on this data.)

1 ACCEPTED SOLUTION

Tony Chatfield1
Kilo Patron

Hi, if your solution is dependent on external data being present before loading your UI then I would not think there was any option other than to wait for it to be returned. Perhaps the solution design needs to be reviewed as this type of UI dependency rarely results in good user experience.

View solution in original post

1 REPLY 1

Tony Chatfield1
Kilo Patron

Hi, if your solution is dependent on external data being present before loading your UI then I would not think there was any option other than to wait for it to be returned. Perhaps the solution design needs to be reviewed as this type of UI dependency rarely results in good user experience.