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

nemamuskan
Tera Contributor

Hello @shruthim6691431 

 

What is the type of variable "selectedUsers" here?? Is it list collector?

Please let me know.

SD_Chandan
Kilo Sage

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


Thank you
Chandan

nemamuskan
Tera Contributor

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"]

 

var newUsersToAdd = [];
var selectedUsers = fd_data.trigger.request_item.variables.users.toString();
var group = fd_data.trigger.request_item.variables.group;
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()) {
        newUsersToAdd.push(userId);
    }
}
 
return newUsersToAdd;
//this returns the sysid's of the user that is not present in group you can check ahead condition from here
 
Please mark this helpful if this helps.
 
Thanks.

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.
 
Copy the script as it is to the flow variable(string type).
 
Please Mark this as helpful if it helps you.

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