Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Business Rule Set Requesteed approvers to "no longer required"

BenjaminY
Tera Contributor

Hello,

I created a business rule to query the approval table for requests within my custom table. 
I want the BR to put all other requested approvers except the one that approved into a "No longer Required" status. 

Here is my business rule:

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

    // Only proceed if the approval state is set to 'cancelled'
    if (current.state == 'cancelled') {

        var targetGR = current.document_id.getRefRecord();

        // Ensure the record is from your custom table
        if (targetGR.getTableName() == 'x_g_dh5_ccht_ccht_task') {

            targetGR.record_status = 'Cancelled'; //update record status field to cancelled
            targetGR.update();

            // 2. Set all other approvers to "not_required"
            var otherApprovers = new GlideRecord('sys_approval_approver');
            otherApprovers.addQuery('document_id', current.document_id);
            otherApprovers.addQuery('sys_id', '!=', current.sys_id); // Exclude current
            otherApprovers.addQuery('state', 'IN', 'requested,approved'); // Only update active approvers
            otherApprovers.query();

            while (otherApprovers.next()) {
                otherApprovers.setValue('No Longer Required');
                otherApprovers.update();
            }
        }
    }

})(current, previous);
1 ACCEPTED SOLUTION

J Siva
Kilo Patron
Kilo Patron

Hi @BenjaminY 
Try the below script.

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


    var target_rec_id = current.document_id;
    var target_rec_table = current.source_table;

    // Ensure the record is from your custom table
    if (target_rec_table == 'x_g_dh5_ccht_ccht_task') {
        var targetGR = new GlideRecord(target_rec_table);
        targetGR.get(target_rec_id);

        targetGR.record_status = 'Cancelled'; //update record status field to cancelled
        targetGR.update();

        // 2. Set all other approvers to "not_required"
        var otherApprovers = new GlideRecord('sysapproval_approver');
        otherApprovers.addQuery('document_id', target_rec_id);
        otherApprovers.addQuery('sys_id', '!=', current.sys_id); // Exclude current
        otherApprovers.addEncodedQuery('stateINrequested,approved'); // Only update active approvers
        otherApprovers.query();

        while (otherApprovers.next()) {
            otherApprovers.setValue('state', 'not_required');
            otherApprovers.setWorkflow(false);
            otherApprovers.update();
        }
    }


})(current, previous);

Regards,
Siva

View solution in original post

8 REPLIES 8

Omender Singh
Tera Guru

I made few changes in the script. Please see if it is working>

 

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

if (current.state == 'cancelled') {

var targetGR = current.document_id.getRefRecord();

if (targetGR.getTableName() == 'x_g_dh5_ccht_ccht_task') {

// Update the record status field if required
targetGR.setValue('record_status', 'Cancelled');
targetGR.update();

// Cancel all other active approvals
var otherApprovers = new GlideRecord('sys_approval_approver');
otherApprovers.addQuery('document_id', current.document_id);
otherApprovers.addQuery('sys_id', '!=', current.sys_id);
otherApprovers.addQuery('state', 'IN', 'requested,approved');
otherApprovers.query();

while (otherApprovers.next()) {
otherApprovers.setValue('state', 'not_required'); // Corrected line
otherApprovers.update();
}
}
}

})(current, previous);

J Siva
Kilo Patron
Kilo Patron

Hi @BenjaminY 
Try the below script.

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


    var target_rec_id = current.document_id;
    var target_rec_table = current.source_table;

    // Ensure the record is from your custom table
    if (target_rec_table == 'x_g_dh5_ccht_ccht_task') {
        var targetGR = new GlideRecord(target_rec_table);
        targetGR.get(target_rec_id);

        targetGR.record_status = 'Cancelled'; //update record status field to cancelled
        targetGR.update();

        // 2. Set all other approvers to "not_required"
        var otherApprovers = new GlideRecord('sysapproval_approver');
        otherApprovers.addQuery('document_id', target_rec_id);
        otherApprovers.addQuery('sys_id', '!=', current.sys_id); // Exclude current
        otherApprovers.addEncodedQuery('stateINrequested,approved'); // Only update active approvers
        otherApprovers.query();

        while (otherApprovers.next()) {
            otherApprovers.setValue('state', 'not_required');
            otherApprovers.setWorkflow(false);
            otherApprovers.update();
        }
    }


})(current, previous);

Regards,
Siva

BenjaminY
Tera Contributor

Thanks this worked! is there a possible if statement I can add if none of the approvers cancelled but the request is stilled in cancelled status. I want it to make all approvers "not_required"

Omender Singh
Tera Guru
(function executeRule(current, previous /*null when async*/) {

if (current.state == 'cancelled') {

var targetGR = current.document_id.getRefRecord();

if (targetGR.getTableName() == 'x_g_dh5_ccht_ccht_task') {

// Update the record status field if required
targetGR.setValue('record_status', 'Cancelled');
targetGR.update();

// Cancel all other active approvals
var otherApprovers = new GlideRecord('sys_approval_approver');
otherApprovers.addQuery('document_id', current.document_id);
otherApprovers.addQuery('sys_id', '!=', current.sys_id);
otherApprovers.addQuery('state', 'IN', 'requested,approved');
otherApprovers.query();

while (otherApprovers.next()) {
otherApprovers.setValue('state', 'not_required'); // Corrected line
otherApprovers.update();
}
}
}

})(current, previous);

I made few changes in the script. Please see if it is working