From 3d1ff946a4f404573db1f1bf9d78c7a7a50800f7 Mon Sep 17 00:00:00 2001 From: Chaitanya Lal <42643661+chaitanyalal18@users.noreply.github.com> Date: Thu, 23 Oct 2025 20:00:23 +0530 Subject: [PATCH 1/4] Create README.md --- .../README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 Server-Side Components/Business Rules/Move attachment from variable to record/README.md diff --git a/Server-Side Components/Business Rules/Move attachment from variable to record/README.md b/Server-Side Components/Business Rules/Move attachment from variable to record/README.md new file mode 100644 index 0000000000..8b70e8e83c --- /dev/null +++ b/Server-Side Components/Business Rules/Move attachment from variable to record/README.md @@ -0,0 +1,16 @@ +**Scenario**: + +In some catalog items, we might want to make attachments mandatory based on certain conditions. +To achieve this, we typically use an Attachment variable on the catalog item. + +When a user submits the catalog item, any attachments uploaded through this variable are stored in the sys_attachment +table with the table name set to the variable’s source — usually ZZ_YYsc_cat_item_producer. +However, in certain cases, we might want these attachments to be associated directly with the RITM (sc_req_item) record instead of staying linked +to the variable. + +**Solution**: + +We can create an After Insert Business Rule on the sc_req_item table that automatically reassigns such attachments to the corresponding RITM. + +This rule will run only for RITMs created from specific catalog items as defined in the filter condition of BR and query the sys_attachment table for records where +table_sys_id matches the current RITM’s sys_id, and table_name equals 'ZZ_YYsc_cat_item_producer' and update the table_name to 'sc_req_item'. From 91a03d273b80e12b1876dc8562e018e41f8c4a22 Mon Sep 17 00:00:00 2001 From: Chaitanya Lal <42643661+chaitanyalal18@users.noreply.github.com> Date: Thu, 23 Oct 2025 20:01:03 +0530 Subject: [PATCH 2/4] Create Script.js --- .../Script.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Server-Side Components/Business Rules/Move attachment from variable to record/Script.js diff --git a/Server-Side Components/Business Rules/Move attachment from variable to record/Script.js b/Server-Side Components/Business Rules/Move attachment from variable to record/Script.js new file mode 100644 index 0000000000..d92e8efc2e --- /dev/null +++ b/Server-Side Components/Business Rules/Move attachment from variable to record/Script.js @@ -0,0 +1,18 @@ +(function executeRule(current, previous /*null when async*/) { + + // Find attachments linked to variables (table_sys_id of RITM's variables) + var attGR = new GlideRecord('sys_attachment'); + attGR.addQuery('table_sys_id', current.sys_id); // attachment originally linked to variable of this RITM + attGR.addQuery('table_name', '!=', 'sc_req_item'); // exclude already moved attachments + attGR.query(); + + while (attGR.next()) { + gs.info('Moving attachment: ' + attGR.file_name); + + // Update to associate with the RITM record + attGR.table_name = 'sc_req_item'; + attGR.table_sys_id = current.sys_id; + attGR.update(); + } + +})(current, previous); From 6f69cccd228f9f0719e4da1e66202f3fc0621c4f Mon Sep 17 00:00:00 2001 From: Chaitanya Lal <42643661+chaitanyalal18@users.noreply.github.com> Date: Fri, 24 Oct 2025 12:14:21 +0530 Subject: [PATCH 3/4] Update Script.js As per the feedback from PR --- .../Script.js | 33 ++++++++++++------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/Server-Side Components/Business Rules/Move attachment from variable to record/Script.js b/Server-Side Components/Business Rules/Move attachment from variable to record/Script.js index d92e8efc2e..c65f6b38c1 100644 --- a/Server-Side Components/Business Rules/Move attachment from variable to record/Script.js +++ b/Server-Side Components/Business Rules/Move attachment from variable to record/Script.js @@ -1,18 +1,29 @@ (function executeRule(current, previous /*null when async*/) { - // Find attachments linked to variables (table_sys_id of RITM's variables) - var attGR = new GlideRecord('sys_attachment'); - attGR.addQuery('table_sys_id', current.sys_id); // attachment originally linked to variable of this RITM - attGR.addQuery('table_name', '!=', 'sc_req_item'); // exclude already moved attachments - attGR.query(); + // List of variable names for which the attachment has to be moved to the record level. + var attachmentVars = ['attachment1', 'attachment2']; + + for (var i = 0; i < attachmentVars.length; i++) { + var varName = attachmentVars[i]; - while (attGR.next()) { - gs.info('Moving attachment: ' + attGR.file_name); + // Get the attachment sys_id from variable value + var attachmentSysId = current.variables[varName]; - // Update to associate with the RITM record - attGR.table_name = 'sc_req_item'; - attGR.table_sys_id = current.sys_id; - attGR.update(); + if (!attachmentSysId) { + gs.info("No attachment found in variable: " + varName); + continue; + } + + // Get attachment record using sys_id + var attGR = new GlideRecord('sys_attachment'); + if (attGR.get(attachmentSysId)) { + gs.info('Moving attachment: ' + attGR.file_name); + + // Update reference to link to the RITM + attGR.table_name = 'sc_req_item'; + attGR.table_sys_id = current.sys_id; + attGR.update(); + } } })(current, previous); From 733766825d7e8a2b87a2c300f033942cbf6de8fa Mon Sep 17 00:00:00 2001 From: Chaitanya Lal <42643661+chaitanyalal18@users.noreply.github.com> Date: Fri, 24 Oct 2025 12:17:13 +0530 Subject: [PATCH 4/4] Update README.md --- .../Move attachment from variable to record/README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Server-Side Components/Business Rules/Move attachment from variable to record/README.md b/Server-Side Components/Business Rules/Move attachment from variable to record/README.md index 8b70e8e83c..4004acfad1 100644 --- a/Server-Side Components/Business Rules/Move attachment from variable to record/README.md +++ b/Server-Side Components/Business Rules/Move attachment from variable to record/README.md @@ -12,5 +12,4 @@ to the variable. We can create an After Insert Business Rule on the sc_req_item table that automatically reassigns such attachments to the corresponding RITM. -This rule will run only for RITMs created from specific catalog items as defined in the filter condition of BR and query the sys_attachment table for records where -table_sys_id matches the current RITM’s sys_id, and table_name equals 'ZZ_YYsc_cat_item_producer' and update the table_name to 'sc_req_item'. +This rule will run only for RITMs created from specific catalog items, as defined in the filter condition of BR, and retrieve the attachment record from the sys_attachment table using the attachment variable value. It will then update the table_name to 'sc_req_item'.