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
Tera Sage

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

Ankur Bawiskar
Tera Patron
Tera Patron

@BenjaminY 

isn't this happening OOTB?

If you are using Group approval and your setting is "Any one to approve", then if 1 person approves then all other will be set as "No longer required" by system only

what's your use-case here? share some more details and screenshots.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

the system does not recognize "Cancelled" as an option. Even adding If record_state == Cancelled condition to flow did not work for me.

BenjaminY
Tera Contributor

The BR was created so when a request on my custom table is put into "Cancelled" status it will be dynamic as the Ask Approval OOB function within flow designer. Currently in my flow designer I have if == "Cancelled" the action is not being triggers due to the Ask approval only functioning from Approval or Reject.

Here is the working Business Rule:

(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.update();
        }
    }


})(current, previous);




@BenjaminY 

That's what I mentioned that this should happen OOTB if you have "Anyone approved" applied for your approval.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader