Exclude the submitter from the Group Approval

knopbrent
Tera Contributor

Hello Community

 

I am trying to write a script to exclude the submitter of a request to be exclude from the Group approval (I have a group configured in the workflow to send the Group Approval, whenever a request gets submitted and if the submitter is a member of that group, approval should be sent only to the rest of the group members and mandate only one approval from the group members). Is this possible ? If yes, can you please help me with the script ? 

5 REPLIES 5

Ankur Bawiskar
Tera Patron
Tera Patron

@knopbrent 

you can use Approval User activity in workflow and use advanced script and exclude the requester if he/she is member of that group.

OR

another way is let the approval record get generated and mark it as not required if requester is same as the approver of that record

How to remove potential member of approval group(s) if equal to requested_by within group approval w... 

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

@Ankur Bawiskar 

Instead of writing a business rule, is there are sample script to achieve this in a Scoped App workflow ?

@Ankur Bawiskar 

 

Anything wrong with the below script ?

Approval Group Activity  - Script

// Get the group ID and submitter ID
var groupId = '2c0d1f5a1b285c50ffc131901a4bcbc8';  // Replace with your group sys_id
var submitterId = current.opened_by.toString();

// Filter group members to exclude the submitter
var filterGroup = new x_stu_cloud.FilterGroupMembers();  // Use your scoped app's name
var approvers = filterGroup.getFilteredGroupMembers(groupId, submitterId);

gs.debug("Filtered Approvers: " + approvers);

// Check if approvers list is correct
if (!approvers || approvers.indexOf(submitterId) !== -1) {
    gs.error("Submitter is still in the approvers list or no approvers found. Aborting.");
    return;
}

// Create a group approval
var gr = new GlideRecord('sysapproval_group');
gr.initialize();
gr.setValue('group', groupId);
gr.setValue('state', 'requested');
gr.setValue('sysapproval', current.sys_id);  // Link to the record being approved
gr.setValue('source_table', current.getTableName());  // Set the source table
gr.insert();

 

Script Include:

var FilterGroupMembers = Class.create();
FilterGroupMembers.prototype = {
    initialize: function() {},

    getFilteredGroupMembers: function(groupId, submitterId) {
        gs.debug("FilterGroupMembers - Group ID: " + groupId);
        gs.debug("FilterGroupMembers - Submitter ID: " + submitterId);

        var approverGroup = new GlideRecord('sys_user_grmember');
        approverGroup.addQuery('group', groupId);
        approverGroup.query();

        var approvers = [];
        while (approverGroup.next()) {
            var approverUserId = approverGroup.user.toString(); // Convert to String
            if (approverUserId != submitterId) {
                approvers.push(approverUserId);
                gs.debug("FilterGroupMembers - Adding Approver: " + approverGroup.user.getDisplayValue());
            } else {
                gs.debug("FilterGroupMembers - Excluding Submitter: " + approverGroup.user.getDisplayValue());
            }
        }

        if (approvers.length === 0) {
            gs.warn("FilterGroupMembers - No approvers found after filtering. Check the group configuration.");
        }

        gs.debug("FilterGroupMembers - Final Approvers List: " + approvers);
        return approvers;
    },

    type: 'FilterGroupMembers'
};



 

@knopbrent 

did you debug by adding gs.info() statement?

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