Merging Duplicates through Script
NetSuite offers a variety of ways to deal with duplicate entities. You can setup duplicate detection to assist in stopping duplicates from being created, you can also merge records that have been identified as duplicates. Merging the records is a manual process that involves selecting the primary record as well as the records you want to merge into the primary.
Our issue occurred after a large number of leads/prospects/customers were imported into the system. We needed a quick way to merge the hundreds of records that were determined to be duplicates. We were given a list that contains the internal id of the primary record as well as the internal id of the records to be merged. You could create your own list using saved searches and/or excel. To keep this article short I will assume you have a csv file that contains your duplicates with the internal id of the Primary and secondary entities.
Step 1: Create your csv file with the primary internal id in the first column and the secondary id's in the following column. For this example I am assuming we are only merging 2 records, but this could be easily changed.
Step 2: Load your csv file into the file cabinet, remember the path it will be needed in the script.
Step 3: Review and copy the script.
/*
*@NApiVersion 2.x
*@NScriptType ScheduledScript
* Created by Mark Kreminski
* Date Created 1/12/2023
*/
define(["N/file","N/task"],
function (f,task) {
/**
* Custom module for executing N/file cookbook examples
*
* @NApiVersion 2.0
* @NModuleScope SameAccount
*/
var exports = {};
function readFile(response) {
/*
* Script to read in a csv file from the file cabinet and merge the 2 customer records
* The Primary internal id should be in the first column, the record to be merge column 2
*/
var testFile = f.load({id: "SuiteScripts/testMerge.csv"}); //** Load the file from the file cabinet
var testData = [];
testFile.lines.iterator().each(function (line) { //** Iterate through each line of the file
var cust = line.value.split(","); //** Split the file on the delimiter
log.debug('parent:child', cust[0]+":"+cust[1]);
var dedupeTask = task.create({taskType: task.TaskType.ENTITY_DEDUPLICATION}); //** Create the deduplication task
/*
* Set the record/Entity type
* Note: If you set entityType to CUSTOMER, the system will
* automatically include prospects and leads in the task request.
* The options are:
* CUSTOMER
* CONTACT
* VENDOR
* PARTNER
* LEAD
* PROSPECT
*/
dedupeTask.entityType = task.DedupeEntityType.CUSTOMER; //** Set the record/entity type
/*
* Set the De-Duplication mode (what action should be taken
* The options are:
* MERGE
* DELETE
* MAKE_MASTER_PARENT
* MARK_AS_NOT_DUPES
*/
dedupeTask.dedupeMode = task.DedupeMode.MERGE; //** set the deduplication mode
/*
* Set the master selection mode. The options are:
* CREATED_EARLIEST
* MOST_RECENT_ACTIVITY
* MOST_POPULATED_FIELDS
* SELECT_BY_ID
*/
dedupeTask.masterSelectionMode = task.MasterSelectionMode.SELECT_BY_ID;
/*
* Set the master record id, in this case the value from column 1
*/
dedupeTask.masterRecordId = cust[0];
/*
* List the internal id's of the records to be merged
* In this instance columns 1 and 2 from our csv file
* more value could be added
*/
dedupeTask.recordIds = [cust[0], cust[1]];
/*
* Submit the merger task
*/
var dedupeTaskId = dedupeTask.submit();
return true;
});
}
return{
execute:readFile
}
});*
step 4: Update the following line of the script to point to the file you uploaded in step 2.
Recommended by LinkedIn
var testFile = f.load({id: "SuiteScripts/testMerge.csv"}); //** Load the file from the file cabinet
Step 5: Upload the script to your NetSuite file cabinet
Step 6: Create and deploy the new script
Step 7: Run the script
This was just a quick sample, the following improvements are planned for the near future.
As always if you have any questions or comments, please feel free to send me a message or comment on this article.
Hi there, I'm attempting to use this process but when I try to create the script, I'm getting this error: @NScriptType is mandatory for SuiteScript 2.x entry point scripts. Any suggestions on incorporating the appropriate 2.x entry point?