Remove a user from Approval - Group if delegated an Approval - User in another workflow step

kabbott1
Tera Expert

Context: In my workflow, I have an "Approval - User" (variable = line_manager_approval) and then an "Approval - Group" (called from DCS approval sys_id in the advanced script) activity in sequence. If the line manager is in the group, I want this user removed from the group to achieve two independent approvals, the line manager and a DCS approver. I have tested my script in "Scripts - Background" and it works but not when I apply it in the workflow. I have not assigned the DCS approval group under the "Approvers" heading because it always includes the line manager despite applying an advanced script to remove a manager from the group if the same as the line manager. It should be a simple task but this is taking time and a sanity toll. Could you tell me why the scripting won't work? I have attempted two different methods:

 

(1) Wait for the condition based on script:

 

(function() {
var answer = []; // Array to hold user sys_ids to be used for approval
var dcsGroupSysId = 'DCS sys id'; // sys_id of DCS group
var lineManagerSysId = current.variables.line_manager_approval.toString(); // line manager sys_id from form variable

// Ensure that the line manager sys_id is available
if (lineManagerSysId) {
var groupMembers = new GlideRecord('sys_user_grmember');
groupMembers.addQuery('group', dcsGroupSysId); // Query members of the DCS group
groupMembers.query();

// Loop through each group member and add to the answer array if they are not the line manager
while (groupMembers.next()) {
var memberSysId = groupMembers.user.sys_id.toString();
gs.log('Group Member Sys ID: ' + memberSysId);
if (memberSysId !== lineManagerSysId) {
answer.push(memberSysId); // Add member to approvers list
}
}
}
})();
 
(2) Advanced additional groups script
 
var answer = []; // Array to hold user sys_ids to be used for approval
var dcsGroupSysId = 'DCS sys id'; // sys_id of DCS group
var lineManagerSysId = current.variables.line_manager_approval.toString(); // Get the line manager sys_id from the form variable

// Ensure that the line manager sys_id is available
if (lineManagerSysId) {
var groupMembers = new GlideRecord('sys_user_grmember');
groupMembers.addQuery('group', dcsGroupSysId); // Query members of the DCS group
groupMembers.query();

// Loop through each group member and add to the answer array if they are not the line manager
while (groupMembers.next()) {
var memberSysId = groupMembers.user.sys_id.toString();
if (memberSysId !== lineManagerSysId) {
answer.push(memberSysId); // Add member to approvers list
}
}
}

// Return the approvers as an array
answer;
1 ACCEPTED SOLUTION

kabbott1
Tera Expert

The solution to my workflow issue is to change my "Approval - Group" to an "Approval - User" because my script is returning users instead of groups in the array.

View solution in original post

1 REPLY 1

kabbott1
Tera Expert

The solution to my workflow issue is to change my "Approval - Group" to an "Approval - User" because my script is returning users instead of groups in the array.