Issue with the Business rule not working

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();
}
9 REPLIES 9

Karunakaran
Mega Guru

Hi @Pooja Khatri ,

 

The issue is because of this line. if you are using logical "||" then you need to consider grouping the conditions using "()" brackets. Also, you dont need to use the array for this operation instead you can use a boolean variable with true or false.

if(current.sysapproval.requested_by.sys_id == current.approver.sys_id || current.sysapproval.assigned_to.sys_id == current.approver.sys_id && mbr =='' && mbr2 =='')

 

If the above line is not an issue as per your requirement, I would like to understand below points.

 

1. what is the table name for this "Change Request Approval.AG" ?

2. Is it because of the hard coded group sys_id. Are you trying with the same group and user record is part of this group?

 

 

Otherwise, we can re-write the code like this.

 

var changeRequest = new GlideRecord('change_request');
if (changeRequest.get(current.sysapproval.getRefRecord())) {
var changeGroup = changeRequest.assignment_group;
var approver = current.approver;
var approverUser = new GlideUser(approver);
if (approverUser.isMemberOf(changeGroup)) {
gs.addErrorMessage("Approver is part of the assignment group of the change request."); //you can do your approval record update here.
}
}

 

 

Please let me know, if you have further questions. Feel free to ask.

 

Thank you.

 

Regards,

Karunakaran.

Hi @Karunakaran - the code which I have mentioned is correct , for line (current.sysapproval.requested_by.sys_id == current.approver.sys_id || current.sysapproval.assigned_to.sys_id == current.approver.sys_id && mbr =='' && mbr2 =='')

 

the part of code : current.sysapproval.requested_by.sys_id == current.approver.sys_id || current.sysapproval.assigned_to.sys_id == current.approver.sys_id  this is checking if the member is apart of first two level of approvals and it is making it as not required in that part 

I need to fix the part where mbr =='' && mbr2 =='' this is allowing the requested by or assigned to approve its own change in the 
third approval from the Change Request.AG approval and fourth one from Change Request Approval.AG approval . 

I need to make changes to the existing code , I cannot change the entire logic of the code .. I just need to make sure that the requested by/ assigned to cannot approve their own changes in the third and fourth level of approvals where the above sys_id's are hardcoded too .

I need to fix the code with the existing code , can you please help me to fix it .

Hi again,

 

Okay.  I think you need to debug the code to understand its behavior. Please write the logs in each loop with its value to understand your code.

You can use gs.info with all the possible variable values in each loop.

 

Please let me know.

 

Regards,

Karunakaran.

Hi @Karunakaran - I tried with the debugging but it didnt helped much , can you help me in fixing it ?