Can the workflow context via run script

MR1
Tera Contributor

Hi All,

I need help to cancel the workflow on the workflow context(wf_context) table when variable set (variable) is set to true on the catalog item.

var gr = new GlideRecord ('sc_req_item');
gr.addQuery('sys_id', current.sysapproval.sys_id);
gr.query();
if (gr.next()){
if (gr.variables.ritm.toString() == 'true'){
var flow = new workflow().getRunningFlows(gr);
while (flows.next()){
new Workflow().cancelContext(flows);
}
}
}
1 ACCEPTED SOLUTION

Hi,

Have you tried to use any log statements to see what is going on? Else you're just coding in the dark and saying your script doesn't work, but you won't know why.

I can only comment on the script itself, but the values and field names is up to you to verify as appropriate.

You can try a script like this:

if (current.variables.ritm.toString() == 'true'){
var wgr = new GlideRecord("wf_context");
wgr.addQuery('id', current.sys_id);
wgr.query();
while(wgr.next()){
new Workflow().cancelContext(wgr);
 }
}

You'd use this in a Run Script activity within your workflow. So simply draw a line to this as part of your flow. This will check that variable value and if true for that specific variable, all workflow contexts will be cancelled.

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

View solution in original post

9 REPLIES 9

The workflow is on sysapproval_approver table and trigger via conditions, when RITM is pending for approval

The above script is in the workflow(run script), I would like to add it in the Advance script of "If" object.

'Yes' would execute the workflow flow

'No' would cancel the workflow

Thanks

Hi,

update as this

if (current.variables.ritm.toString() == 'true'){
	var flow = new workflow().getRunningFlows(current);
	while (flows.next()){
		new Workflow().cancelContext(flows);
	}
}

Regards
Ankur

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

Thanks for the quick response!

This script should be in the advance script of "if" object workflow?

Yes should pass the workflow, No would cancel the workflow. 

I tried as the advance script of "if" condition but unfortunately it didn't work.

Hi,

your workflow is on catalog item i.e. RITM then it should work fine

did you check variable name is correct?

what's the if script you are using?

Regards
Ankur

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

Thanks!