Skip to content

Commit c446a8e

Browse files
committed
script to update all email domains
1 parent 7441ac6 commit c446a8e

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Update User Email Domain in ServiceNow
2+
3+
This script finds all users in the **`sys_user`** table whose email addresses contain an old domain (e.g. `bad_domain.com`) and replaces it with a new domain (e.g. `new_domain.com`) using **regular expressions**.
4+
5+
---
6+
7+
## Purpose
8+
9+
To bulk–update user email domains safely and efficiently without manual edits.
10+
11+
Example use case:
12+
When your organization migrates from `@bad_domain.com` to `@new_domain.com`, this script updates all users automatically.
13+
14+
---
15+
16+
## Example
17+
| Old Email | New Email |
18+
|-------------------------|------------------------|
19+
| alice@bad_domain.com | alice@new_domain.com |
20+
| bob@bad_domain.com | bob@new_domain.com |
21+
| carol@bad_domain.com | carol@new_domain.com |
22+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Use: Fix script to update all email domains in sys_user table
2+
// Replaces 'bad_domain.com' with 'new_domain.com' in email field
3+
4+
var usr = new GlideRecord('sys_user');
5+
usr.addEncodedQuery('emailLIKEbad_domain.com'); // find users with old domain
6+
usr.query();
7+
8+
while (usr.next()) {
9+
gs.print('OLD EMAIL: ' + usr.email);
10+
11+
// Create regex to match old domain
12+
var regex = new SNC.Regex('bad_domain\\.com'); // uses '\\' to escape dot for regex
13+
// Use replaceAll method to substitute
14+
usr.email = regex.replaceAll(usr.email, 'new_domain.com');
15+
usr.update();
16+
gs.print('NEW EMAIL: ' + usr.email);
17+
}

0 commit comments

Comments
 (0)