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  - 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();
    }
  }
}
   
 

@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