- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-27-2025 11:30 PM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-28-2025 07:37 AM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-28-2025 07:17 AM
@sunil maddheshi Please see if this helps https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0960722
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-28-2025 07:37 AM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader