Skip to content

Commit 6f69ccc

Browse files
Update Script.js
As per the feedback from PR
1 parent 338a7b0 commit 6f69ccc

File tree

1 file changed

+22
-11
lines changed
  • Server-Side Components/Business Rules/Move attachment from variable to record

1 file changed

+22
-11
lines changed
Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
11
(function executeRule(current, previous /*null when async*/) {
22

3-
// Find attachments linked to variables (table_sys_id of RITM's variables)
4-
var attGR = new GlideRecord('sys_attachment');
5-
attGR.addQuery('table_sys_id', current.sys_id); // attachment originally linked to variable of this RITM
6-
attGR.addQuery('table_name', '!=', 'sc_req_item'); // exclude already moved attachments
7-
attGR.query();
3+
// List of variable names for which the attachment has to be moved to the record level.
4+
var attachmentVars = ['attachment1', 'attachment2'];
5+
6+
for (var i = 0; i < attachmentVars.length; i++) {
7+
var varName = attachmentVars[i];
88

9-
while (attGR.next()) {
10-
gs.info('Moving attachment: ' + attGR.file_name);
9+
// Get the attachment sys_id from variable value
10+
var attachmentSysId = current.variables[varName];
1111

12-
// Update to associate with the RITM record
13-
attGR.table_name = 'sc_req_item';
14-
attGR.table_sys_id = current.sys_id;
15-
attGR.update();
12+
if (!attachmentSysId) {
13+
gs.info("No attachment found in variable: " + varName);
14+
continue;
15+
}
16+
17+
// Get attachment record using sys_id
18+
var attGR = new GlideRecord('sys_attachment');
19+
if (attGR.get(attachmentSysId)) {
20+
gs.info('Moving attachment: ' + attGR.file_name);
21+
22+
// Update reference to link to the RITM
23+
attGR.table_name = 'sc_req_item';
24+
attGR.table_sys_id = current.sys_id;
25+
attGR.update();
26+
}
1627
}
1728

1829
})(current, previous);

0 commit comments

Comments
 (0)