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

Hi @Pooja Khatri 

I have build a workflow to simulate this scenario and found the issue. Hope you are using Approval - Group activity in workflow like the one below.

AnveshKumarM_0-1701161061081.png

This will not trigger the business rule as it won't set the approval state in change record. Hence we need to create a business rule on sysapproval_group table like the one below. Try the below steps and let me know.

 

1. Create After - Insert business rule on sysapproval_group table

AnveshKumarM_1-1701161344103.png

 

2. Use the below script

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

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

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

        if (approvers.length > 0) {
            var grpApproval = new GlideRecord("	x");
            grpApproval.addQuery("parent", current.parent.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.parent);
                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

I am using approval group activity in my workflow 
 

Hi @AnveshKumar M - I tried with the above code made change in this part of code :

if (approvers.length > 0)

{ var grpApproval = new GlideRecord(" x");

to 

 if (approvers.length > 0) {
            var grpApproval = new GlideRecord("sysapproval_group");
 
also I entered logs in my code to check the issue and see the code is not working after the below part , no logs are getting printed from this code .
 
if (grpApproval._next()) {
                var ciapproval = new GlideRecord('sysapproval_approver');
                ciapproval.addQuery('sysapproval', current.parent);
                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_valid");
                        ciapproval.update();
 
                    }
                }
            }
        }
 
Can you please help me to fix this issue ? 

Amit Gujarathi
Giga Sage
Giga Sage

HI @Pooja Khatri ,
I trust you are doing great.

(function executeRule(current, previous /*null when async*/) {
    var assignmentGroup = current.assignment_group;
    var cmdbCiGroup = current.cmdb_ci.assignment_group;

    if (assignmentGroup == cmdbCiGroup) {
        var approval = new GlideRecord('sysapproval_approver');
        approval.addQuery('sysapproval', current.sys_id); // Ensure correct field name
        approval.query();
        while (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'; // Ensure 'not_valid' is a valid choice
                    current.update();
                    return; // Exit the script after updating
                }
            }
        }
    }
})(current, previous);

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi