Calling an Outgoing REST API from a Service Now workflow

The magic link between Service Now workflows and API calls are Run Scripts. In this article we will go over this so you can make those outgoing API calls you have already built in Service Now (see this article about that).

To create your call to your API do the following:

  1. Checkout your workflow
  2. Right click anywhere on the workflow and click “Add Core Activity”

3. Next take select the “Run script definition

4. Create your run script. Below is a sample of what it looks like. This is a super basic call, but you get the idea here. The magic part of this is declaring an object that is a Rest Message and setting the parameters. The rest is history.



Code Block for this example is here:

try { 
 var r = new sn_ws.RESTMessageV2('myRestOutboundCall', 'NameOfHttpMethod'); 
 r.setStringParameterNoEscape('MyVariableSubstitution', current.variables.varFromForm );
 r.setStringParameterNoEscape('MyVariableSubstitution2', current.variables.second_varFromForm );
var varExecute = r.execute();
}



catch(ex) {
 var message = ex.message;
}

workflow.info("Some Debug Message of the logs - Running REST API call:" + " Value for MyVariableSubstitution: " + current.variables.varFromForm + ", My second var MyVariableSubstitution2: " + current.variables.second_varFromForm );