i want to add multiple users to group using workflow run script but iam not able do it please modify

pavan_041
Tera Contributor
var array = [];
var finalusers = current.variables.add_groups_to_user.toString();
array.push(finalusers+ "");
 
);
for (var i=0;i<array.length;i++){
var gr = new GlideRecord('sys_user_grmember');
gr.initialize();
gr.user =array[i] ;
 
gr.group = current.variables.select_group_name;
//gr.setWorkflow(false);
gr.insert();
}
1 REPLY 1

Shoheb_IbaaBoss
Tera Guru

Hi.

Try Below:

 

(function executeScript() {
// Define the group name and user IDs
var groupName = 'your_group_name'; // Replace with your group name
var userIDs = ['user_id_1', 'user_id_2', 'user_id_3']; // Replace with your user IDs

// Get the group record
var groupRecord = new GlideRecord('sys_user_group');
if (groupRecord.get('name', groupName)) {
// Iterate through the user IDs and add them to the group
for (var i = 0; i < userIDs.length; i++) {
var userID = userIDs[i];

// Check if the user exists
var userRecord = new GlideRecord('sys_user');
if (userRecord.get(userID)) {
// Check if the user is not already in the group
var membershipRecord = new GlideRecord('sys_user_grmember');
if (!membershipRecord.get('group', groupRecord.sys_id, 'user', userRecord.sys_id)) {
// Add the user to the group
var newMembership = new GlideRecord('sys_user_grmember');
newMembership.initialize();
newMembership.user = userRecord.sys_id;
newMembership.group = groupRecord.sys_id;
newMembership.insert();
}
} else {
gs.info('User with ID ' + userID + ' not found.');
}
}

gs.info('Users added to the group successfully.');
} else {
gs.info('Group ' + groupName + ' not found.');
}
})();

 

replace 'your_group_name' with the actual name of your group and 'user_id_1', 'user_id_2', 'user_id_3' with the actual user IDs you want to add to the group.

 

Regards,

Ibaa