Merge Bulk Location Records
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-01-2024 01:13 PM
Hello Everyone,
I've a requirement to Merge Location Records. Below script is updating the single record as expected, however i have 600+ such locations. Can you show some light on how can i do bulk merging of location.
Below is the code i've used for single record update.
var sourceLocationSysId = '99deb511db257c50c4cb1619139619bd';
var targetLocationSysId = '7a3a884997e88ed4a61830b6f053af90';
var sourceLocation = new GlideRecord('cmn_location');
sourceLocation.get(sourceLocationSysId);
var targetLocation = new GlideRecord('cmn_location');
targetLocation.get(targetLocationSysId);
for (var fieldName in sourceLocation) {
if (fieldName.startsWith('sys_') || fieldName == 'sys_id') {
continue;
}
if (gs.nil(targetLocation[fieldName])) {
targetLocation[fieldName] = sourceLocation[fieldName];
}
}
targetLocation.update();
var tablesToUpdate = ['cmn_location'];
for (var i = 0; i < tablesToUpdate.length; i++) {
var table = tablesToUpdate[i];
var relatedGR = new GlideRecord(table);
relatedGR.addQuery('location', targetLocationSysId);
relatedGR.query();
while (relatedGR.next()) {
relatedGR.location = sourceLocationSysId;
relatedGR.update();
}
}
sourceLocation.u_active = false;
sourceLocation.update();
Regards,
Arun
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-02-2024 01:17 AM
You are updating based on unique values, defining the source and target. Without having more information on how you decide which location should be the target and which the source and how you define which locations need to be merged, it's impossible to get a good script for this.
If there is logic to it, you can apply it by dynamically setting the sys_ids. For instance: find all locations with the same postal code, get the oldest one as target and merge the others to this one as source.
Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark