Built something you're proud of? Tell the story. A quick G2 review of App Engine or Build Agent helps other developers see what's possible on ServiceNow. Share your experience.

how to cancel the approver before 5 months as part of cleanup activity

SuganyaV6003655
Tera Contributor

We have cancel the request which is waiting for approval more than 5 months in production, as a clean up activity we are doing,  can we approach manually or we can do it in code the data is around 5k showing

3 REPLIES 3

vaishali231
Kilo Sage

Hey @SuganyaV6003655 

For ~5,000 records, manual cancellation is not recommended. It’s time-consuming, error-prone, and difficult to track. A scripted approach (Fix Script or Scheduled Job) is the preferred and scalable solution.

 

 Approach

Use a Fix Script (preferred for PROD) or Background Script (one-time execution) to identify and cancel requests that have been in Waiting for Approval state for more than 5 months.

 

Script:

var gr = new GlideRecord('sc_req_item'); 
gr.addQuery('state', 'requested'); // adjust based on your "Waiting for Approval" state
gr.addQuery('sys_created_on', '<=', gs.daysAgoStart(150)); // ~5 months
gr.query();
var count = 0;
while (gr.next()) {
   // Optional: double-check approval field if used
   if (gr.approval == 'requested') {
       gr.state = 'cancelled'; // adjust to correct state value
       gr.comments = 'Auto-cancelled as part of cleanup (older than 5 months)';
       gr.update();
       count++;
   }
}
gs.print('Total records cancelled: ' + count);

 

  Approvals 

If approvals exist, also cancel related records in sysapproval_approver to avoid orphaned approvals:

var appr = new GlideRecord('sysapproval_approver');
appr.addQuery('state', 'requested');
appr.addQuery('sysapproval.sys_created_on', '<=', gs.daysAgoStart(150));
appr.query();
while (appr.next()) {
   appr.state = 'cancelled';
   appr.update();
}

 

*********************************************************************************************************

If this response helps, please mark it as Accept as Solution and Helpful.

Doing so helps others in the community and encourages me to keep contributing.

Regards

Vaishali Singh

Servicenow Developer
Linkedin - https://www.linkedin.com/in/vaishali-singh-2273361bb





 

namanajain
Kilo Guru

Step 1: Identify Records (Read‑Only First)

Run a Background Script (read‑only) to count:

var cutoff = new GlideDateTime();
cutoff.addMonthsUTC(-5);

var ritm = new GlideRecord('sc_req_item');
ritm.addQuery('state', 'IN', '1,2'); // Requested / In Progress (example)
ritm.addQuery('sys_created_on', '<=', cutoff);
ritm.query();

var count = 0;
while (ritm.next()) {
    count++;
}
gs.info('RITMs eligible for cancellation: ' + count);

Step 2: Cancel via Script (Controlled Execution)

After approval, update state + approvals:

var cutoff = new GlideDateTime();
cutoff.addMonthsUTC(-5);

var ritm = new GlideRecord('sc_req_item');
ritm.addQuery('state', 'IN', '1,2'); // Waiting for approval
ritm.addQuery('sys_created_on', '<=', cutoff);
ritm.query();

while (ritm.next()) {

    // Cancel approvals
    var appr = new GlideRecord('sysapproval_approver');
    appr.addQuery('sysapproval', ritm.sys_id);
    appr.addQuery('state', 'requested');
    appr.query();

    while (appr.next()) {
        appr.state = 'cancelled';
        appr.update();
    }

    // Cancel RITM
    ritm.state = 'cancelled';
    ritm.comments = 'Auto-cancelled due to approval pending > 5 months (cleanup activity)';
    ritm.update();
}

If this response helps, please mark it as Accept as Solution and Helpful.

Ankur Bawiskar
Tera Patron

@SuganyaV6003655 

the only option is script

what script did you try in DEV and what's not working?

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