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.


 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.

  • Add script parameters to contain the file path of the csv file
  • Additional logging
  • Additional error checking and notifications


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?

Like
Reply

To view or add a comment, sign in

More articles by Mark Kreminski

  • Exploring NetSuite + AI: My Early Impressions and Experiments

    After tuning into the keynotes and presentations from SuiteWorld, one thing is crystal clear—NetSuite is diving deep…

  • NetSuite Customer Dashboards

    You can add a Custom Customer Dashboard for viewing important customer information. Each user can setup their own…

  • Navigating Success: The Role of Executive Dashboards in NetSuite

    In today’s world having a clear understanding of your organization’s performance is essential. With the advent of…

  • NetSuite Project 360 Dashboards

    NetSuite Projects 360 Dashboard New dashboard within NetSuite SuiteProjects brings an additional layer of visibility…

  • NetSuite Scripted Records

    What are Scripted Records Scripted Records are any record in NetSuite that has a client script, user event script or…

  • Getting Started with NetSuite part 1

    Getting Started with NetSuite Logging in You should have received an email from either the support desk or from…

  • Gathering Customer Information

    Online Forms, Suitelets and Processing Records Online Custom Record Forms • An online custom record form is used to…

    1 Comment
  • Saved Searches limiting results

    Limiting Saved Search Results We recently had a request to create a saved search that monitor the number of emails…

  • Limits and Governance

    Overcoming Scheduled Script Limitations Recently when creating a scheduled script I ran into 2 limitations. Overcoming…

    1 Comment
  • Suitelets, HTML and Custom Pages

    Did you know that you can create your own NetSuite pages by combining Suitelets, Javascript and HTML? Well you can and…

    3 Comments

Others also viewed

Explore content categories