Need to fix the script in Business rule

Pooja Khatri
Tera Contributor

Hi All ,

 

I have written the below business rule for the condition : 

 

1) Check if the AG of the assignment group and the CI assignment group is same in the change request
2) If same check the member who has approved the change request at the approval from assignment group don't allow the same user to approve at CI assignment group approval
3) Update the current state to 'Not Valid' for the same user in the CI assignment group approval level

 

Script 

 

Business rule is written on change_request table , with in When to run condition , it should run before , Insert and Update 

 

Script : 

 

(function executeRule(current, previous /*null when async*/) {
 
// Add your code here
var assignmentgroup = current.assignment_group;
var cmdbcigroup = current.cmdb_ci.assignment_group;
 
if (assignmentgroup == cmdbcigroup) {
var approval = new GlideRecord('sysapproval_approver');
approval.addQuery('approval',current.sys_id);
approval.query();
if(approval.next()){
var approver = approval.approver ; 
 
var ciapproval = new GlideRecord('sysapproval_approver');
ciapproval.addQuery('sysapproval',current.sys_id);
ciapproval.addQuery('group',cmdbcigroup);
ciapproval.query();
while(ciapproval.next()){
if(ciapproval.approver == approver) {
current.state = 'not_valid';
current.update();
}
}
}
}
})(current, previous);

 

But the script is not working , can anyone please help where this script is wrong ?

 

 

7 REPLIES 7

AnveshKumar M
Tera Sage
Tera Sage

Hi @Pooja Khatri 

 

I understood that,

  1. Your change module has First level approval as Change Assignment group
  2. Second level approval is by CI Assignment Group.

And, your requirement is, if your CR assignment group and the CI Assignment Group are same, the same person who approved at First Level (CR Assignment group) should not be able to approve at Second Level (CI Assignment Group).

 

The below script will help you to achieve your requirement.

 

(function executeRule(current, previous /*null when async*/ ) {

    var assignmentgroup = current.assignment_group.sys_id + '';
    var cmdbcigroup = current.cmdb_ci.assignment_group.sys_id + '';

    if (assignmentgroup == cmdbcigroup) {
        var approval = new GlideRecord('sysapproval_approver');
        approval.addQuery('sysapproval', current.sys_id);
        approval.addQuery('state', 'approved');
        approval.query();
        var approvers = [];
        while (approval.next()) {
            approvers.push(approval.getValue("approver"));
        }

        if (approvers.length > 0) {
            var grpApproval = new GlideRecord("sysapproval_group");
            grpApproval.addQuery("parent", current.sys_id);
            grpApproval.addQuery("assignment_group", cmdbcigroup);
            grpApproval.addQuery("approval", "requested");
            grpApproval.query();
            if (grpApproval._next()) {
                var ciapproval = new GlideRecord('sysapproval_approver');
                ciapproval.addQuery('sysapproval', current.sys_id);
                ciapproval.addQuery('group', grpApproval.getUniqueValue());
                ciapproval.addQuery('state', 'requested');
                ciapproval.query();
                var arrayUtil = new ArrayUtil();
                while (ciapproval.next()) {
                    if (arrayUtil.contains(approvers, ciapproval.getValue('approver'))) {
                        ciapproval.setValue("state", "not_required");
                        ciapproval.update();
                    }
                }
            }
        }

    }
})(current, previous);

 

 Please mark my answer helpful and accept as a solution if it helped 👍✔️

Thanks,
Anvesh

Hi @AnveshKumar M - I tried with the above script but it is not working out , it still shows as requested for the same person in second level of approval 

 

Please find the attached screenshot .

How can I fix it ?

 

PoojaKhatri_0-1701101730370.png

Hi @Pooja Khatri 

Let me know, both these approvals trigger at same time or different times? I mean the second approval triggers after the first approval set is approved?

 

Are you using any work flow OR flow to trigger these approvals or approval policies?

Thanks,
Anvesh

Hi @AnveshKumar M  - I am using workflow for the approvals , and the approvals are triggering one after other .. the second approval triggers after the first approval .

 

How can I fix it ?