Need flow variable script to check if user is existing in a group
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-04-2025 08:12 AM
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
thankyou
shruthi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-04-2025 12:01 PM
Hello @shruthim6691431
What is the type of variable "selectedUsers" here?? Is it list collector?
Please let me know.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-04-2025 12:36 PM
Hi @shruthim6691431 ,
You can use a Flow variable to get users not in the group, then use a For Each loop to add them to sys_user_grmember using Create Record. After that, add a comment to the RITM with the names of users added.
script to return the list of users not already in the group
var newUsersToAdd = [];
var selectedUsers = fd_data.trigger.request_item.variables.select_all_people_you_would_like_added_to_the_group;
var group = 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', group);
gr.query();
if (!gr.hasNext()) {
newUsersToAdd.push(userId);
}
}
return newUsersToAdd;
Hope this information is helpful
Chandan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-04-2025 01:23 PM
Hi @shruthim6691431 ,
You can use flow variable and the following script within that. This script returns the users not present in group.
Example format= ["0a826bf03710200044e0bfc8bcbe5d7a","71826bf03710200044e0bfc8bcbe5d3b"]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-05-2025 03:41 AM - edited ‎07-07-2025 10:14 PM
Hello @shruthim6691431 ,
Please try this updated script, this script check the users that are not present in the group ,add the users to the group as well as adds the comment as well on the RITM of the users that were not present to the group.
var addedUserNames=[];
var newUsersToAdd = [];
var selectedUsers = fd_data.trigger.request_item.variables.select_all_people_you_would_like_added_to_the_group.toString();
var group = fd_data.trigger.request_item.variables.what_group_do_you_want_to_add_remove_members_from;
var selectedUsers= selectedUsers.split(",");
// 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', group);
gr.query();
if (!gr.hasNext()) {
// Add user to group
var addGR = new GlideRecord('sys_user_grmember');
addGR.initialize();
addGR.user = userId;
addGR.group = group;
addGR.insert();
// Get user display name for the comment
var userGR = new GlideRecord('sys_user');
if (userGR.get(userId)) {
addedUserNames.push(userGR.getDisplayValue());
}
newUsersToAdd.push(userId);
}
}
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.toString(); //returns the sys_ids of all the user not in group.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-06-2025 11:08 PM
Hello @shruthim6691431,
Greetings of the day!!
Just wanted to check if my reply answer was helpful to you and answers your question?
If my response helped, please mark it helpful by selecting Accept as Solution and Helpful and close the thread so that it benefits future readers.
Thanks, and Regards,
Muskan Nema