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

Hello @Manju Shree 

 

have you declared input in inputs section of the workflow? if not, then create a new input record in your workflow as shown below:

AhmmedAli_0-1666845630513.png

 

Once it is done, try below script in scheduled job:

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.NAME_OF_INPUT_DEFINED = grp; //replace name of input defined, ex:u_group in above pic
var context = w.startFlow('c07c51d11b3a5d10321f9759b04bcb1d', temp, 'update', vars);
}
}

triggerapproval();

 

 

 

and in run script activity of workflow: 

gs.log('Inside Wf');
var groupval = workflow.inputs.NAME_OF_INPUT_DEFINED; //replace name of input defined, ex:u_group in above pic
gs.log(groupval+ ' grp value check'); 

 

Thanks,

Ali

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

Thank you,
Ali