Interested in a ServiceNow event built for developers? Registration for now[dev]26 is officially open!

Automatically Delete HR Case Attachments 10 Days After Closed Complete Status

adityaashok
Tera Contributor

Hi Team,

I have a requirement for HR Cases (sn_hr_core_case) where attachments need to be automatically removed after a retention period.

Requirement

When an HR Case reaches "Closed Complete" status:

  1. Attachments should remain available for 10 calendar days after the case is closed.
  2. On the 11th day, all attachments associated with the HR Case should be automatically deleted.
  3. The HR Case record itself must remain unchanged.
  4. Deletion activity should be logged for audit purposes.
  5. The process should run automatically without manual intervention.
  6. Any errors during attachment deletion should be captured in system logs and ideally reported to administrators.

Current Approach

I am attempting to use a Scheduled Script Execution to identify HR Cases that have been in the Closed Complete state for more than 10 days and then delete the associated records from the sys_attachment table.

However, I am looking for guidance on:

  • Whether a Scheduled Script Execution is the best practice for this use case.
  • Whether there is a more efficient approach using Scheduled Jobs
  • Best practices for logging and auditing attachment deletions.
  • Any considerations specific to HRSD regarding attachment retention and deletion.
  • For more information I shared the scheduled job script.
    gs.info('HR Attachment Deletion Job Started');
    var cutoffDate = new GlideDateTime();
    cutoffDate.addSecondsUTC(-600); // 10 minute
    var hrCase = new GlideRecord('sn_hr_core_case');
    hrCase.addQuery('state', '3');
    hrCase.addQuery('closed_at', '<=', cutoffDate);
    hrCase.query();
    gs.info('Cases Found = ' + hrCase.getRowCount());
    while (hrCase.next()) {
        gs.info('Case Found = ' + hrCase.number);
        var attachGR = new GlideRecord('sys_attachment');
        attachGR.addQuery('table_name', 'sn_hr_core_case');
        attachGR.addQuery('table_sys_id', hrCase.getUniqueValue());
        attachGR.query();
        gs.info('Attachments Found = ' + attachGR.getRowCount());
        while (attachGR.next()) {
            gs.info('Attachment Found = ' + attachGR.file_name);
             new GlideSysAttachment().deleteAttachment(attachGR.getUniqueValue());
        }
    }

Has anyone implemented a similar requirement for HR Cases? Any recommendations, sample scripts, or best practices would be greatly appreciated.

Thanks in advance!

5 REPLIES 5

@adityaashok 

any reason why you want to use scheduled job and not flow?

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