Skip to content

Commit 48480f0

Browse files
committed
better Readme for CloneUser
1 parent 452ffcf commit 48480f0

File tree

2 files changed

+54
-1
lines changed
  • Server-Side Components/Background Scripts/Clone User with Roles and Groups
  • Specialized Areas/Fix scripts/Bulk Update User Time Zones

2 files changed

+54
-1
lines changed
Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,29 @@
11
# Clone User with Roles and Groups
2-
I have created a script in ServiceNow to replicate a user's profile. This script not only duplicates the user's data but also replicates the roles and groups assigned to that user.
2+
3+
A background script that clones an existing user's profile including all their roles and group memberships to a new user account.
4+
5+
## Usage
6+
7+
1. Navigate to **System Definition → Scripts - Background**
8+
2. Copy and paste the script content
9+
3. Update the function call at the bottom with the source and target user IDs:
10+
```javascript
11+
cloneUser('source.username', 'new.username');
12+
```
13+
4. Click "Run script"
14+
15+
## What It Does
16+
17+
The script:
18+
1. Creates a new user record with the specified username
19+
2. Copies all field values from the source user to the new user (except fields already set)
20+
3. Clones all directly assigned roles (excludes inherited roles)
21+
4. Clones all group memberships
22+
5. Returns the sys_id of the newly created user
23+
24+
## Use Cases
25+
26+
- Onboarding new team members with similar access needs
27+
- Creating test users with specific role/group combinations
28+
- Setting up backup user accounts with identical permissions
29+
- Standardizing user setup based on role templates
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Variant: Update users by group name instead of sys_id
2+
3+
var GROUP_NAME = 'Service Desk';
4+
var NEW_TIME_ZONE = 'America/Chicago';
5+
6+
var groupGR = new GlideRecord('sys_user_group');
7+
groupGR.addQuery('name', GROUP_NAME);
8+
groupGR.query();
9+
if (groupGR.next()) {
10+
var groupId = groupGR.sys_id.toString();
11+
var grMember = new GlideRecord('sys_user_grmember');
12+
grMember.addQuery('group', groupId);
13+
grMember.query();
14+
var count = 0;
15+
while (grMember.next()) {
16+
var user = new GlideRecord('sys_user');
17+
if (user.get(grMember.user)) {
18+
user.time_zone = NEW_TIME_ZONE;
19+
user.update();
20+
count++;
21+
}
22+
}
23+
gs.print('Updated time zone for ' + count + ' users in group "' + GROUP_NAME + '"');
24+
} else {
25+
gs.print('Group not found: ' + GROUP_NAME);
26+
}

0 commit comments

Comments
 (0)