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

Hi @Deepak Shaerma - I tried with the above change in the code , but its not working .

How can I fix it ?

var usr = current.approver.sys_id;
var isInChangeRequestAG = userInGroup(usr, '67598c756f6a6100800e9eff6f3ee4e5'); //Change Request.AG
var isInChangeRequestApprovalAG = userInGroup(usr, 'f37d1d6adb57e050d407da75ca9619e7'); //Change Request Approval.AG

if ((current.sysapproval.requested_by.sys_id == current.approver.sys_id || current.sysapproval.assigned_to.sys_id == current.approver.sys_id) && !isInChangeRequestAG && !isInChangeRequestApprovalAG) {
    current.state = 'not_required';
    current.update();
}

function userInGroup(userId, groupId) {
    var grp = new GlideRecord('sys_user_grmember');
    grp.addQuery('group', groupId);
    grp.addQuery('user', userId);
    grp.setLimit(1); // No need to fetch more; we’re just checking existence
    grp.query();
    return grp.hasNext(); // True if user is in group, false otherwise
}

Hi @Deepak Shaerma - I tried with the code but its not working.