How to add a user to a group by using background script

Shaik nabi khaj
Tera Expert

 

Hi,

 

I have a reqirement called

How can we add a user to a group by using background script.

 

Kindly provide ur inputs it is on priority.

 

Best Regards,

Shaik Nabi Khaja.

2 ACCEPTED SOLUTIONS

Hi @Shaik nabi khaj 

Then use script provided by me.

Just copy past user and group sys_id where ever mentioned.

 


Please Mark My Response as Correct/Helpful based on Impact
Regards,
Gunjan Kiratkar
2X ServiceNow MVP
Community Rising Star 2022
Youtube : ServiceNow Guy

View solution in original post

Hi @Shaik nabi khaj ,

Apply filter on sys_user table for those users based on email or user ID.

Copy that encoded query as below, 

GunjanKiratkar_0-1672388410387.png

Script :- Add group sys_id only

Replace below encoded query with yours

 

var grUser = new GlideRecord("sys_user");
grUser.addEncodedQuery("emailINshaun.tait@example.com,john.smith@example.com");
grUser.query();
while (grUser.next()) {
    var gr = new GlideRecord('sys_user_grmember');
    gr.initialize();
    gr.group = 'group sys_id';
    gr.user = grUser.sys_id;
    gr.insert();
}

 

 


Please Mark My Response as Correct/Helpful based on Impact
Regards,
Gunjan Kiratkar
2X ServiceNow MVP
Community Rising Star 2022
Youtube : ServiceNow Guy

View solution in original post

8 REPLIES 8

Saurabh Gupta
Kilo Patron
Kilo Patron

Hi,
You can use below code.

 

 

function addUserToGroup(usr_list,id){//usr_list: comma sepaerated sys_ids of users, id: sys_is of group
	var usersAdd = usr_list.split(",");
	var newgrpmember;
	var newgrpmemberIsPresent;
	for (var i = 0; i < usersAdd.length; i++)
	{
		newgrpmemberIsPresent = new GlideRecord('sys_user_grmember');
		newgrpmemberIsPresent.addQuery('group',id);
		newgrpmemberIsPresent.addQuery('user',usersAdd[i]+"");
		newgrpmemberIsPresent.query();
		if(!newgrpmemberIsPresent.next())
		{
			newgrpmember = new GlideRecord('sys_user_grmember');
			newgrpmember.initialize();
			newgrpmember.group =id;
			newgrpmember.user=usersAdd[i]+"";
			newgrpmember.insert();
		}
	}
}
addUserToGroup("user1sys_id,user2sys_id","groupsys_id");
addUserToGroup("user3sys_id","groupsys_id");

 

 


Thanks and Regards,

Saurabh Gupta

Gunjan Kiratkar
Kilo Patron
Kilo Patron

Hi @Shaik nabi khaj ,

Your requirement is not clear, where exactly you wanted to use that how you are getting users and groups records. But at the high level You can use below script 

var gr = new GlideRecord('sys_user_grmember');
gr.initialize();
gr.group='group sys_id';
gr.user = 'user sys_id';
gr.insert();

 


Please Mark My Response as Correct/Helpful based on Impact
Regards,
Gunjan Kiratkar
2X ServiceNow MVP
Community Rising Star 2022
Youtube : ServiceNow Guy

Hi @Gunjan Kiratkar,

I have a group called 'Azure patch' in group table.

I have a user called 'cristina altofin' in user table.

So now i want to add that user to group by using background script.

 

Best Regards,

Shaik Nabi Khaja.

Hi @Shaik nabi khaj 

Then use script provided by me.

Just copy past user and group sys_id where ever mentioned.

 


Please Mark My Response as Correct/Helpful based on Impact
Regards,
Gunjan Kiratkar
2X ServiceNow MVP
Community Rising Star 2022
Youtube : ServiceNow Guy