Send Incident details via outbound REST

karanpreet
Tera Guru

Hi,

I'm trying to trigger a business rule to send out any work notes or additional comments to a 3rd party tool via outbound REST.

I have the outbound request created but I need to be able to get certain fields from the incident and include them in the JSON to send. For eg. I need the value of the "caller_id" so that I can update the original event in the 3rd party tool, similarly I would need the value for the work notes and the additional comments and create a JSON with them to be sent.

How can this be done?

I have created a static JSON in the "HTTP Query Parameters" section of the outbound REST message and was able to successfully test it.

I have also created the Business rule to be triggered and defined the "When to Run" conditions correctly, I have also defined a basic advanced script to call the outbound REST message like so:

function onAfter(current, previous)

{

var r;

var response;

var responseBody;

var httpStatus;

var message;

     

try {

r = new sn_ws.RESTMessageV2('Incident Notes Send', 'put');

response = r.execute();

responseBody = response.getBody();

httpStatus = response.getStatusCode();

}

catch(ex) {

message = ex.getMessage();

}

gs.log("Content: " + current.work_notes.getJournalEntry(1));

gs.log("Request Body: " + response);

gs.log("Response: " + responseBody);

gs.log("HTTP Status: " + httpStatus);

gs.log("Error Message:" + message);

}

But I'm not sure how to pass variable from here to the REST request.

Also as I understand I should see the "gs.log()" entries in the debug business rules but I see only that the business rule was triggered.

I'm very new to ServiceNow and any help from the gurus would be much appreciated.

Regards,

Karan

1 ACCEPTED SOLUTION

karanpreet
Tera Guru

Hello,



Thanks for all the help. I was finally able to solve this.



It turned out that the request I was sending was generating errors on the other side due to the format.



The final version of the javascript was :



try {


      var r = new sn_ws.RESTMessageV2('Incident Notes to event', 'put');


      var notes = current.work_notes.getJournalEntry(1);


      notes=notes.replace(/["']/g, "");


      notes=notes.replace(/[\n*]/g, " ");


      notes =   String(notes);


      var id = String(current.u_eventid).replace(/["']/g, "");


      r.setStringParameter("id",id);


      r.setStringParameter("notes",notes);


      r.setRequestHeader("Accept","application/json");


      r.setRequestHeader('Content-Type','application/json');


      var response = r.execute();


      var responseBody = response.getBody();


      var httpStatus = response.getStatusCode();


      gs.addInfoMessage("Response Body:" + responseBody);


      gs.addInfoMessage("Response status:" + httpStatus);


}


catch(ex) {


      var message = ex.getMessage();


}



Thanks again for the help in getting this work!


Regards,


Karan


View solution in original post

15 REPLIES 15

what are the conditions on the business rule?


Hi Abhinay,



The conditions on the business rules are as follows:



find_real_file.png



Regards,


Karan


Indrajeet
Mega Expert

HI


Can you please correct the code.


Use r.setRequestHeader('Content-Type','application/json');



Hi Indrajeet,



I changed the code but still see the same result.



Regards,


Karan


Hi,



I changed the code after taking a look at the preview script usage in the REST message screen. The code now works and is as follows:



try {


var r = new sn_ws.RESTMessageV2('Incident Notes to event', 'put');



var id = String(current.u_eventid).replace(/["']/g, "");


//var notes = String(current.work_notes.getJournalEntry(1));



r.setStringParameter('id',id);


r.setStringParameter('notes', 'Business rule note!');


//r.setStringParameter('notes', notes);



var response = r.execute();


var responseBody = response.getBody();


var httpStatus = response.getStatusCode();


gs.addInfoMessage("Response Body:" + responseBody);



}


catch(ex) {


var message = ex.getMessage();


}



I can see the event being updated when a work note is added.



The only thing that's not working now is when I try to get the work note from the incident itself. The autocomplete doesn't show getJournalEntry as an option in the drop down and when I include it in the code, the request doesn't go through successfully as I see the response body as "Null" in the InfoMessage.



I also tried getDisplayValue but to no success.



Regards,


Karan