Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

Question regarding executeAsync() RESTMessageV2 in outbound

Palle
Tera Contributor

Hi Community,

I have created an asynchronous business rule to trigger a REST Message which execute asynchronously (using executeAsync() method) to create a ticket in third party system. As per my understanding any lines after  executeAsync() method will not work. But, in my case I had defined getStatusCode and getBody methods in the script after executeAsync(), and I am able to parse the response. How is this possible? I am missing something? or my understanding is incorrect?

Thanks

 

6 REPLIES 6

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

Share your script.

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi @Ankur Bawiskar below is the function I had.

Script Include: 

sendNewTicket: function(incident){

var newTicket = new sn_ws.RESTMessageV2('rest message','post');
 newTicket.setRequestHeader('Authorization','Bearer '+this.accessToken);
 newTicket.setRequestHeader('Content-Type','application/json');

 var requestBody = {"number": "123456"};

 newTicket.setRequestBody(JSON.stringify(requestBody));

 var response = newTicket.executeAsync();

var httpStatus = response.getStatusCode();
 var responsebody = response.getBody();
 var parseResponse = JSON.parse(responsebody);

var ticketId = parseResponse.id;

// I am updating incident object from here by passing current object from business rule to script include

incident.correlation_id = ticketId

incident.update();

}

Hi @Ankur Bawiskar  below is the function I had in the script include.

 

sendNewTicket: function(incident){

var newTicket = new sn_ws.RESTMessageV2('rest message','post');
 newTicket.setRequestHeader('Authorization','Bearer '+this.accessToken);
 newTicket.setRequestHeader('Content-Type','application/json');

 var requestBody = {"number": "123456"};

 newTicket.setRequestBody(JSON.stringify(requestBody));

 var response = newTicket.executeAsync();

var httpStatus = response.getStatusCode();
 var responsebody = response.getBody();
 var parseResponse = JSON.parse(responsebody);

var ticketId = parseResponse.id;

// I am updating incident object from here by passing current object from business rule to script include, ex:

incident.correlation_id = ticketId

incident.update();

}

suvro
Mega Sage

This KB may be helpful

 

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0694711