Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Merge Duplicate User Records Automatically


Automatically detects and merges duplicate User records in ServiceNow based on the email address.

Ensures data integrity and prevents duplicate entries.

Reassigns related records (e.g., incidents) to a master record.

Deactivates duplicate users.

In this UseCase
Multiple users exist with the same email address.

The first record found is treated as the master user.

All duplicates are: Reassigned in related records (e.g., tasks, incidents).

Marked inactive.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
(function() {
var userEmailMap = {};
var userGR = new GlideRecord('sys_user');
userGR.addNotNullQuery('email');
userGR.addQuery('active', true);
userGR.query();

var duplicatesFound = 0;

while (userGR.next()) {
var email = userGR.email.toLowerCase();

if (!userEmailMap[email]) {
// First user with this email
userEmailMap[email] = userGR.sys_id.toString();
} else {
// Duplicate found then merge with master
duplicatesFound++;
var masterSysId = userEmailMap[email];

// Reassign related incidents
var incGR = new GlideRecord('incident');
incGR.addQuery('caller_id', userGR.sys_id);
incGR.query();
var incidentsReassigned = 0;
while (incGR.next()) {
incGR.caller_id = masterSysId;
incGR.update();
incidentsReassigned++;
}

// Deactivate duplicate
userGR.active = false;
userGR.u_merged_to = masterSysId; // optional custom field
userGR.update();

gs.info("Merged duplicate user '" + userGR.name + "' into master. Reassigned " + incidentsReassigned + " incidents.");
}
}

gs.info("Total duplicates merged: " + duplicatesFound);

})();
Loading