What Should I Do to Make Duplicates Migration to Salesforce Classic Successful?

  1. Home
  2. Knowledge Base
  3. Salesforce
  4. What Should I Do to Make Duplicates Migration to Salesforce Classic Successful?

Data2CRM handles the migration of duplicates to Salesforce for the following modules:

  1. Accounts
  2. Contacts
  3. Leads

To have them migrated successfully to Salesforce Classic, you should proceed with some steps in your Salesforce Classic platform.

Firstly, you should log in to your Salesforce Classic platform. Then, choose Setup button :

setup

Then, choose Develop on the left sidebar:

Step 2

Then, choose Apex Triggers from a drop-down list on the left sidebar:

Apex triggers

Then, press the Developer console button. When the Developer console window will open - press File -> New -> Apex Trigger:

apex trig

Then, the window New Apex Trigger will open, and you should name it "AccountDuplicateTrigger" and choose the sObject Account from a drop-down list; and press Submit:

Step 3

Then, the window with the initial trigger code will open - you should replace it with a new code (we will provide the code below so you can copy it and paste into that window):

selection

code ppts

The code you should copy for the Accounts:

trigger AccountDuplicateTrigger on Account (before insert, before update) {
    try {
        for (Account account : Trigger.new) {
            List<Account> accountList = new List<Account>();
            accountList.add(account);

            Datacloud.FindDuplicatesResult[] results = Datacloud.FindDuplicates.findDuplicates(accountList);

            List<String> duplicateRecords = new List<String>();

            for (Datacloud.FindDuplicatesResult findDupeResult : results) {
                for (Datacloud.DuplicateResult dupeResult : findDupeResult.getDuplicateResults()) {
                    for (Datacloud.MatchResult matchResult : dupeResult.getMatchResults()) {
                        for (Datacloud.MatchRecord matchRecord : matchResult.getMatchRecords()) {
                            duplicateRecords.add(matchRecord.getRecord().Id);
                        }
                    }
                }
            }

            if (duplicateRecords.size() != 0) {
                account.addError('Duplicate! Records Ids: ' + string.join(duplicateRecords, ','));
            }
        }
    } catch(exception e) {
        // No active duplicate rules are defined for the Account object type
    }
}

Note: make sure you copied the code correctly - it affects your CRM directly.

And, finally, make sure to Save the new trigger code:

Save Code

To check if the new trigger was saved, return to the Apex Triggers to see it (it will have the name you gave it - AccountDuplicateTrigger) :

check code

To proceed with the successful migration of Contacts duplicates, you should repeat the same steps, but name the new trigger "ContactDuplicateTrigger" and choose from a drop-down list the sObject Contact.

The code you should copy and paste in the trigger code window is the following:

trigger ContactDuplicateTrigger on Contact (before insert, before update) {
    try {
        for (Contact contact : Trigger.new) {
            List<Contact> contactList = new List<Contact>();
            contactList.add(contact);

            Datacloud.FindDuplicatesResult[] results = Datacloud.FindDuplicates.findDuplicates(contactList);
           
            List<String> duplicateRecords = new List<String>();
           
            for (Datacloud.FindDuplicatesResult findDupeResult : results) {
                for (Datacloud.DuplicateResult dupeResult : findDupeResult.getDuplicateResults()) {
                    for (Datacloud.MatchResult matchResult : dupeResult.getMatchResults()) {
                        for (Datacloud.MatchRecord matchRecord : matchResult.getMatchRecords()) {
                            duplicateRecords.add(matchRecord.getRecord().Id);
                        }
                    }
                }
            }
           
            if (duplicateRecords.size() != 0) {
                contact.addError('Duplicate! Records Ids: ' + string.join(duplicateRecords, ','));
            }
        }
    } catch(exception e) {}
}

To proceed with the successful migration of Leads duplicates, you should repeat the same steps, but name the new trigger "LeadDuplicateTrigger" and choose from a drop-down list the sObject Lead.

The code you should copy and paste in the trigger code window is the following:

trigger LeadDuplicateTrigger on Lead (before insert, before update) {
    try {
        for (Lead lead : Trigger.new) {
            List<Lead> leadList = new List<Lead>();
            leadList.add(lead);

            Datacloud.FindDuplicatesResult[] results = Datacloud.FindDuplicates.findDuplicates(leadList);
           
            List<String> duplicateRecords = new List<String>();
           
            for (Datacloud.FindDuplicatesResult findDupeResult : results) {
                for (Datacloud.DuplicateResult dupeResult : findDupeResult.getDuplicateResults()) {
                    for (Datacloud.MatchResult matchResult : dupeResult.getMatchResults()) {
                        for (Datacloud.MatchRecord matchRecord : matchResult.getMatchRecords()) {
                            duplicateRecords.add(matchRecord.getRecord().Id);
                        }
                    }
                }
            }
           
            if (duplicateRecords.size() != 0) {
                lead.addError('Duplicate! Records Ids: ' + string.join(duplicateRecords, ','));
            }
        }
    } catch(exception e) {}
}

If you still cannot do it by yourself or something is not working for you, feel free to contact our CRM expert to walk you through all the steps or to provide the tech help for you.

Was this article helpful?

Related Articles

Para seguir utilizando este sitio web, debe permitir las cookies de acuerdo con nuestra Política de cookies

×