How to copy the role from one user to another user

Sagaya1
Giga Expert

Hi ,

I have a user 'A' which mapped 80 roles . Now need to create a new user 'B' and  copy the same roles which has available in role 'A' ?

Steps

Export the user role to XML file .

find_real_file.png

2. Import the XML in user 'B'.

find_real_file.png

3. Getting the below error.

 

find_real_file.png

 

Please help to fix this issue.

 

Regards,

Sagaya.

 

 

 

 

 

 

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Sagaya 

It's not straight forward for XML export/import.

you will have to run a script for this either one time schedule job or fix script

Sample below

createUserRoles();

function createUserRoles(){

var gr = new GlideRecord('sys_user_has_role');

gr.addQuery('user', 'userASysId');

gr.query();

while(gr.next()){

var gr1 = new GlideRecord('sys_user_has_role');

gr1.initialize();

gr1.user = 'userB SysId';

gr1.role = gr.role;

gr1.insert();

}

}

Regards
Ankur

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

View solution in original post

2 REPLIES 2

Shubham Bongulw
Giga Guru

Hi Sagaya,

Sharing you the link of similar query which has two different solution, it might help you-

How to copy a role

 

Warm Regards,

Shubham Bongulwar

Please mark helpful or correct if it helps!!!

Ankur Bawiskar
Tera Patron
Tera Patron

@Sagaya 

It's not straight forward for XML export/import.

you will have to run a script for this either one time schedule job or fix script

Sample below

createUserRoles();

function createUserRoles(){

var gr = new GlideRecord('sys_user_has_role');

gr.addQuery('user', 'userASysId');

gr.query();

while(gr.next()){

var gr1 = new GlideRecord('sys_user_has_role');

gr1.initialize();

gr1.user = 'userB SysId';

gr1.role = gr.role;

gr1.insert();

}

}

Regards
Ankur

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