Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to get data from an HTTP Response from a REST Message

DevSalvo
Tera Contributor

Hello Everyone, 


TL;DR >  I need to extract a specific value of a REST Message RESPONSE and assign it to a record field to fix our CMDB.

Full version:

I am currently integrating a tool and I have a working REST Message, below this is the response I get from my message. You can see the JSON query in CONTENT and the Response being brought. For the purposes of this integration I only need the Highlighted part in the Response. I need this so I can update our CMDB with that missing field that does not come with the OOB solution. But I have found no way to properly extract that bit.

DevSalvo_0-1686530469076.png

This is the latest iteration of my Code in a Business Rule to try and get the "userName" value in the Response to the Assigned_to of the form view. ( I also tried the short_description just to see if it brought anything but no results so far):

Please bear in mind I am trying also to replace the hardcoded mac address in the PostBody with a variable. The hardcoded part is just there for testing. 

 

(function executeRule(current, previous /*null when async*/ ) {
 
 
    var r = new sn_ws.RESTMessageV2('Lansweeper client V2', 'Assigned To POST');
    var macAddress = current.mac_address;
    var PostBody = {
        "query": "query getAssetResources { site(id: \"7560397a-a752-46fa-bce1-628fc6fa5f94\") { assetResources( fields: [\"assetBasicInfo.name\", \"assetBasicInfo.userName\", \"assetCustom.manufacturer\", \"url\"], filters: { conjunction: AND, conditions: [ { operator: LIKE, path: \"assetBasicInfo.mac\", value: \"3c:52:82:7b:4a:17\" } ] } ) { total pagination { limit current next page } items } } }"
    };
    r.setRequestBody(JSON.stringify(PostBody));
              r.execute();
    var response = r.execute();
    var responseBody = response.getBody();
    var httpStatus = response.getStatusCode();
 
 
    var parsedData = JSON.parse(responseBody);
    var userName = parsedResponse.data.site.assetResources.items[0].assetBasicInfo.userName;
 
    current.assigned_to = userName;
 
 
 
})(current, previous);

 



Any guidance is appreciated. 

Thank you in advanced for your valuable time and kind support. 

0 REPLIES 0