How To call workflow from scheduled job

Manju Shree
Tera Expert

Hi Team,

 

I have a requirement to call a workflow from a scheduled job and wanted to pass a group sys id to the workflow. Below is my script.

 

function triggerapproval() {
var temp = new GlideRecord('std_change_record_producer');
temp.addEncodedQuery('active=true');
temp.addEncodedQuery('u_number=123');
temp.query();
while (temp.next()) {

var grp = '<sys_id>';
var w = new Workflow();
var vars = {};
var context = w.startFlow('b85c9d111b3a5d10321f9759b04bcb79', temp, 'check template', null);
}
}

 

In the code, b85c9d111b3a5d10321f9759b04bcb79 is my workflow sys_id, temp is the object, check template is my workflow name. 

The above code is not helping me to reach the workflow. Can anyone guide what is wrong and also how to pass the variable grp in the workflow calling.

 

Any help on this would be much appreciated.

 

Regards,

Manjushree

5 REPLIES 5

Ahmmed Ali
Mega Sage

Hello,

 

The third argument in the startFlow() function is operation (refer: Workflow | ServiceNow Developers).

Try:

function triggerapproval() {
var temp = new GlideRecord('std_change_record_producer');
temp.addEncodedQuery('active=true');
temp.addEncodedQuery('u_number=123');
temp.query();
while (temp.next()) {

var grp = '<sys_id>';
var w = new Workflow();
var vars = {};
var context = w.startFlow('b85c9d111b3a5d10321f9759b04bcb79', temp, 'update', {}); //The sys_id should be from table wf_workflow.
}
}

triggerapproval();

 

 

Thank you,

Ali

If I could help you with your Query then, please hit the Thumb Icon and mark my answer as Correct!!

Thank you,
Ali

Thank you Ali for the reply. I'm able to reach the workflow. 

 

Also can you let me know how to access the value of the variable 'grp' from scheduled job in workflow

Hello Manju Shree,

 

You can pass group sys_id as below:

 

function triggerapproval() {
var temp = new GlideRecord('std_change_record_producer');
temp.addEncodedQuery('active=true');
temp.addEncodedQuery('u_number=123');
temp.query();
while (temp.next()) {

var grp = '<sys_id>';
var w = new Workflow();
var vars = [];

vars["INPUT_NAME_DEFINED_IN_WORKFLOW"] = grp;
var context = w.startFlow('b85c9d111b3a5d10321f9759b04bcb79', temp, 'update', vars); //The sys_id should be from table wf_workflow.
}
}

triggerapproval();

 

Thank you,

Ali

If I could help you with your Query then, please hit the Thumb Icon and mark my answer as Correct!!

Thank you,
Ali

Hi Ali,

 

I have modified my code to below

 

function triggerapproval() {
var temp = new GlideRecord('std_change_record_producer');
temp.addEncodedQuery('active=true');
temp.addEncodedQuery('u_number=SCTMPL0001107');
temp.query();
while (temp.next()) {
var grp = '123';
var w = new Workflow();
var vars = [];
vars["groupval"] = grp;
var context = w.startFlow('c07c51d11b3a5d10321f9759b04bcb1d', temp, 'update', vars);
}
}

triggerapproval();

 

In workflow I have declared as below

 

gs.log('Inside Wf');
var groupval;
gs.log(groupval+ ' grp value check'); // value in groupval is undefined

 

Could you please help with this.