← Script library

Background Scripts

Report Aging Requested Approvals

List up to 100 requested approval records created more than seven days ago for manual follow-up, without changing approvals or sending notifications.

JavaScript
(function reportAgingApprovals() {
  var prefix = '[SN-Tricks:AgingApprovals]';
  var ageDays = 7;
  var limit = 100;
  try {
    var cutoff = new GlideDateTime();
    cutoff.addDaysUTC(-ageDays);
    var approvals = new GlideRecord('sysapproval_approver');
    approvals.addQuery('state', 'requested');
    approvals.addQuery('sys_created_on', '<', cutoff.getValue());
    approvals.orderBy('sys_created_on');
    approvals.orderBy('sys_id');
    approvals.setLimit(limit + 1);
    approvals.query();

    var shown = 0;
    var truncated = false;
    while (approvals.next()) {
      if (shown >= limit) {
        truncated = true;
        break;
      }
      gs.info(prefix + ' approval=' + approvals.getUniqueValue() +
        ' | created_utc=' + approvals.getValue('sys_created_on') +
        ' | approver=' + (approvals.getValue('approver') || '(none)') +
        ' | task=' + (approvals.getValue('sysapproval') || '(none)') +
        ' | document=' + (approvals.getValue('document_id') || '(none)') +
        ' | source_table=' + (approvals.getValue('source_table') || '(none)'));
      shown++;
    }
    gs.info(prefix + ' Requested approvals created before ' + cutoff.getValue() +
      ' UTC: reported ' + shown + '.' +
      (truncated ? ' More matches exist; output capped at ' + limit + '.' : ' All matching approvals shown.'));
  } catch (error) {
    gs.error(prefix + ' Report failed; disregard partial output: ' + error);
  }
})();

How to use it

1. As an authorized administrator, open System Definition > Scripts - Background in a non-production instance and select Global scope. Paste and run this read-only diagnostic. Default ageDays is 7; keep it a positive whole number. limit controls printed rows, not database scan cost. 2. Find [SN-Tricks:AgingApprovals] in the output or system log. The report selects state=requested and sys_created_on strictly before the execution-time UTC cutoff, oldest first. It prints internal approval, approver, task, and document identifiers without fetching target records or logging names/descriptions. 3. Test requested approval records older than seven days (included), newer ones and exactly-at-cutoff records (excluded), and old approved/rejected/not-yet-requested records (excluded). Missing approver or target fields print (none). Task approvals may use sysapproval; non-task approvals may use document_id and source_table. These raw references are not resolved or validated. 4. Check no matches, exactly 100 matches, and 101 matches. Only the last case should report truncation. Compare against a sysapproval_approver list using State is Requested and Created before the exact UTC cutoff logged by the script, with the same execution/domain context. List date displays may use your local timezone. 5. This measures record creation age, NOT time since entering Requested, an approval due date, SLA breach, business days, or delegate availability. Re-requested approvals retain their original creation timestamp and can appear immediately. Review approval history before taking action; the script never approves, rejects, cancels, escalates, or sends reminders. 6. GlideRecord in this administrative context is not an ACL-filtered end-user report. Domain separation, query business rules, and concurrent updates affect results. Test query runtime on representative data: the 101-row limit does not guarantee a cheap database query. Restrict internal identifiers to authorized staff. No cross-scope access is required for this Global example. 7. If an error is logged, disregard partial output. There are no inserts, updates, or deletes and no rollback is needed. Pasting into Scripts - Background creates no reusable configuration to promote; if later saved as a scheduled job, review scheduling, access, and update-set capture separately.

Adapt the table names, fields, and conditions to your instance. Test the behavior in a development environment before using it in production.