Assigning a role to multiple group.

Shyna1
Tera Contributor

Hi,

I need to add a role called 'abc_grp' to all the groups of type = approval . 

Can someone help with a sanitized code.

 

 

2 REPLIES 2

Murthy Ch
Giga Sage

@Shyna 

You can try this:

try
{
var gr= new GlideRecord('sys_user_group');
gr.addEncodedQuery('typeLIKE1cb8ab9bff500200158bffffffffff62');  //change your encoded query
gr.query();
gs.info(gr.getRowCount());
while(gr.next())
{
var grI= new GlideRecord('sys_group_has_role');
grI.initialize();
grI.group=gr.getUniqueValue();
grI.role='2a7a86c31ba84950558d639bbc4bcb55';  //map your role sys_id
grI.insert();
}
}
catch(e)
{
gs.info('Error is:'+e.message);
}

Hope it helps

 

Thanks,

Murthy

Thanks,
Murthy

Saurav11
Kilo Patron
Kilo Patron

Hi Shyna,

Background Script will help you to accomplish this task.

Navigate to System Definition--> Scripts - Background-->and use the below sample code as per your requirement.

var grp = new GlideRecord('sys_user_group');

grp.addEncodedQuery('typeLIKE1cb8ab9bff500200158bffffffffff62'); // mention the sys_ids of type approval

//you can also comment above line of code if you want it to be run for all groups.


grp.query();

while(grp.next()){

var gr = new GlideRecord('sys_group_has_role');

gr.Initialize();

gr.group = grp.sys_id;


gr.role = '974381f9538823004247ddeeff7b1267';   //Pass the sys_id of the role which you want to add

gr.insert();

}

 

Please mark correct/helpful as per the impact.

Thanks.