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.

Schedule is not working

sunil maddheshi
Tera Guru

Hello everyone,

I have below schedule job to cancel workflow context running for more than 2 years, but script is not working 

var w = new Workflow();
var oldContexts = new GlideRecord('wf_context');
oldContexts.addEncodedQuery("active=true^startedRELATIVELT@year@ago@2^state=executing");
oldContexts.query();
gs.print(oldContexts.getRowCount()); // here we are getting 2.4 lakh records
while (oldContexts.next()) {
w.cancelContext(oldContexts);
}

When i execute this code it runs for approx 200 records then it stopped it seems as records are not getting reduced. Please help I want to cancel all of them.

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@sunil maddheshi 

try this once

var w = new Workflow();
var oldContexts = new GlideRecord('wf_context');
oldContexts.addEncodedQuery("active=true^startedRELATIVELT@year@ago@2^state=executing");
oldContexts.query();
while (oldContexts.next()) {
    new Workflow().cancelContext(oldContexts);
}

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

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@sunil maddheshi 

try this once

var w = new Workflow();
var oldContexts = new GlideRecord('wf_context');
oldContexts.addEncodedQuery("active=true^startedRELATIVELT@year@ago@2^state=executing");
oldContexts.query();
while (oldContexts.next()) {
    new Workflow().cancelContext(oldContexts);
}

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