Automatically Delete HR Case Attachments 10 Days After Closed Complete Status
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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:
- Attachments should remain available for 10 calendar days after the case is closed.
- On the 11th day, all attachments associated with the HR Case should be automatically deleted.
- The HR Case record itself must remain unchanged.
- Deletion activity should be logged for audit purposes.
- The process should run automatically without manual intervention.
- 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 minutevar 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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
any reason why you want to use scheduled job and not flow?
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader
