|
| 1 | +//Auto-assignment based on application variable selection in catalog |
| 2 | +///* |
| 3 | +//============================================================================== |
| 4 | +// Script: Auto Assignment by Application Group |
| 5 | +// Table: sc_req_item | When: Before Insert/Update |
| 6 | + |
| 7 | +// PREREQUISITES ✅ |
| 8 | +// ------------------------------------------------------------------------------ |
| 9 | +// 1️⃣ Catalog Variable must exist: |
| 10 | +// - Name: dummy_app_group (Select Box) |
| 11 | +// - Values: AppGroup A/B/C etc. |
| 12 | + |
| 13 | +// 2️⃣ Assignment Group sys_ids mapping must be updated with real values in PROD. |
| 14 | + |
| 15 | +// 3️⃣ Variable stored in sc_item_option_mtom for sync: |
| 16 | + // - Name: dummy_group_variable (Reference: sys_user_group) |
| 17 | + |
| 18 | +// 4️⃣ Assignment Group field must be present on RITM form. |
| 19 | + |
| 20 | +// Summary: Auto-assign Assignment Group based on selected Application Group + |
| 21 | + //update associated variable for data consistency. |
| 22 | +//============================================================================== |
| 23 | +//*/ |
| 24 | + |
| 25 | +(function executeRule(current, previous /*null when async*/) { |
| 26 | + // Retrieve the application group variable |
| 27 | + var appGroupVar = current.variables.dummy_app_group; |
| 28 | + var groupSysId = ''; // This will store the dummy sys_id |
| 29 | + |
| 30 | + // Ensure the variable exists before proceeding |
| 31 | + if (appGroupVar) { |
| 32 | + var appGroupValue = appGroupVar.getDisplayValue(); // Get the display value of the variable |
| 33 | + |
| 34 | + // Match group values and assign dummy group sys_ids |
| 35 | + if (appGroupValue === 'AppGroup A') { |
| 36 | + groupSysId = '11111111111111111111111111111111'; |
| 37 | + } else if (appGroupValue === 'AppGroup B') { |
| 38 | + groupSysId = '22222222222222222222222222222222'; |
| 39 | + } else if (appGroupValue === 'AppGroup C') { |
| 40 | + groupSysId = '33333333333333333333333333333333'; |
| 41 | + } |
| 42 | + |
| 43 | + // Update Assignment Group in the RITM |
| 44 | + if (groupSysId) { |
| 45 | + current.assignment_group = groupSysId; // Set dummy sys_id in Assignment Group |
| 46 | + gs.addInfoMessage('Assignment Group updated to sys_id: ' + groupSysId); |
| 47 | + |
| 48 | + // Update the group variable on sc_item_option_mtom table |
| 49 | + var grVars = new GlideRecord('sc_item_option_mtom'); |
| 50 | + grVars.addQuery('request_item', current.sys_id); |
| 51 | + grVars.addQuery('sc_item_option.item_option_new.name', 'dummy_group_variable'); |
| 52 | + grVars.query(); |
| 53 | + if (grVars.next()) { |
| 54 | + grVars.value = groupSysId; |
| 55 | + grVars.update(); |
| 56 | + gs.addInfoMessage('Group variable updated to sys_id: ' + groupSysId); |
| 57 | + } else { |
| 58 | + gs.addErrorMessage('Group variable not found for the RITM.'); |
| 59 | + } |
| 60 | + } else { |
| 61 | + gs.addErrorMessage('No valid Assignment Group found for: ' + appGroupValue); |
| 62 | + } |
| 63 | + } else { |
| 64 | + gs.addErrorMessage('Application group variable is not set.'); |
| 65 | + } |
| 66 | +})(current, previous); |
0 commit comments