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

Allen Andreas
Administrator
Administrator

Hi,

Can you give a bit more information as far as when is this script being ran? Where?

And for what you have with that code: what is working? isn't working? what errors are you getting, if applicable? What do your troubleshooting log messages say, etc.?

Please mark reply as Helpful, if applicable. Thanks!


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

Hi,

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. The above script isn't working as expected.

'Yes' would execute the workflow flow

'No' would cancel the workflow

Thanks

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!

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

where have you written the above script?

Regards
Ankur

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