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 08:32 AM
where have you written this script?
You can use a flow variable to hold users who will be added and then use this flow variable to update the comments
use this in a flow variable of type String and then use that flow variable
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;
// 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()) {
newUsersToAdd.push(userId);
}
}
var displayNameUser = [];
var gr = new GlideRecord("sys_user");
gr.addQuery("sys_id", "IN", newUsersToAdd.toString());
gr.query();
while (gr.next()) {
displayNameUser.push(gr.getDisplayValue());
}
return displayNameUser.toString();
You can then use this Flow variable to perform Lookup and then Iterate to add to group member table
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-04-2025 10:49 AM
Hi Ankur,
Script is not returning any value it is empty.
Thank you,
Shruthi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-04-2025 10:37 PM
update this line so that the variable is an array and you can then iterate
var selectedUsers = fd_data.trigger.request_item.variables.select_all_people_you_would_like_added_to_the_group.toString().split(',');
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-09-2025 12:12 AM
Hi Ankur,
I used this script to fetch the users and add them in the additional comments. Unfortunately, the script is retrieving all the selected users instead of only the users who are actually being added to the group.
I need it to capture only the users who are being added, not the ones who are skipped because they are already part of the group.