- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-29-2016 04:05 AM
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
Solved! Go to Solution.
- Labels:
-
Integrations
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-02-2016 02:11 AM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-30-2016 07:35 AM
what are the conditions on the business rule?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-30-2016 09:53 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-30-2016 10:06 PM
HI
Can you please correct the code.
Use r.setRequestHeader('Content-Type','application/json');
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-30-2016 11:49 PM
Hi Indrajeet,
I changed the code but still see the same result.
Regards,
Karan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-01-2016 02:28 AM
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