Skip to content

Commit dc23375

Browse files
Delete Attachments - Closed Approvals (#1770)
* script.js * readme.md
1 parent fb13cf2 commit dc23375

File tree

2 files changed

+43
-0
lines changed
  • Server-Side Components/Background Scripts/Delete Attachments - Closed Approvals

2 files changed

+43
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
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()
2+
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.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
deleteinactiveattachments();
2+
3+
//deletes from approval table
4+
function deleteinactiveattachments() {
5+
var appr = new GlideRecord('sysapproval_approver');
6+
// appr.addQuery('sys_id','2d7f326287351ad0195eb99c8bbb35b5'); //uncomment this and replace sys_id and use this to check for 1 record
7+
appr.addEncodedQuery('state!=requested^ORstate=NULL');
8+
appr.query();
9+
while (appr.next()) {
10+
var answer = deleteparentattachment(appr.sys_id);
11+
deletechildattachment(appr.sys_id);
12+
if (answer == true || answer == 'true') {
13+
appr.comments = "Attachment's were removed from this record. If still needed it can be located at the corresponding ticket level.";
14+
appr.autoSysFields(false);
15+
appr.setWorkflow(false);
16+
appr.update();
17+
}
18+
}
19+
}
20+
//deletes from sys_attachment table
21+
function deleteparentattachment(record) {
22+
var attach_found = false;
23+
var attach_primary = new GlideRecord('sys_attachment');
24+
attach_primary.addQuery('table_sys_id', record);
25+
attach_primary.query();
26+
while (attach_primary.next()) {
27+
attach_primary.deleteRecord();
28+
attach_found = 'true';
29+
}
30+
return attach_found;
31+
}
32+
33+
//deletes from sys_attachment_doc table
34+
function deletechildattachment(record) {
35+
var attach_child = new GlideRecord('sys_attachment_doc');
36+
attach_child.addQuery('sys_attachment.table_sys_id', record);
37+
attach_child.query();
38+
while (attach_child.next()) {
39+
attach_child.deleteRecord();
40+
}
41+
}

0 commit comments

Comments
 (0)