- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2020 03:15 AM
Hi Guys,
We have scripted rest API which gets the request from another API. we have to update this request in a field in a table. I am able to get the request details & i can see the request body by capturing in the logs but when i am trying to set the same body to the concerned field in the required table , i couldn't do so, it's empty. (pls see the screen shots for reference).
I need that request body to be updated to the particular field . Any help on this please ?
Solved! Go to Solution.
- Labels:
-
Integrations
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2020 04:32 PM
as discussed over hangout , we solved it with below line.
var parser = new JSON();
var obj = parser.decode(ab);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2020 01:47 PM
Hi Harsh,
First I am using the Brule to Post Trigger REST Message
(function executeRule(current, previous /*null when async*/ ) {
// Add your code here
var ciname = current.cmdb_ci.getDisplayValue();
gs.addInfoMessage("ciname" +ciname );
var r = new sn_ws.RESTMessageV2('OrchestrationScript', 'POST');
r.setStringParameterNoEscape('uid', 'svc_SNOW');
r.setStringParameterNoEscape('Name', '<-->-SystemDriveCleanup-WIN');
r.setStringParameterNoEscape('description', 'Test');
r.setStringParameterNoEscape('callbackURL', 'https://--------sandbox.service-now.com/api/thlr/pvk_orchestration_test_inbound');
//r.setStringParameterNoEscape('callbackHeaders', '');
r.setStringParameterNoEscape('computers', 'c213fxvinfdev');
//https://--------sandbox.service-now.com/api/thlr/pvk_orchestration_test_inbound
//https://------ssandbox.service-now.com/api/thlr/orchestration1
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
var obj = JSON.parse(responseBody);
var job_id = obj.status_text.JobID;
gs.addInfoMessage(job_id);
current.work_notes = 'Open Automation has taken over processing the request with the jobID ' + job_id;
current.update();
var ga = new GlideRecord('u_test_table_automation');
ga.initialize();
ga.u_jobid = job_id;
ga.u_number = current.number;
ga.u_name = 'TR-SystemDriveCleanup-WIN';
ga.u_computers = 'c213fxvinfdev';
ga.insert();
var number = current.number;
var inc_number = new GlideRecord('u_orchestration_incidents_test');
inc_number.addQuery('u_incident_number', 'number');
inc_number.query();
if (inc_number.next()) {
current.work_notes = "number is there";
current.update();
} else {
inc_number.initialize();
inc_number.setValue('u_incident_number', number);
inc_number.setValue('u_inc_number', number);
inc_number.setValue('u_job_id', job_id);
inc_number.insert();
}
})(current, previous);
After the BRule triggers the post message & creates a job in the other application ,
the response will be sent back in form of request in the scripted rest api URL which we mention at callbackURL parameter set at the above Brule.
the code in post method scripted rest api
(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
// implement resource here
var requestBody = request.body;
var c = request.body.dataString;
var c1 = request.body.data;
gs.log("PVK putting the request body"+c);
var response_details = new PVK().response_details(c);
}
})(request, response);
The script include code
var PVK = Class.create();
PVK.prototype = {
initialize: function() {
this.response_payload = null;
this.job_i_d = null;
},
response_details: function(payload) {
var response_payload = payload;
var job_id = response_payload.JobID;
this.job_i_d = job_id;
var parser = new JSON();
var pay = parser.encode(response_payload);
this.response_payload = pay;
// gs.log("firstly , the job id & payload are " +this.response_payload +" " +this.job_i_d);
this.pvk1();
},
pvk1: function() {
// gs.log("secondly , the job id & payload are " +this.response_payload +" " +this.job_i_d);
var inc_test = new GlideRecord('u_orchestration_incidents_test');
inc_test.addQuery('u_job_id', this.job_i_d);
inc_test.query();
if (inc_test.next()) {
gs.log('PVK Row count is '+inc_test.getRowCount());
gs.log("pulagam PVK it's there but why ?");
// inc_test.setValue('u_response_body_payload', response_payload);
inc_test.u_test_checkbox = true;
inc_test.u_test_string_value = "updated by pulagam vamsi";
inc_test.u_response_body_payload = this.response_payload;
inc_test.update();
}
// var i = new GlideRecord('incident');
// i.addEncodedQuery('number=INC1868296');
// i.query();
// if (i.next()) {
// i.work_notes = "updated by PVK";
// i.update();
// }
},
type: 'PVK'
};
The below is the value i am trying to set in a field value in u_orchestration_incidents_test table
i have asked by providing detailed information including screen shots at https://community.servicenow.com/community?id=community_question&sys_id=64ea5bfa1bcb8c50ada243f6fe4b...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2020 01:52 PM
Later i changed the piece of code so that, it would be hopeful at-least if it updates the incident ........ but no luck unfortunately.
i did this in POST method & then PUT method still it's not working . I am even not sure what mistake have i done.
(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var c = request.body.dataString;
var c1 = request.body.data;
gs.log("PVK the c value is "+c);
var i = new GlideRecord('incident');
i.addEncodedQuery('number=INC1868296');
i.query();
if (i.next()) {
gs.log("the number is"+i.number);
i.description = "why the hell ?";
i.work_notes = "updated by PVK";
i.update();
}
var response_details = new PVK().response_details(c);
})(request, response);
Any help on this would be very much thankful.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2020 01:53 PM
is it possible for quick webex or connect me on google hangout for screen-sharing ?
hvrdhn88@gmail.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2020 02:06 PM
yes sure i have sent the invitation. Thank you

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2020 04:32 PM
as discussed over hangout , we solved it with below line.
var parser = new JSON();
var obj = parser.decode(ab);