diff --git a/Server-Side Components/Background Scripts/Change Approver/Change Approver_1.js b/Server-Side Components/Background Scripts/Change Approver/Change Approver_1.js new file mode 100644 index 0000000000..b83a605af8 --- /dev/null +++ b/Server-Side Components/Background Scripts/Change Approver/Change Approver_1.js @@ -0,0 +1,36 @@ +// Parent record sys_id (change as needed) +var parentSysId = 'd2cdb552db252200a6a2b31be0b8f5ee'; + +// Dummy user display name (must exist in sys_user table) +var dummyApproverName = 'Dummy'; + +// Step 1: Find the existing approval for the parent record +var approvalGR = new GlideRecord('sysapproval_approver'); +approvalGR.addQuery('sysapproval', parentSysId); +approvalGR.query(); + +if (approvalGR.next()) { + // Step 2: Mark current approval as not required + approvalGR.state = 'No Longer Required'; + approvalGR.update(); + gs.info('Existing approval marked as not required for record: ' + parentSysId); + + // Step 3: Find the dummy user in sys_user + var userGR = new GlideRecord('sys_user'); + if (userGR.get('name', dummyApproverName)) { // exact match of Name field + // Step 4: Create a new approval for the dummy user + var newApproval = new GlideRecord('sysapproval_approver'); + newApproval.initialize(); + newApproval.sysapproval = parentSysId; + newApproval.approver = userGR.sys_id; + newApproval.state = 'requested'; + newApproval.insert(); + + gs.info('New approval assigned to ' + dummyApproverName + ' for record: ' + parentSysId); + } else { + gs.warn('Dummy user not found: ' + dummyApproverName); + } + +} else { + gs.warn('No approval record found for parent record sys_id: ' + parentSysId); +} diff --git a/Server-Side Components/Background Scripts/Change Approver/Change README.md b/Server-Side Components/Background Scripts/Change Approver/Change README.md new file mode 100644 index 0000000000..0dd462f9bb --- /dev/null +++ b/Server-Side Components/Background Scripts/Change Approver/Change README.md @@ -0,0 +1,49 @@ +Safe Approval Reassignment Script — Dummy User +Overview + +This ServiceNow background script safely reassigns an approval for a given record. +Instead of directly overwriting the existing approver (which is not recommended), it: + +Marks the current approval as “No Longer Required”. + +Creates a new approval for a dummy user in the sys_user table. + +This approach ensures safe testing, demos, or Hacktoberfest contributions without impacting real approvals or notifications. + +Features + +Safe & Best Practice: Avoids overwriting existing approvals that may have already triggered notifications. + +Dynamic: Can be reused for any parent record by changing parentSysId. + +Dummy User: Uses a placeholder user to safely test or demonstrate approval assignment. + +Clear Logging: Outputs info/warning messages for easy verification. + +How to Use + +Create a dummy user in the sys_user table (mandatory). + +Update the script with: + +parentSysId → sys_id of the record whose approval you want to reassign. + +dummyApproverName → Name field of the dummy user in sys_user. + +Open Scripts – Background in your ServiceNow instance. + +Paste the script and run. + +Check the system logs for info/warnings about the reassignment. + +Example Output +Existing approval marked as not required for record: d2cdb552db252200a6a2b31be0b8f5ee +New approval assigned to Dummy for record: d2cdb552db252200a6a2b31be0b8f5ee + +Notes + +Ensure the dummy user exists in the sys_user table; otherwise, the script will warn Dummy user not found. + +This script is read-only for the old approval and only modifies workflow safely for testing. + +Can be easily extended to handle multiple approvals or multiple dummy users for more complex scenarios.