diff --git a/Server-Side Components/Business Rules/Auto-approved opened by is approver/ReadMe.md b/Server-Side Components/Business Rules/Auto-approved opened by is approver/ReadMe.md new file mode 100644 index 0000000000..bb65031acd --- /dev/null +++ b/Server-Side Components/Business Rules/Auto-approved opened by is approver/ReadMe.md @@ -0,0 +1,17 @@ +>**When a new Approval record (sysapproval_approver table) is created** + +1. Create a Before Business Rule on the Approval (sysapproval_approver) table. + +2. Check if the table of the record being approved is the Requested For table. + +3. If it does: + + Verify whether the Approver (approver) is the same as the Opened by (opened_by) field on the related Requested For record. + + If both match: + + Automatically approve the approval record. + + Add appropriate approval comments (e.g., “Auto-approved since approver is the requestor (Opened By).”) + + Use setWorkflow(false) to prevent triggering additional workflows or business rules. diff --git a/Server-Side Components/Business Rules/Auto-approved opened by is approver/code.js b/Server-Side Components/Business Rules/Auto-approved opened by is approver/code.js new file mode 100644 index 0000000000..6545c0b1dc --- /dev/null +++ b/Server-Side Components/Business Rules/Auto-approved opened by is approver/code.js @@ -0,0 +1,37 @@ +(function executeRule(current, previous /*null when async*/ ) { + + try { + gs.setWorkflow(false); + if (!current.sysapproval) + return; + + // Get the parent record (RITM / REQ / etc.) + var parent = current.sysapproval.getRefRecord(); + if (!parent || !parent.isValidRecord()) + return; + + if (parent.getTableName() == "sc_req_item") { + + // Load Opened By user record + var userGR = new GlideRecord("sys_user"); + if (!userGR.get(parent.requested_for)) + return; + + + // If approver == Opened By → auto-approve + if (current.approver == parent.opened_by) { + current.state = "approved"; + current.comments = "Auto-approved as " + current.getDisplayValue("approver") + " is the manager of Requested For"; + current.update(); + + // Also update parent RITM stage to 'Approved' + parent.stage = "approved"; + parent.update(); + } + } + + } catch (ex) { + gs.error("Auto-approval BR error: " + ex.message); + } + +})(current, previous);