Issue with the Business rule
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-01-2024 02:26 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-01-2024 04:05 AM
Hey
Couple of code sanity, and best practice pointers
- Avoid unnecessary dot-walk
current.getValue('approver')
is more efficient thancurrent.approver.sys_id
- Don't hard-code sys_id's. Store them in a system property
- If you're in global scope, make use of cached information
- If you're modifying the current record, use a before business rule
- Use getters and setters on GlideRecords
- Don't build monolithic business rules - break them down into separate purposes
var approvingUser = current.getValue('approver');
var glideUser = gs.getUser().getUserByID(approver);
//We've failed to get the user, glideUser will be the guest user which we don't want to eval against
if(glideUser.isDefault())
return;
//Store the groups in a comma seperated string sys prop
var exemptGroups = gs.getProperty('company_code.change_management.approval_exempt_groups','');
var exemptGroupsArr = exemptGroups.split(",");
var isGroupMember = exemptGroupsArr.some(function(groupId){
return glideUser.isMember(groupId)
})
if(isGroupMember)
current.setValue('state' , 'not_required')
You can then have a separate no-code business rule for self-approval
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-03-2024 01:09 AM
Hi @Kieran Anson - I want to handle the scenario in the existing code itself and dont want to replace it , how can I fix it in the same part ?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-03-2024 02:04 AM
The existing solution is bad practice and doesn't follow recommended coding standards and maintability. However the below would function in a before BR with a condition to check the approving task is a change_request
var approvingUser = current.getValue('approver');
var glideUser = gs.getUser().getUserByID(approvingUser);
var exemptGroups = ['67598c756f6a6100800e9eff6f3ee4e5', 'f37d1d6adb57e050d407da75ca9619e7'];
var isGroupMember = exemptGroupsArr.some(function(groupId) {
return glideUser.isMember(groupId)
})
//If they're a group member, set to not required
if (isGroupMember) {
current.setValue('state', 'not_required')
return;
}
var taskRecordGR = current.sysapproval.getRefRecord();
if (!taskRecordGR.isValidRecord())
return;
//If they're the requestor, set to not required
if (approvingUser == taskRecordGR.getValue('requested_by')){
current.setValue('state', 'not_required')
return;
}
//If they're the assignee, set to not required
if (approvingUser == taskRecordGR.getValue('assigned_to')){
current.setValue('state', 'not_required')
return;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-01-2024 04:09 AM
Hi @Pooja Khatri
your code is correct, but you’re using mbr == '' and mbr2 == '' to determine if they’re empty, but since they are arrays, this comparison will not work. In javascript, comparing an array directly to an empty string will not check its emptiness correctly.
if ((current.sysapproval.requested_by.sys_id == current.approver.sys_id || current.sysapproval.assigned_to.sys_id == current.approver.sys_id) && mbr.length == 0 && mbr2.length == 0) {
current.state = 'not_required';
current.update();
}
Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help both the community and me..
- Keep Learning
Thanks & Regards
Deepak Sharma