Scripted Outbound REST call

gm01
Kilo Explorer

I am trying to send a REST message without having a Outbound REST message record.   I am using #3 in this as a guide Scripting Outbound REST - ServiceNow Wiki

I have variables that need to be passed from the Request into this workflow and this is what I am doing in the Workflow

var Name = current.variables.vm_name;

var MEM = current.variables.vm_memoryMb;

var CPU = current.variables.vm_numVcpus;

ar restMessage = new sn_ws.RESTMessageV2();

restMessage.setBasicAuth("admin", "admin");

restMessage.setHttpMethod("post");

restMessage.setEndpoint("https://HostIP/gateway/services/rest/v1/vms/a8005786-07ab-4263-b065-98fc868ac6de");

restMessage.setRequestHeader("Content-Type", "application/json");

restMessage.setRequestHeader("Accept", "application/json");

restMessage.setMIDServer("midserver01");

restMessage.setRequestBody = ({"specList":[{"name":"Name","memoryMb":MEM,"numVcpus":CPU,"numCoresPerVcpu":1,"overrideNetworkConfig":true,"vmNics":[{"networkUuid":"8e06c992-7543-48f7-9380-51493b09317b"}]}]});

var response = restMessage.executeAsync();

The workflow executes with no errors but I never see anything happen on the other end and can't find where to look for the output of the script so I can see what if anything is being sent out.

Any help is appreciated.

1 ACCEPTED SOLUTION

Geoffrey2
ServiceNow Employee
ServiceNow Employee

A few things:


  • You're missing a 'v' at the start of line 4: var restMessage
  • Try stringifying the Object body
  • I think you have unwanted quotes around the Name variable in the body
  • Log the response to see what it is


var Name = String(current.variables.vm_name);


var MEM = String(current.variables.vm_memoryMb);


var CPU = String(current.variables.vm_numVcpus);


var obj = {"specList":[{"name":Name,"memoryMb":MEM,"numVcpus":CPU,"numCoresPerVcpu":1,"overrideNetworkConfig":true,"vmNics":[{"networkUuid":"8e06c992-7543-48f7-9380-51493b09317b"}]}]};


var json = JSON.stringify(obj);




var rm, response, status, responseBody, errorMsg;


try {


  rm = new sn_ws.RESTMessageV2();


  rm.setEndpoint("https://HostIP/gateway/services/rest/v1/vms/a8005786-07ab-4263-b065-98fc868ac6de");


  rm.setHttpMethod("post");


  rm.setBasicAuth("admin", "admin");


  rm.setRequestHeader("Content-Type", "application/json");


  rm.setRequestHeader("Accept", "application/json");


  rm.setMIDServer("midserver01");


  rm.setRequestBody(json);



  response = rm.execute();


  status = response.getStatusCode();


  responseBody = response.getBody();


  errorMsg = response.haveError() ? response.getErrorMessage() : '';


} catch (ex) {


  errorMsg = ex.getMessage();


  status = '500';


}




gs.log('Response\nstatus: ' + status + '\nerrorMsg: ' + errorMsg + '\nresponseBody: ' + responseBody);


View solution in original post

1 REPLY 1

Geoffrey2
ServiceNow Employee
ServiceNow Employee

A few things:


  • You're missing a 'v' at the start of line 4: var restMessage
  • Try stringifying the Object body
  • I think you have unwanted quotes around the Name variable in the body
  • Log the response to see what it is


var Name = String(current.variables.vm_name);


var MEM = String(current.variables.vm_memoryMb);


var CPU = String(current.variables.vm_numVcpus);


var obj = {"specList":[{"name":Name,"memoryMb":MEM,"numVcpus":CPU,"numCoresPerVcpu":1,"overrideNetworkConfig":true,"vmNics":[{"networkUuid":"8e06c992-7543-48f7-9380-51493b09317b"}]}]};


var json = JSON.stringify(obj);




var rm, response, status, responseBody, errorMsg;


try {


  rm = new sn_ws.RESTMessageV2();


  rm.setEndpoint("https://HostIP/gateway/services/rest/v1/vms/a8005786-07ab-4263-b065-98fc868ac6de");


  rm.setHttpMethod("post");


  rm.setBasicAuth("admin", "admin");


  rm.setRequestHeader("Content-Type", "application/json");


  rm.setRequestHeader("Accept", "application/json");


  rm.setMIDServer("midserver01");


  rm.setRequestBody(json);



  response = rm.execute();


  status = response.getStatusCode();


  responseBody = response.getBody();


  errorMsg = response.haveError() ? response.getErrorMessage() : '';


} catch (ex) {


  errorMsg = ex.getMessage();


  status = '500';


}




gs.log('Response\nstatus: ' + status + '\nerrorMsg: ' + errorMsg + '\nresponseBody: ' + responseBody);