geoffc
geoffc

Reputation: 4100

Can I start a Service Now workflow via an external SOAP call?

I would like to make a call into the ServiceNow SOAP webservice to start an instance of a specific web service.

I can find the WSDL for functions like incident.do but seem to be missing the step needed to find the proper table/endpoint for workflows to start.

Upvotes: 1

Views: 816

Answers (2)

Daniel C. Oderbolz
Daniel C. Oderbolz

Reputation: 89

The tricky bit is getting the variables into the Workflow. While this sounds easy, in fact it isn't.

If your workflow runs on the table sc_req_item (which is likely if you are dealing with Request Fulfillment), you first need to set the Property (sys_properties) glide.workflow.enable_input_variables to true, because otherwise, you will not be able to add normal Input variables to your workflow.

Then, add the Input variables to the workflow. Note that you have some nifty datatypes available there. Note for example the "Data Structure" type. All Input variables are treated like custome columns (in fact they are columns of a workflw-specific table). That is why the names start with u_.

Lets say, you define an input variable called u_dynamic_vars (Datatype "Data Structure").

Here is how to call the workflow:

var wf_name = "Name of your workflow";

// Instantiate JSON machinery
var parser = new JSON();

//Declare an instance  of workflow.js 
var wf  = new Workflow ();
//Get the workflow id 
var  wfId  = wf.getWorkflowFromName (wf_name) ;

//Start workflow, passing along object containing name/value pairs mapping to inputs expected by the workflow 
var vars  = { } ;

// Prepare the JSON Datastructure
var obj ={"name":"George",
      "lastname":"Washington"};

// Encode the data
vars.u_dynamic_vars = parser.encode(obj);
vars.u_new_email = "[email protected]";


// Get a specific RITM
var gr = GlideRecord("sc_req_item");
gr.get("18d8e9740f4013002f504c6be1050e48");
gs.print(gr.number);

// Start the Workflow with a "current" record
wf.startFlow(wfId , gr , "update" , vars ) ;

// You may also pass null, then current is null.
wf.startFlow(wfId , null , "update" , vars ) ;

In the workflow, you then unpack the data like so:

// Let's unpack it. For some reason, intantiating the parse won't work here...
payload = JSON.parse(workflow.variables.u_dynamic_vars);
gs.print("payload.first_name:" + payload.name);

Also note that a workflow does not necessarily need to run on a table. To achieve this, choose "global" as table name when defining the workflow.

Upvotes: 2

makim
makim

Reputation: 3284

If you want to start a Workflow via SOAP I think the only way to do this is to create a Scripted Web-Service or a Custom Processor.

In there you will have to define a script which starts your Workflow.

var w = new Workflow();
var context = w.startFlow(id, current, current.operation(), getVars());

In this wiki article you can find API Methods for Workflows.

Upvotes: 2

Related Questions