I have 2 users and i want to replicate the user same as the another user how can do it in the easy
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-21-2024 04:54 PM
Hi,
I have a user called "Micheal (user)" which consists of more roles an groups and i want to provide the exactly same roles and groups to Daniel (user), how can i provide it in the easiest way because by manually i need to provide almost 50 groups and 250 roles.
any alternative to achieve this.
Thanks,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-21-2024 09:09 PM
Hi @kranthi2
You can use the script to Achieve this: run the below code with user sys_id then it will work.
var sourceUserSysId = '26ba806193d70210260bb2ddfaba1067'; // Source sys_id of User
var targetUserSysId = 'fcea087993db4210260bb2ddfaba10c1'; // Target sys_id of User
// Copying Roles
var grUser = new GlideRecord('sys_user_has_role');
grUser.addQuery('user', sourceUserSysId);
grUser.query();
while (grUser.next()) {
var newUserRoleGr = new GlideRecord('sys_user_has_role');
newUserRoleGr.initialize();
newUserRoleGr.user = targetUserSysId;
newUserRoleGr.role = grUser.role;
newUserRoleGr.insert();
}
// Copying Groups
var userGroupGr = new GlideRecord('sys_user_grmember');
userGroupGr.addQuery('user', sourceUserSysId);
userGroupGr.query();
while (userGroupGr.next()) {
var newUserGroupGr = new GlideRecord('sys_user_grmember');
newUserGroupGr.initialize();
newUserGroupGr.user = targetUserSysId;
newUserGroupGr.group = userGroupGr.group;
newUserGroupGr.insert();
}
If my response proves useful, please indicate its helpfulness by selecting "Accept as Solution" and " Helpful."
Thanks,
BK

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-21-2024 10:21 PM
I'd first question why does a single user need to belong to that many groups and why roles would be assigned to them directly instead of being inherited by the groups they belong to. It becomes hard to maintain when you're managing users who belong to this many groups and assume this many roles. Running the script as mentioned works but it doesn't fix the maintainability of how your users are managed in this capacity.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-21-2024 10:25 PM
Hi @Community Alums
Thanks for your response, more useful. But my requirement is i want to use this same user for ATF hence i created the same. my boss is saying that don't use the ServiceNow user instead please use the separate user for ATF.
Is there any possibility can i use the same user in the ATF.
Please help me out
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-21-2024 10:40 PM
Hi @kranthi2
I had the similar requirement and I got it through the following Background script-
var sourceUserSysId = '4a0be************998'; // Source sys_id of User
var targetUserSysId = '2c0c*************a0'; // Target sys_id of User
function copyRecords(table, sourceField, targetField) {
var gr = new GlideRecord(table);
gr.addQuery('user', sourceUserSysId);
gr.query();
while (gr.next()) {
var newGr = new GlideRecord(table);
newGr.initialize();
newGr.user = targetUserSysId;
newGr[sourceField] = gr[sourceField];
newGr.insert();
}
}
copyRecords('sys_user_has_role', 'role', 'role');
copyRecords('sys_user_grmember', 'group', 'group');
Please mark my answer helpful and correct.
Regards,
Amit