Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Cancel Workflow from one RITM if another RITM is found

Luiz Lucena
Mega Sage

Hello friends, 

Having a situation here, we have a catalog item to convert employee to contractor and another catalog item to terminate user.
So, let's say if a user is being converted to contractor, the workflow will check the active Requested Items, query for Termination request for the same user being converted, if found a record, will need to cancel the termination request.

The current script in the this workflow was canceling ALL termination requests (not good!), I manage to fix that by adding the Encoded Query below, while leaving part of the old query commented as we will have internal discussions about. it.

var gr = new GlideRecord("sc_req_item");
gr.addEncodedQuery('cat_item=045a343bdb0caf00d299dd0b5e9619b5^active=true^variables.82a385ffdb0caf00d299dd0b5e961951=' + current.variables.u_requested_by.sys_id);
//gr.addQuery("current.variables.u_requested_by", current.variables.u_requested_by);
gr.query();
while (gr.next()) {
    if (gr.state == 1 || gr.state == 2 || gr.state == -5) {
        gr.state = 3;
		gr.stage = 'closed_incomplete';
        gr.work_notes = 'Closed by ATC - Employee to Contractor - Cancel any pending termination request';
        gr.update();
    }

 However, the workflow associated with the termination request was not cancelled, how can I cancel the workflow if the query above returns true?

1 ACCEPTED SOLUTION

SAI VENKATESH
Kilo Patron
Kilo Patron

 Hi @Luiz Lucena 

 

You can try by adding  the below two lines :

 

 // Cancel  workflow after gr.update()
        var wf = new Workflow();
        wf.cancel(gr);

 

Thanks and regards

Sai Venkatesh

 

View solution in original post

3 REPLIES 3

SAI VENKATESH
Kilo Patron
Kilo Patron

 Hi @Luiz Lucena 

 

You can try by adding  the below two lines :

 

 // Cancel  workflow after gr.update()
        var wf = new Workflow();
        wf.cancel(gr);

 

Thanks and regards

Sai Venkatesh

 

Thanks, Sai, that worked and showed me how dumb I am... 😀
I saw similar example in the documentation, but was passing "current" in the parenthesis!