|
1 | 1 | (function executeRule(current, previous /*null when async*/) { |
2 | 2 |
|
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]; |
8 | 8 |
|
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]; |
11 | 11 |
|
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 | + } |
16 | 27 | } |
17 | 28 |
|
18 | 29 | })(current, previous); |
0 commit comments