How to cancel a pending workflows of a particular work flow through the schedule job ?

ads
Tera Expert

Hi All,

I want to cancel the pending workflows of one particular workflow, if the number exceeds the amount of 1000 pending workflows, then it will cancel from the oldest one from the schedule job.

 

Can anyone please help me out on this ?

 

Thanks.

 

2 ACCEPTED SOLUTIONS

@Ankur Bawiskar  - Hi, I tested the below script, It worked. Thanks for your support.

var wfCount = new GlideAggregate('wf_context');
wfCount.addQuery('active', true);
wfCount.addQuery('workflow','85028aa71be60510f18ba8ade54bcb5e');
wfCount.addAggregate('COUNT');
wfCount.query();
if (wfCount.next()) {
  var totalCount = parseInt(wfCount.getAggregate('COUNT'));
  if (totalCount > 60) {
     var wfCancel = new GlideRecord('wf_context');
     wfCancel.addQuery('active', true);
    wfCancel.orderBy('sys_created_on');
    wfCancel.setLimit(totalCount - 60);
    wfCancel.query();
    while (wfCancel.next()) {
   
      wfCancel.state = 'cancelled';
      wfCancel.update();
    }
  }
}
   
 

View solution in original post

@ads 

Glad to know.

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

6 REPLIES 6

Ankur Bawiskar
Tera Patron
Tera Patron

@ads 

didn't get your requirement?

cancel the pending workflows of 1 particular workflow?

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

@Ankur Bawiskar - Hi, I am having one workflow named "xyz", I want to cancel all the pending workflows triggered for the same workflow. But it will only cancel when the total record number is more than 1000 and it will cancel from the oldest workflow triggered.

 

If its not clear, Please let me know.

@ads 

got it

then query workflow context table and cancel it in ascending order by using orderBy on sys_created_on

Sample script to cancel workflow

var gr = new GlideRecord('incident');
gr.get('sys_id');
var workflow = new Workflow();
workflow.cancel(gr); 
Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Ankur Bawiskar  - Thanks, How to check the total count is more than from 1000, I got the total count by getRowCount(). I have tried the below,

var wf = new GlideRecord('wf_context');
wf.addQuery('active', true);
wf.addQuery('workflow', '85028aa71be60510f18ba8ade54bcb5e')
wf.query();
while(wf.next()){
    gs.print(wf.getRowCount());
   wf.state = 'cancelled'
   wf.update();
}