Issue with the Business rule

Pooja Khatri
Tera Contributor
I have a requirement where a change request goes through 4 level of approvals , first from the ag approval , second from Configuration item AG approval  third from the Change Request.AG approval and fourth one from Change Request Approval.AG approval . 
 
so if the requested by or assigned to is a member of any of the above mentioned 4 AG approval group it should show the state as "not required" for those members in all the 4 AG approval levels
 
the first two approval groups first from the ag approval , second from Configuration item AG approval  is managed by this part of code : (current.sysapproval.requested_by.sys_id == current.approver.sys_id || current.sysapproval.assigned_to.sys_id == current.approver.sys_id and it is working perfectly fine
 
How to manage the scenario where the requested by or assigned to is a member of the Change Request.AG or Change Request Approval.AG -- this code is not working
 
How can I fix the above mentioned highlighted issue . 
 
Business rule : 
 
The BR is writted on the sysapproval_approver table and its an after insert BR .
Script : 
var usr= current.approver.sys_id;
var mbr =[];
var mbr2 =[]; 
var grp = new GlideRecord('sys_user_grmember');
grp.addQuery('group', '67598c756f6a6100800e9eff6f3ee4e5');//Change Request.AG
grp.addQuery('user', usr);
grp.query();
while(grp.next()){
   mbr.push('yes');
}
 
var grp2 = new GlideRecord('sys_user_grmember');
grp2.addQuery('group', 'f37d1d6adb57e050d407da75ca9619e7');//Change Request Approval.AG
grp2.addQuery('user', usr);
grp2.query();
while(grp2.next()){
   mbr2.push('yes');
}
if(current.sysapproval.requested_by.sys_id == current.approver.sys_id || current.sysapproval.assigned_to.sys_id == current.approver.sys_id && mbr =='' && mbr2 ==''){
current.state = 'not_required';
current.update();
}
7 REPLIES 7

Kieran Anson
Kilo Patron

Hey

Couple of code sanity, and best practice pointers

  1. Avoid unnecessary dot-walk current.getValue('approver') is more efficient than current.approver.sys_id
  2. Don't hard-code sys_id's. Store them in a system property
  3. If you're in global scope, make use of cached information
  4. If you're modifying the current record, use a before business rule
  5. Use getters and setters on GlideRecords
  6. 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

KieranAnson_0-1714561493242.pngKieranAnson_1-1714561513306.png

 

 

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 ?

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;
}

Deepak Shaerma
Kilo Sage

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