- 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-29-2016 05:02 AM
Hi karan
you can use
- try {
- r = new sn_ws.RESTMessageV2('Incident Notes Send', 'put');
- r.setStringParameter("short_description", current.short_description);
- response = r.execute();
- responseBody = response.getBody();
- httpStatus = response.getStatusCode();
- }
- catch(ex) {
- message = ex.getMessage();
- }
- You have to define the parameter "short_description" in variable substitution of put function in REST Message.
- you can define the Testvalue of short_description anything. The value will be override with current.short_description value.
PLease hit like if it is helpful
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-29-2016 05:29 AM
Hi Indrajeet,
Thanks for the suggestion, I'll try it out!!
Can I define multiple such variables and use the r.setStringParameter method to define them all and send them in one go?
Regards,
Karan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-30-2016 05:36 AM
Yes you can define multiple variables
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-30-2016 07:20 AM
Hi Indrajeet,
Thanks for your suggestions. I've changed the script in the business rule to
function onAfter(current, previous)
{
var r;
var response;
var responseBody;
var httpStatus;
var message;
try {
r = new sn_ws.RESTMessageV2('Incident Notes to event', 'put');
r.setRequestHeader("Accept","application/json");
r.setRequestHeader('Content-Type','application/json-patch+json');
r.setStringParameterNoEscape("fieldone", current.u_bppm_eventid);
r.setStringParameter("fieldtwo", current.work_notes.getJournalEntry(1));
response = r.execute();
responseBody = response.getBody();
httpStatus = response.getStatusCode();
gs.log("getBody: " + response.getBody());
gs.addInfoMessage("getStatusCode: " + response.getStatusCode());
gs.print("getHeaders " + response.getRequestBody());
}
catch(ex) {
message = ex.getMessage();
}
}
Although the rule is triggered(as seen in the debug section) and the REST message in itself runs successfully(I have defined the substitution variables and test values along with the json format). I can't see the results when the business rules trigger it. I have tried logging(gs.log,gs.print,gs.addInfoMessage) but I can't see anything in the business rules or log debugging.
How can I make sure that the business rule is triggering the REST message? Also can I log the payload being sent by the REST message once triggered by the rule?
Regards,
Karan