How to give roles for bulk groups while importing group memebers on group member table

Chaitanya Redd1
Tera Guru

Hello Team,

I'm trying to import group members to sys_user_grmember table through transform map but here I want to assign roles to group using transform script but it's not working can anyone help me how to assign roles.

OnAfter 

(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {
 
   var gr = new GlideRecord('sys_user_has_role');

   gr.initialize();

   gr.user = getUserSysId(source.u_email_id); //based on email Id

   gr.role = '282bf1fac6112285017366cb5f867469'; 

   gr.insert();
 
   function getUserSysId(emailId)

   {

   var userGr = new GlideRecord('sys_user');

   userGr.addQuery('email', emailId);

   userGr.query();

   if(userGr.next())

   {

   return userGr.sys_id;
 
   }
 
   }
 
})(source, map, log, target);
 
 
Thanks,
Chaitanya
1 ACCEPTED SOLUTION

Hi,

then why not give role to group in onAfter transform script instead of members

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

View solution in original post

9 REPLIES 9

Dhruv Gupta1
Kilo Sage
Kilo Sage

Always assign role to group. Never assign role directly to user. Add users to a group and then assign the role.

 

In case your requirement is to assign role to multiple group kindly consider the below script.

 

 

For this you need to use the Background script.

 

Please activate your elevated privilege (i.e., the 'security_admin' role)-->Navigate to System Definition--> Scripts - Background-->and use the below sample code as per your requirement.

 

 

var gr = new GlideRecord('sys_group_has_role');



gr.addEncodedQuery('group.nameLIKEcatalog'); //It fetches all the group which contains 'catalog' in it's name. Please use your requiremnent.



gr.query();



while(gr.next()){



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



gr.update();



}

 

Hi Dhruv,

I have inserted groups in different data source and now loading group members based on groups and I know that we have assign roles to groups not to user that's why I was trying to write Transform Script On After which can assign roles to groups. I have tried your script but didn't worked.

 

Thanks

Hi Chaitanya,

So where you want to give role

you have 1 data source which loads groups

you have 1 data source which loads group members

where have you written the transform map onAfter? is it for data source of group or members

Regards
Ankur

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

Hi Ankur,

I need to give roles on Group member transform map scripts.

Thanks,

Chaitanya