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-07-2025 10:18 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-09-2025 01:04 AM
Hello @shruthim6691431,
Have you tried this one once? Please try this one once. Maybe it could help.
Thanks,
Muskan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2025 10:40 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-06-2025 11:24 PM
@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