Need flow variable script to check if user is existing in a group

shruthim6691431
Giga Contributor
Hi,

In my requirement, I am checking whether the selected users are already part of the group. If they are, I do not add them again. If the users do not exist in the group, I add them. After this process, I need to add an additional comment indicating which users were added to the group, but the current script for this is not working. Can you please help me
var newUsersToAdd = [];
 
var selectedUsers = fd_data.trigger.request_item.variables.select_all_people_you_would_like_added_to_the_group;
// Just to clarify: selectedUsers is an array of sys_ids
for (var i = 0; i < selectedUsers.length; i++) {
    var userId = selectedUsers[i];
 
    var gr = new GlideRecord('sys_user_grmember');
    gr.addQuery('user', userId);
    gr.addQuery('group', fd_data.trigger.request_item.variables.what_group_do_you_want_to_add_remove_members_from);
    gr.query();
 
    if (!gr.hasNext()) {
        newUsersToAdd.push(userId);
    }
}
 
return newUsersToAdd;

thankyou 
shruthi
13 REPLIES 13

Hello @shruthim6691431,

 

Please check the above snippet for the flow designer variable script.

Hope it helps, Please mark it helpful and also close the thread for future readers help.

 

Thanks and Regards,

Muskan Nema

Hello @shruthim6691431,

 

Have you tried this one once? Please try this one once. Maybe it could help.

Thanks,

Muskan 

Shubham_Jain
Mega Sage

@shruthim6691431  

 

var newUsersToAdd = [];
var addedUserNames = [];

var selectedUsers = fd_data.trigger.request_item.variables.select_all_people_you_would_like_added_to_the_group;
var groupId = fd_data.trigger.request_item.variables.what_group_do_you_want_to_add_remove_members_from;

for (var i = 0; i < selectedUsers.length; i++) {
var userId = selectedUsers[i];

var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('user', userId);
gr.addQuery('group', groupId);
gr.query();

if (!gr.hasNext()) {
// Add user to group
var addGR = new GlideRecord('sys_user_grmember');
addGR.initialize();
addGR.user = userId;
addGR.group = groupId;
addGR.insert();

// Get user display name for the comment
var userGR = new GlideRecord('sys_user');
if (userGR.get(userId)) {
addedUserNames.push(userGR.getDisplayValue());
}

// Track sys_id if needed
newUsersToAdd.push(userId);
}
}

// Add comment to the request item (if any user was added)
if (addedUserNames.length > 0) {
var comment = 'The following users were added to the group: ' + addedUserNames.join(', ');
var reqItem = fd_data.trigger.request_item;
reqItem.comments = comment;
reqItem.update(); // Make sure to update the record
}

return newUsersToAdd;

 

 

  • Make sure fd_data.trigger.request_item points to the correct RITM object in your context.
  • If you want to write to work notes instead of comments, use reqItem.work_notes = ....
  • For large user lists, consider using a GlideAggregate to optimize checking.

 

✔️ If this solves your issue, please mark it as Correct.


✔️ If you found it helpful, please mark it as Helpful.



Shubham Jain


@shruthim6691431  Just wanted to check was my answer helpful for you? Please confirm. 

✔️ If this solves your issue, please mark it as Correct.


✔️ If you found it helpful, please mark it as Helpful.



Shubham Jain