How to get data from an HTTP Response from a REST Message
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-11-2023 05:47 PM - edited 06-11-2023 05:56 PM
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.
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.