need to trigger the workflow through scheduled script

saikumarkak
Tera Contributor
Hello,
var workflow = new Workflow();
var contextId = workflow.startFlow('87ca7031834822104323fdc6feaad33a', null, null);

if (contextId) {
    gs.info("Workflow started successfully. Context ID: " + contextId);
} else {
    gs.error("Workflow failed to start.");
}

can anyone make it correct way
4 REPLIES 4

Brad Bowman
Kilo Patron
Kilo Patron

The startFlow method of the Workflow API has four arguments, so try adding another

https://www.servicenow.com/docs/bundle/yokohama-api-reference/page/app-store/dev_portal/API_referenc... 

 

Also ensure the sys_id you are providing is that of a record, such as a Problem, RITM... which has a workflow associated to it.

Anurag Tripathi
Mega Patron
Mega Patron

Hi,

In StartFlow function you second argument needs to have the record this flow is for. Pass the right arguments and this will run.

AnuragTripathi_0-1741004325904.png

 

-Anurag

Ankur Bawiskar
Tera Patron
Tera Patron

@saikumarkak 

Please check the documentation and you will find many examples on how to use it.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

sunil maddheshi
Tera Guru

@saikumarkak  Below is the correct syntax

Workflow - startFlow(String workflowId, GlideRecord current, String operation, Array vars)

Starts a specified workflow.

 
ParametersName Type Description
workflowIdStringThe sys_id of the workflow to start. This sys_id refers to table wf_workflow.
currentGlideRecordThe record to use as current in this workflow. This is normally from the Table field of the workflow properties for this workflow.
operationStringThe operation to perform on current. Possible values: insert, update, delete.
varsArrayCollection of variables to add to the workflow

Example

////where current is a task record with a workflow context
      var w = new Workflow();
      var context = w.startFlow(id, current, current.operation(), getVars());
 
Please mark correct/helpful if this helps you!