Parsing JSON From REST API Request

Mike C2
Kilo Contributor

Hello all. Long time lurker, first time poster!

 

To start with, I appreciate everything I've been able to take in over the years. It's only because of this Community that I'm at the point I am now, and whilst that is a "point" of trouble I'm appreciative that I'm at a place of asking for help.

 

Long story short, I'm working to set up a ServiceNow to ServiceNow integration. What I've currently done;

- On target instance, table has been created, extends import set, necessary fields populated, transform map built

- On target instance, REST user has been created, roles provisioned correctly, validated with REST API EXPLORER that user can provision new tickets

- On source instance the Outbound REST message has been created for a POST to the staging table, the "CONTENT" block of the HTTP request is populated with variables

- On source instance, a business rule has been created to run "ON INSERT" to Incident table in source. It replaces the variables of the HTTP request block with the ticket information and sends it, as well as captures the response and logs information from target instance into the ticket on source instance.

 

 

So far EVERYTHING is working except I cannot gather the SYS_ID and NUMBER from the response. Below is the code from my business rule. I'm definitely weak on this subject matter but working to correct that. In checking the syslog I see a few lines of entry that indicate "org.mozilla.javascript.EcmaError: Empty JSON string" so I'm pretty sure my issues reside in parsing the JSON.

 

 

 

 

(function executeRule(current, previous /*null when async*/) {

// Add your code here
var request = new sn_ws.RESTMessageV2("Target ServiceNow Instance", "create_incident");
request.setStringParameterNoEscape("presincnum", current.number);
request.setStringParameterNoEscape("presincsys", current.sys_id);
request.setStringParameterNoEscape("abrincnum", current.u_client_incident_number);
request.setStringParameterNoEscape("shodes", current.short_description);
request.setStringParameterNoEscape("des", current.description);
request.setStringParameterNoEscape("incsta", current.incident_state);
request.setStringParameterNoEscape("imp", current.impact);
request.setStringParameterNoEscape("urg", current.urgency);
request.setStringParameterNoEscape("pri", current.priority);
request.setStringParameterNoEscape("connam", current.caller_id.name);
request.setStringParameterNoEscape("conema", current.caller_id.email);
request.setStringParameterNoEscape("conite", current.cmdb_ci);
request.setStringParameterNoEscape("addcom", current.comments);
request.setStringParameterNoEscape("rescod", current.close_code);
request.setStringParameterNoEscape("resnot", current.close_notes);
request.setStringParameterNoEscape("respar", current.u_sync_ticket);

var response = request.execute();
var requestBody = request.getRequestBody();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();

parsedJSON = JSON.parse(responseBody);

var result = parsedJSON["result"];
var targetTicNumber = result[0]["display_value"];
var targetSys_id = result[0]["sys_id"];
current.u_client_sysid = targetSys_id;
gs.log(targetSys_id);

})(current, previous);

 

1 ACCEPTED SOLUTION

The last thing I can think of would be to perhaps execute the process using credentials with admin and see if that makes any difference. If it does, it might be a role or something getting in the way. If it doesn't work with admin, I'd review the logs on your target instance. Maybe an error is occurring that doesn't stop the record creation, but stops the response?

I hope this helps!
Michael D. Jones
Proud member of the GlideFast Consulting Team!

View solution in original post

22 REPLIES 22

last try

gs.log(parameterArr.result[0].display_value);

Getting "Cannot read property "0" from undefined". If the entire variable is coming back as undefined that would mean I've passed nothing into it?

If that's the case I'm just not sure why nothing is being passed into it. The POST request is definitely creating a ticket on the TARGET instance.

This your response is correct. I'm running same script in background script and works fine. try it

var body = '{"import_set":"ISET0010003","staging_table":"u_incidents_for_rest","result":[{"transform_map":"Incidents","table":"incident","display_name":"number","display_value":"INC0010046","record_link":"https://dev82263.service-now.com/api/now/table/incident/cbfc7c97db9e40109411a9954b961989","status":"inserted","sys_id":"cbfc7c97db9e40109411a9954b961989"}]}';

var parser = new JSONParser();
var parameterArr = parser.parse(body);

gs.print(parameterArr.result[0].display_value);

Yes, I've done some additional testing and it seems I'm not getting a response from TARGET instance.

 

I'm able to set up an API POST request to go straight to the Incident table on TARGET instance and I successfully get a response that I can scrub through. However, when going through the staging table I'm getting nothing back.

So, this line looks a little different that I would expect:

var request = new sn_ws.RESTMessageV2("Target ServiceNow Instance", "create_incident");

I've usually seen something more like 

var request = new sn_ws.RESTMessageV2("Target ServiceNow Instance", "post");

Where the second param is the name of the method. 

Maybe you don't get a response because you're not passing an expected method?

I hope this helps!
Michael D. Jones
Proud member of the GlideFast Consulting Team!