VMware Cloud Community
Jamison618
Contributor
Contributor
Jump to solution

vRO ServiceNow (snow) REST API Code

Hello,

I will preface this with the fact that I'm not a JavaScript code guy, I usually manage to muddle my way around things.  Smiley Happy

I am creating a workflow to Add or Update ServiceNow CMDB records when we deploy ESXi hosts or VMs.  As I get started in our developer instance, I'm able to get the content but when I convert it using JSON.parse all of the content appears "undefined".  I've done this before without problems on simpler API calls but the snow records are a huge table so I might not be doing this correctly.  Here is the code and the log results.  As you can see, I'm successfully grabbing the data (note the urlTemplate is static for testing purposes only).

var urlTemplate = "/api/now/table/cmdb_ci_esx_server?sysparm_exclude_reference_link=True&sysparm_fields=name%2Cinstall_status%2Cserial_number&name=pxesx723"

var request = snowRestHost.createRequest("GET", urlTemplate, null);

var contentType = "application/json"

request.contentType = contentType

System.log("Request Content Type: " + request.contentType);

var response = request.execute();

System.log("STRING: " + response.contentAsString);

var responseInJson = JSON.parse(response.contentAsString);

System.log("Result should be Object: " + responseInJson); //confirm it is object

System.log("Headers: " + responseInJson.getAllHeaders);

System.log("Content Length: " + responseInJson.contentLength);

System.log("statusCode: " + responseInJson.statusCode);

System.log("JSON Name Field: " + responseInJson.name);

System.log("JSON Name Field: " + responseInJson.result.name);

var responseString = JSON.stringify(responseInJson);

System.log("Back to string: " + responseString);

Notice that when I try to get headers in the JSON object everything is "undefined".  I also see that it appears to be nested.  Where am I going wrong here?

[2019-10-23 09:05:59.097] [I] Request Content Type: application/json

[2019-10-23 09:05:59.425] [I] STRING: {"result":[{"install_status":"1","name":"pxesx723","serial_number":"USE4338MN9"}]}

[2019-10-23 09:05:59.431] [I] Result should be Object: [object Object]

[2019-10-23 09:05:59.436] [I] Headers: undefined

[2019-10-23 09:05:59.438] [I] Content Length: undefined

[2019-10-23 09:05:59.441] [I] statusCode: undefined

[2019-10-23 09:05:59.443] [I] JSON Name Field: undefined

[2019-10-23 09:05:59.445] [I] JSON Name Field: undefined

[2019-10-23 09:05:59.448] [I] Back to string: {"result":[{"install_status":"1","name":"pxesx723","serial_number":"USE4338MN9"}]}

Also, not know REST all that well, is there a difference between using a URL vs. a URI in the code above?  Here is one of the URI instances sent to me:

$uriTemplate = "/api/now/identifyreconcile?sysparm_data_source=citrix"

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
iiliev
VMware Employee
VMware Employee
Jump to solution

Hi,

If you check the content string, you'll see that the result entry is not a plain object but an array. So you should access it's elements by index.

For example, the line 16 instead of

System.log("JSON Name Field: " + responseInJson.result.name); 

should be:

System.log("JSON Name Field: " + responseInJson.result[0].name);

The other attributes like statusCode should be accessed from response variable, not from responseInJson. responseInJson is just the response body, not the enire response including status, headers, etc.

View solution in original post

0 Kudos
1 Reply
iiliev
VMware Employee
VMware Employee
Jump to solution

Hi,

If you check the content string, you'll see that the result entry is not a plain object but an array. So you should access it's elements by index.

For example, the line 16 instead of

System.log("JSON Name Field: " + responseInJson.result.name); 

should be:

System.log("JSON Name Field: " + responseInJson.result[0].name);

The other attributes like statusCode should be accessed from response variable, not from responseInJson. responseInJson is just the response body, not the enire response including status, headers, etc.

0 Kudos