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  ||  ✨ 10x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

9 REPLIES 9

Ankur Bawiskar
Tera Patron

Hi,

Is it inserting empty row in that table?

Did you check by adding logs you are getting user sys_id

Also it is best practice to give role to group rather than user in this case.

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  ||  ✨ 10x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi Ankur,

There are only two fields on excel sheet which are Group & User which is same as on target table sys_user_grmember. I have tried to add user's are creating and they are assigned to specific group as well but roles are not getting assigned I need to assigned roles once group members are added to groups.

Thanks

Chaitanya

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  ||  ✨ 10x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi Ankur, thanks for suggestion I have tried below script on group transform map. it worked

(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {
 
 var groupsysid=new GlideRecord('sys_user_group');
 groupsysid.get('name',source.u_name);
   var gr = new GlideRecord('sys_group_has_role');
 gr.addQuery('group',groupsysid.sys_id);
 gr.addQuery('role','282bf1fac6112285017366cb5f867469');
 gr.query();
 if(!gr.hasNext()){
   gr.initialize();
   gr.group=groupsysid.sys_id;
   gr.role='282bf1fac6112285017366cb5f867469';
   gr.insert();
 }
})(source, map, log, target);

You are welcome

Regards
Ankur

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