ServiceNow Virtual Agent API Integration

shishirsapkal
Tera Contributor

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. 

 

1 ACCEPTED SOLUTION

@shishirsapkal 

 

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.

View solution in original post

13 REPLIES 13

Amit Verma
Kilo Patron
Kilo Patron

Hi @shishirsapkal 

 

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.

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

        }

    ]

}

 

Amit Verma
Kilo Patron
Kilo Patron

@shishirsapkal 

 

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.

Hi Amit! I tried to use both your codes but I am getting responseBody is not defined. Attaching the screenshot of error below. Getting same error in both codes.