- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-14-2024 10:46 PM
Hi!
I am trying to run a remote action using ServiceNow Virtual Agent which will return the device health from a third party tool called Nexthink. I have created the required REST Message and HTTP method to invoke the API call. I have also created a topic in Virtual Agent Designer which contains a script to call the REST message and return the response. I will attach the code below. I am able to get correct response through the REST message, but I am not able to parse the response or output through Virtual Agent. Any help would be appreciated.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-15-2024 11:47 PM
Please try replacing
vaVars.responseBody
with
var responseBody
in both the codes and retry.
Please mark this response as correct and helpful if it assisted you with your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-15-2024 02:48 AM
I can see that you are simply returning the response body entirely instead of fetching the device name from the response. After parsing the JSON response, you can dot walk to device.name key under data key to get the device name and return it.
Can you please check on this ?
Thanks and Regards
Amit Verma
Please mark this response as correct and helpful if it assisted you with your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-15-2024 06:40 AM
Hi @Amit Verma. I tried returning the device name using dot walking but it is giving error as: " responseBody" is not defined. I think I might not be doing it right. I am attaching the updated code below as well as the dictionary response of the correct output, can you please help me with it? Thanks in advanced.
(function execute() {
try{
var r = new sn_ws.RESTMessageV2('Nexthink_Diagnostic_System', 'Nexthink_Diagnostic_POST_RA');
var response = r.execute();
vaVars.responseBody = response.getBody();
var health_data = JSON.parse(responseBody);
return health_data.body.data[3];
//
// return responseBody;
//var health_data = JSON.parse(responseBody);
//return health_data.data;
}
catch(ex){
var errorMessage = 'Error calling API' + ex.message;
gs.error(errorMessage);
return errorMessage;
}
})()
PS: I have also tried to use: return health_data.body.data[0].diskfreepercentage in line number 11.
Adding Response from HTTP method containing data dictionary.
{
"queryId": "#diagnose_device_health",
"executedQuery": "devices| where device.name == \"CPC-b**an-P5V34\"| include device_performance.events during past 168h| compute free_space = system_drive_free_space.avg() / 1000000000, used_memory_ = event.used_memory.avg(), used_memory_percentage= event.used_memory.avg()*100/device.hardware.memory.avg()| include volumes| compute diskfreepercentage = (1 - usage.sum())*100| list collector.uid, device.name, remote_action.get_startup_impact_windows.execution.outputs.HighImpactCount, remote_action.get_battery_status.execution.outputs.BatteryHealth, diskfreepercentage, used_memory_percentage",
"rows": 1,
"executionDateTime": "2024-05-10T06:46:55",
"data": [
{
"device.collector.uid": "c959666d-2471-421c-8977-595285bac1bb",
"remote_action.get_startup_impact_windows.execution.outputs.HighImpactCount": null,
"used_memory_percentage": 50.52209335186128,
"device.name": "CPC-brian-P5V34",
"remote_action.get_battery_status.execution.outputs.BatteryHealth": null,
"diskfreepercentage": 70.715535
}
]
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-15-2024 09:44 PM
Please try with below code :
(function execute() {
try{
var r = new sn_ws.RESTMessageV2('Nexthink_Diagnostic_System', 'Nexthink_Diagnostic_POST_RA');
var response = r.execute();
vaVars.responseBody = response.getBody();
var health_data = JSON.parse(responseBody);
var deviceName = health_data .data[0]['device.name'] ;
return deviceName;
}
catch(ex){
var errorMessage = 'Error calling API' + ex.message;
gs.error(errorMessage);
return errorMessage;
}
})()
If this doesn't work, try with the below code :
(function execute() {
try{
var r = new sn_ws.RESTMessageV2('Nexthink_Diagnostic_System', 'Nexthink_Diagnostic_POST_RA');
var response = r.execute();
vaVars.responseBody = response.getBody();
var deviceName = responseBody .data[0]['device.name'] ;
return deviceName;
}
catch(ex){
var errorMessage = 'Error calling API' + ex.message;
gs.error(errorMessage);
return errorMessage;
}
})()
Please mark this response as correct and helpful if it assisted you with your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-15-2024 11:32 PM