When requestor is approver approval should not go
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-05-2025 02:38 AM
Hello all,
I have a requirement: We have a custom table MDM where if the ticket is raised the approver is triggered from MDM Flow Data lookup table. Now if there is only one approver and if he raises MDM ticket it should show error message but if there are multiple approver and one of them raise MDM ticket the approval should go to other approvers ecept him.
For this I have written a before Business rule on "MDM request" table with below script:
Thanks & Regards,
I Das
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-05-2025 03:21 AM
try this -> don't use includes() but use indexOf()
Also I assume approval_users is reference type and referring to sys_user
Also add gs.info() for debugging
(function executeRule(current, previous /*null when async*/ ) {
// Get the requester's user ID
var requester = current.requested_for || current.requested_by;
// Fetch approvers from the data flow lookup
var approvers = getApprovers(current);
if (approvers.length === 0) {
return; // No approvers found, exit
}
if (approvers.length === 1 && approvers[0] == requester) {
// If only one approver and they are the requester, show an error
gs.addErrorMessage("You cannot approve your own request.");
current.setAbortAction(true); // Stops the request from being created
return;
}
if (approvers.indexOf(requester) > -1) {
// Remove requester from approvers list if multiple approvers exist
var index = approvers.indexOf(requester);
if (index > -1) { // only splice array when item is found
approvers.splice(index, 1); // 2nd parameter means remove one item only
} // Update the approvers list in the approval record
updateApprovers(current, approvers);
}
})(current, previous);
// Function to get approvers from data flow lookup
function getApprovers(current) {
var approvers = [];
var lookupGR = new GlideRecord('x_hclan_mdm_smart_flow_data_lookups');
lookupGR.addQuery('approval_users', current.approval_users);
lookupGR.query();
while (lookupGR.next()) {
approvers.push(lookupGR.approver.toString());
}
return approvers;
}
// Function to update the approval list
function updateApprovers(current, approvers) {
var approvalGR = new GlideRecord('sysapproval_approver');
approvalGR.addQuery('sysapproval', current.sys_id);
approvalGR.query();
while (approvalGR.next()) {
if (approvers.indexOf(approvalGR.approver.toString()) == -1) {
approvalGR.deleteRecord(); // Remove requester from approval process
}
}
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-05-2025 04:36 AM
Hello @Ankur Bawiskar ,
It is still not working. And Approval user is list type referencing user.
Regards,
I Das
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-05-2025 07:22 PM
if it's list type then update this line
lookupGR.addQuery('approval_users', 'IN' ,current.approval_users);
(function executeRule(current, previous /*null when async*/ ) {
// Get the requester's user ID
var requester = current.requested_for || current.requested_by;
// Fetch approvers from the data flow lookup
var approvers = getApprovers(current);
if (approvers.length === 0) {
return; // No approvers found, exit
}
if (approvers.length === 1 && approvers[0] == requester) {
// If only one approver and they are the requester, show an error
gs.addErrorMessage("You cannot approve your own request.");
current.setAbortAction(true); // Stops the request from being created
return;
}
if (approvers.indexOf(requester) > -1) {
// Remove requester from approvers list if multiple approvers exist
var index = approvers.indexOf(requester);
if (index > -1) { // only splice array when item is found
approvers.splice(index, 1); // 2nd parameter means remove one item only
} // Update the approvers list in the approval record
updateApprovers(current, approvers);
}
})(current, previous);
// Function to get approvers from data flow lookup
function getApprovers(current) {
var approvers = [];
var lookupGR = new GlideRecord('x_hclan_mdm_smart_flow_data_lookups');
lookupGR.addQuery('approval_users', 'IN' ,current.approval_users);
lookupGR.query();
while (lookupGR.next()) {
approvers.push(lookupGR.approver.toString());
}
return approvers;
}
// Function to update the approval list
function updateApprovers(current, approvers) {
var approvalGR = new GlideRecord('sysapproval_approver');
approvalGR.addQuery('sysapproval', current.sys_id);
approvalGR.query();
while (approvalGR.next()) {
if (approvers.indexOf(approvalGR.approver.toString()) == -1) {
approvalGR.deleteRecord(); // Remove requester from approval process
}
}
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader