Parsing a REST get message

ryanwoodburn
Kilo Expert

Hi

I am building a proof of concept of integrating into ServiceNow using REST.   To show this I am using a snowA to snowB instance integration.

As the client may have a different Incident number than our service desk, I am looking at using a mapping table to map their ticket number to a customer reference field, and coalesce on this (please advise if anyone has had a similar issue and has used a different method).

To do this, I have a REST post message set up and working in snowA, pointing towards the mapping table in snowB as below:

try {

var r = new sn_ws.RESTMessageV2('XX_RW_INSERT', 'post');

r.setStringParameter('insert.short_description', 'REST 3 test summary');

r.setStringParameter('insert.number', 'INC-REST');

//override authentication profile

//authentication type ='basic'/ 'oauth2'

//r.setAuthentication(authentication type, profile name);

var response = r.execute();

var responseBody = response.getBody();

var httpStatus = response.getStatusCode();

parsedData = JSON.parse(responseBody);

gs.log(parsedData.result.number);

}

catch(ex) {

var message = ex.getMessage();

}

This will return *** Script: INC-REST when ran as a background script (the HTTP response comes from the mapping table, and therefore number returned is the number sent).

When this record is created from the mapping table on the Incident table in snowB, Customer ref = INC-REST, and Number is autogenerated.

I want to add a GET query to my script, pointing to the Incident table on snowB, in order to return the incident number of the ticket raised in snowB.

I can get the GET request to return a 200 code, plus what appears to be the correct request body.   However, when I try to parse this as I have above, I get -   *** Script: undefined.

I have done this setting up the REST message and calling as above, and by taking the snow script from the REST API explorer and adding the parsing - both times I receive - *** Script: undefined.

I have added my script below (note at this stage I have hard coded an Inc number in).

try {

var r = new sn_ws.RESTMessageV2('XX_RW_INSERT', 'get');

r.setStringParameter('insert.number', 'INC0065984');

//override authentication profile

//authentication type ='basic'/ 'oauth2'

//r.setAuthentication(authentication type, profile name);

var response = r.execute();

  gs.log("first response" + response);

var responseBody = response.getBody();

var httpStatus = response.getStatusCode();

gs.log("http status: " + httpStatus);

var parser = new JSONParser();

var parsed = parser.parse(responseBody);

  gs.log("responseBody" + responseBody);

gs.log("Parse Value: " + parsed.number);

}

catch(ex) {

var message = ex.getMessage();

}

---------------------------------------------

My results are as follows:

*** Script: first response[object RESTResponseV2]

*** Script: http status: 200

*** Script: responseBody{"result":[{"number":"INC0065984","short_description":"REST 3 test summary"}]}

*** Script: Parse Value: undefined

--------------------------------------------

As you can see, the response body contains the correct result, I simply cannot parse this.

Any advice on the parsing, and if this is a suitable approach to mapping external tickets to internal tickets on snow, would be welcomed.

Thanks

Ryan

1 ACCEPTED SOLUTION

My recommendation would be to leave the [ ] alone and use some code similar to my example below. I use this when I get a response back from the SN Table API or Attachment API.



Script:


var parsed = JSON.parse(responseBody);


//JSUtil.logObject(parsed, "Inbound REST response parsed");


var remoteIncNumber = parsed.result[0].number



Hope this helps point you in the right direction.


View solution in original post

5 REPLIES 5

sergiu_panaite
ServiceNow Employee
ServiceNow Employee

Thanks, I will check this out.



I believe the issue is the response body from the get is returning with []


*** Script: {"result":[{"number":"INC0065981","short_description":"Test-2"}]}



Whereas the response body that can successfully be parsed returns as:


*** Script: {"result":{"number":"INC0065981","short_description":"Test-2"}}



I can get the get response to parse if I remove []



I do this within the script as follows (I only want to remove first [ and last ])


var str = responseBody.replace('[', '');


str = str.slice(0, -2) + '}';



Has anyone any suggestions as to how to return the get response in the correct format?   The message response is not set up any differently to the post response..



Thanks


Ryan


My recommendation would be to leave the [ ] alone and use some code similar to my example below. I use this when I get a response back from the SN Table API or Attachment API.



Script:


var parsed = JSON.parse(responseBody);


//JSUtil.logObject(parsed, "Inbound REST response parsed");


var remoteIncNumber = parsed.result[0].number



Hope this helps point you in the right direction.


I am using this logic in virtual agent scripted input and script output. please guide me how to divide the code in scripted input and output section (these are bot responses in palette on left side in VA). Also, How to display this parsed result in virtual agent (which option to use from the palette menu)?