diff --git a/Server-Side Components/Background Scripts/Delete Attachments - Closed Approvals/readme.md b/Server-Side Components/Background Scripts/Delete Attachments - Closed Approvals/readme.md new file mode 100644 index 0000000000..d7b047a94f --- /dev/null +++ b/Server-Side Components/Background Scripts/Delete Attachments - Closed Approvals/readme.md @@ -0,0 +1,2 @@ +Many organizations still prefer copying over the attachments from RITM or any ticket type that has approval required to its corresponding approvals by using GlideSysAttachment.copy() +This ideally makes sense till the time approval is needed however, when approvals are actioned (approved/rejected) there is no need for attachments to stay on the approval record till eternity (it will be stored at its ticket level). Use this script as a scheduled script to remove attachments from closed (approved/rejected) approval records and help consume less storage. diff --git a/Server-Side Components/Background Scripts/Delete Attachments - Closed Approvals/script.js b/Server-Side Components/Background Scripts/Delete Attachments - Closed Approvals/script.js new file mode 100644 index 0000000000..0519a52d09 --- /dev/null +++ b/Server-Side Components/Background Scripts/Delete Attachments - Closed Approvals/script.js @@ -0,0 +1,41 @@ +deleteinactiveattachments(); + +//deletes from approval table +function deleteinactiveattachments() { + var appr = new GlideRecord('sysapproval_approver'); +// appr.addQuery('sys_id','2d7f326287351ad0195eb99c8bbb35b5'); //uncomment this and replace sys_id and use this to check for 1 record + appr.addEncodedQuery('state!=requested^ORstate=NULL'); + appr.query(); + while (appr.next()) { + var answer = deleteparentattachment(appr.sys_id); + deletechildattachment(appr.sys_id); + if (answer == true || answer == 'true') { + appr.comments = "Attachment's were removed from this record. If still needed it can be located at the corresponding ticket level."; + appr.autoSysFields(false); + appr.setWorkflow(false); + appr.update(); + } + } +} +//deletes from sys_attachment table +function deleteparentattachment(record) { + var attach_found = false; + var attach_primary = new GlideRecord('sys_attachment'); + attach_primary.addQuery('table_sys_id', record); + attach_primary.query(); + while (attach_primary.next()) { + attach_primary.deleteRecord(); + attach_found = 'true'; + } + return attach_found; +} + +//deletes from sys_attachment_doc table +function deletechildattachment(record) { + var attach_child = new GlideRecord('sys_attachment_doc'); + attach_child.addQuery('sys_attachment.table_sys_id', record); + attach_child.query(); + while (attach_child.next()) { + attach_child.deleteRecord(); + } +}