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..4004acfad1 --- /dev/null +++ b/Server-Side Components/Business Rules/Move attachment from variable to record/README.md @@ -0,0 +1,15 @@ +**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 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'. 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..c65f6b38c1 --- /dev/null +++ b/Server-Side Components/Business Rules/Move attachment from variable to record/Script.js @@ -0,0 +1,29 @@ +(function executeRule(current, previous /*null when async*/) { + + // 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]; + + // Get the attachment sys_id from variable value + var attachmentSysId = current.variables[varName]; + + 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);