From c446a8e600e7ea40273398400d70a7fc7f594bca Mon Sep 17 00:00:00 2001 From: Sumith Thota <108344062+SumithThota@users.noreply.github.com> Date: Tue, 21 Oct 2025 17:36:53 +0530 Subject: [PATCH] script to update all email domains --- .../Mass Email Domain Update/README.md | 22 +++++++++++++++++++ .../Mass Email Domain Update/script.js | 17 ++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 Specialized Areas/Fix scripts/Mass Email Domain Update/README.md create mode 100644 Specialized Areas/Fix scripts/Mass Email Domain Update/script.js diff --git a/Specialized Areas/Fix scripts/Mass Email Domain Update/README.md b/Specialized Areas/Fix scripts/Mass Email Domain Update/README.md new file mode 100644 index 0000000000..7200a2e55e --- /dev/null +++ b/Specialized Areas/Fix scripts/Mass Email Domain Update/README.md @@ -0,0 +1,22 @@ +# Update User Email Domain in ServiceNow + +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**. + +--- + +## Purpose + +To bulk–update user email domains safely and efficiently without manual edits. + +Example use case: + When your organization migrates from `@bad_domain.com` to `@new_domain.com`, this script updates all users automatically. + +--- + +## Example +| Old Email | New Email | +|-------------------------|------------------------| +| alice@bad_domain.com | alice@new_domain.com | +| bob@bad_domain.com | bob@new_domain.com | +| carol@bad_domain.com | carol@new_domain.com | + diff --git a/Specialized Areas/Fix scripts/Mass Email Domain Update/script.js b/Specialized Areas/Fix scripts/Mass Email Domain Update/script.js new file mode 100644 index 0000000000..0cd87ce7c7 --- /dev/null +++ b/Specialized Areas/Fix scripts/Mass Email Domain Update/script.js @@ -0,0 +1,17 @@ +// Use: Fix script to update all email domains in sys_user table +// Replaces 'bad_domain.com' with 'new_domain.com' in email field + +var usr = new GlideRecord('sys_user'); +usr.addEncodedQuery('emailLIKEbad_domain.com'); // find users with old domain +usr.query(); + +while (usr.next()) { + gs.print('OLD EMAIL: ' + usr.email); + + // Create regex to match old domain + var regex = new SNC.Regex('bad_domain\\.com'); // uses '\\' to escape dot for regex + // Use replaceAll method to substitute + usr.email = regex.replaceAll(usr.email, 'new_domain.com'); + usr.update(); + gs.print('NEW EMAIL: ' + usr.email); +}