The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Asynchronous call between REST API and Script Include

Rob Bushrod
Tera Guru

Hi All,

I am trying to set up a REST API to create a catalog item based on a value within the received payload; New user, Update User, Disable Account.

Part of the requirement is to send back to the 3rd party system that is raising the request, the sys_id and reference number of one of the tasks that is being created from the workflow. This was being achieved by calling a script include that is checking the sc_task table for a task that is associated to the RITM and has a particular assignment group. This was working fine in dev but when promoted to production kept returning an empty result. 

On investigation by ServiceNow for why no value was being returned in prod but being returned in dev the answer was that it shouldn't really be working in dev as the the timing means I'm querying for a uncommitted/partially committed record. Their advise was to try and run it asynchronously so that the request item and tasks are fully committed.

I have been working on this today but can't seem to get it right as the script messages keep giving me a yellow '!' about the new function being in the wrong place when trying to create a GlideAjax function. 

 

Any suggestions gratefully received, attached is the code from the section of the scripted REST API and the script include where I am trying to do the log up. The commented out section was the original with the new functions above.

 

Rob

6 REPLIES 6

Oya Orhan
Giga Guru

Hi @Rob Bushrod,

I think your code is not working in the front interface, so you don't need to use AJAX. From where are you calling Script Include? 

Can an advanced business rule which runs async solve your issue?

 

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.

Regards,

Oya

Hi @Oya Orhan,

 

I am calling the script include from within the Scripted REST API, originally in this line; 

// var sctaskNum = ameliaUtils.getSCtask(ritmSys);

However I have then tried to do it in a GlideAjax as the suggestion was to do this asynchronously.

I don't think a business rule is the answer as I need the values returned in the Scripted REST API so that I can return them in the response to the 3rd party system.

 

Many Thanks,

Rob

GlideAjax is certainly is not your answer as "The GlideAjax class enables a client script to call server-side code in a script include." 

I think you are working on the payload and in the same payload you want to send information back to your 3rd party. Splitting these would help you. First work on the incoming payload. Create the catalog item and the task. Then trigger sending the information to the 3rd party by using an async BR with the required conditions.

ashishdevsingh
Tera Expert

Hi @Rob Bushrod ,

Please correct my understanding if I am wrong. So, you are receiving the payload and creating the catalog record via workflow. Now the problem is: you need to send the payload but you are getting empty value. do you want to send the data by using another outbound or as a response you need to send. ? 

 

If you are sending via another outbound servicesthen: 

If this is the scenario, I would suggest you to not use glideAjax. Instad of that, store all values in workflow.scratchpad and when you insert the record in workflow activity, add one more parameter as workflow.scratchpad.sys_id. then create the task and store task info in workflow.scratchpad. 

 

In next activity give pause for 5 sec and add another activity to create payload by using all workflow.scratchpad info and send back to the system by triggering the outbound services. 

 

If you are trying to send the sysId and associated task info in the return of response then, i think it is not a good idea to hold the rest response cycle.