Send approval request(s) to a particular group.

Taaha M
Tera Contributor

Send approval request(s) to a particulars group. Only one of the members of the group needs to provide approval.
If 'Opened by' is the same person as an approver member, mark it as approved.

Help me with a script

3 REPLIES 3

AJ-TechTrek
Giga Sage
Giga Sage

Hi @Taaha M ,

 

I hope below code will help you.

 

// Define the group name to which the approval request will be sent
var groupName = 'approval_group';

// Get the current user who opened the request
var openedBy = gs.getUserDisplayName();

// Query the users who are members of the approval group
var groupMembers = new GlideRecord('sys_user_grmember');
groupMembers.addQuery('group', groupName);
groupMembers.query();

// Flag to check if the opener is also an approver
var isOpenerApprover = false;

// Loop through the group members to check if the opener is also an approver
while (groupMembers.next()) {
var approver = groupMembers.user.getDisplayValue();
if (approver == openedBy) {
isOpenerApprover = true;
break;
}
}

// If the opener is an approver, mark the request as approved
if (isOpenerApprover) {
current.approval = 'approved';
current.approval_notes = 'Automatically approved because the opener is also an approver.';
} else {
// If the opener is not an approver, send an approval request to the group
var approval = new GlideApproval();
approval.setTableName(current.getTableName());
approval.setRecord(current);
approval.setRequestedFor(current.requested_for);
approval.setRequestedBy(current.opened_by);
approval.setApprovalDefinition('default');
approval.request(current.opened_by, groupName, 'Please approve this request.');
}

 

  • Replace 'approval_group' with the name of your approval group.
  • It first checks if the user who opened the request is also an approver in the group.
  • If the opener is an approver, it automatically marks the request as approved.
  • If the opener is not an approver, it sends an approval request to the group.

Please appreciate the efforts of community contributors by marking appropriate response as Mark my Answer Helpful or Accept Solution this may help other community users to follow correct solution in future.

 

Thanks

Ajay Kumar

Linkedin Profile:- https://www.linkedin.com/in/ajay-kumar-66a91385/

ServiceNow Community Rising Star 2024

This script is not working

SAI VENKATESH
Tera Sage
Tera Sage

Hi @Taaha M 

You can try the below code 

After business rule (inserted or updated)

(function executeRule(current, previous /*null when async*/) {
    var approvalGroupSysId = 'Your group sysid'; 
    var openedByUserID = current.opened_by.sys_id;
    var userGroup = false;

    var gm = new GlideRecord('sys_user_grmember');
    gm.addQuery('group', approvalGroupSysId);
    gm.addQuery('user', openedByUserID);
    gm.query();

    if (gm.next()) {
        userGroup = true;
    }

    // Create an approval request for the group
    var GR = new GlideRecord('sysapproval_approver');
    GR.initialize();
    GR.approver.setValue('group', approvalGroupSysId);
    GR.document_id = current.sys_id;
    GR.source_table = current.getTableName();
    GR.insert();
    if (userGroup) {
        GR.setValue('state', 'approved');
        GR.update();
    }
})(current, previous);

 

Thanks and Regards

Sai Venkatesh