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

Mike Patel
Tera Sage

try

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

gs.log(parameterArr.result[0].number;

gs.log(parameterArr.result[0].sys_id;

 

codycotulla
Tera Guru

Hi Mike,

You say that you are able to validate using the REST explorer.

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

When you validate through the REST explorer what JSON is returned? Since you say in a later post that the response body is blank. It might be that you are not returning any content in the response body.

So, start on the Target instance with the REST explorer and make sure that you are getting a response body and that it contains the content you expect.

Hope this helps.

Thanks,

Cody

Mike C2
Kilo Contributor

@cody 

According to the REST API on TARGET system the response for a call is;

{
  "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"
    }
  ]
}



@Mike Patel 
I've updated my Business Rule to be the following
find_real_file.png
After creating a new incident from SOURCE to TARGET successfully;

System Log on Source
find_real_file.png

HTTP Log on Source
find_real_file.png

change to below

gs.log(parameterArr.result.display_value);

Changed. Getting "Information  |  undefined" for both log attempts.