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

Ankur Bawiskar
Tera Patron
Tera Patron

@shruthim6691431 

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

AnkurBawiskar_0-1751642993981.png

AnkurBawiskar_1-1751643068420.png

 

 

AnkurBawiskar_2-1751643137823.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi Ankur, 

 

Script is not returning any value it is empty.

 

Thank you,

Shruthi

@shruthim6691431 

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.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

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.