How to use Duplicate Check with a Custom Object in Salesforce Lightning

Last published at: 2023-01-02 15:04:21 UTC
Delete

The use of custom objects in Duplicate Check is available in the Advanced and Premium editions.

Delete

In this article, find out how to integrate Duplicate Check in your custom object. This tutorial is for Salesforce Lightning. If you are using Salesforce Classic please see this article.

Step 1 Add Custom Object to DC Setup

  1. Navigate to the DC Setup Page and under 'Object Setup' click on 'Add Object'.
  2. In the list that appears, choose the Custom Object you wish to use. In this example, we are using the custom object 'Price'.


  3. After clicking 'Add', the object will appear in the Object Setup.

Delete

After adding the custom object to the setup, create a scenario to define a strategy for finding duplicate records.

Step 2 Create a custom DC Check button in Salesforce Lightning (optional)

The DC Check button allows you to do a search from the record page layout. After the button is clicked, Duplicate Check will search for duplicate records for that particular record you're working from. Creating the DC Check button is not mandatory. 

  1. Navigate to the Salesforce Setup - Object Manager.
  2. In the Object Manager, find your custom object. Under 'Details' copy the 'API name' of the custom object. 
  3. Navigate to the 'Buttons, Links and Actions' menu.
  4. On the top-right of the page, click 'New Button or Link'.
  5. Fill out the form using the details in the example below. Copy and paste the code below.

    In this example, we are using 'Price' as the *Custom Object*:
    Replace *API_NAME* with the API name that corresponds with your custom Object.
    {!URLFOR('/apex/dupcheck__dc3Check?Id='+*API_NAME*.Id)}

  6. Click 'Save'. Your DC Check button is now created.

Step 3 Add the DC Check button to your Custom Object Layout (optional)

  1. To add the DC Check custom button to your custom object page layout navigate to the Salesforce Setup  menu.
  2. Navigate to the Object Manager.
  3. Now, find and open the Custom Object.
  4. Navigate to the 'Page Layouts' menu and open the page layout you want to add the DC Check button to. 
  5. From the 'Mobile & Lightning Actions' menu, drag and drop the 'DC Check' button to the custom button section below, which will be highlighted in green when you drag the button.

    Click here to see a short video of this step
  6. Click 'Save'. You have added the DC Check Custom button to your custom object. Here is an example of how it looks added to the custom object 'Price':

Step 4 Create the DC Merge button in Salesforce Lightning (optional)

  1. Navigate to the Salesforce Setup - Custom Code - Visualforce Pages.
  2.  Click 'New'. 
  3. At 'Label', enter a value, for example, "DC Merge Price". 
  4. At 'Available for Lightning Experience, Lightning Communities, and the mobile app', set the checkbox to 'true'.
  5. At 'Visualforce Markup', replace all contents with the markup mentioned below. Make sure to replace "*Object__c*" with the API name of your Custom Object.
    <apex:page standardController="*Object__c*" extensions="dupcheck.dc3ControllerMergeList" recordSetVar="records" docType="html-5.0" sidebar="false">
        <dupcheck:dc3MergeList records="{!allRecords}" selected="{!selectedRecords}"/>
    </apex:page>
  6. Press Save. 
  7. Now navigate to the Object Manager - Custom object - Buttons, Links, and Actions.
  8.  Click 'New Button or Link'. 
  9. At 'Label' enter a value, for example, "DC Merge". This is the label of the button the users see in the User Interface.
  10. At 'Display type', choose 'List Button'. The checkbox at 'Display checkboxes' should be set to 'true'. 
  11. At 'Behavior', choose 'Display in existing window with sidebar'.
  12.  At 'Content source', choose 'Visualforce Page'.
  13. At 'Content', find the new Visualforce Page we just created. 
  14. Press Save. 

Step 5 Add the 'DC Merge' button to your List View (optional)

  1. Navigate to the Salesforce Setup > Object Manager > your custom object > List View Button Layout
  2. At the list view you want to add the button to, click Edit
  3. Move the 'DC Merge' button from Available Buttons to Selected Buttons
  4. Click Save.

Step 6 Create a 'Disable Duplicate Check' checkbox in Salesforce Lightning (mandatory)

  1. Navigate to the Salesforce Setup - Object Manager. 
  2. Find the custom object you want to use. Under 'Details' copy the API name of the *Object*.
  3. Navigate to the 'Fields & Relationships' page.
  4.  On the top right click on the 'New' button.
  5. Under 'Data Type' select the 'Checkbox' and then click 'Next'
  6. Enter the following settings and click 'Next'


    Field Label: Disable Duplicate Check
    Field Name: dc3DisableDuplicateCheck

  7. Depending on your Salesforce edition (Professional and up), you will now get a screen to establish field-level security. Decide who may see the Disable Duplicate Check button and click 'Next'. 
  8. Depending on your Salesforce edition (Professional and up), you will now get a screen where you are able to add the Disable Duplicate Check button to the page layout. Add the button to the pages you would like. 
  9. Click 'Save'. Your 'Disable Duplicate Check' checkbox is now created.


Step 7 Create the Apex Trigger (mandatory)

Delete

As per Salesforce policy, you first need to create the Apex Trigger and Test Class in your sandbox and then deploy from sandbox to production.

  1. Go to the Salesforce Setup > Object Manager.
  2. In the Object Manager, find the custom object you want to use and click on it. 
  3. Under Details, copy the API Name of the Object.
  4. At the left, go to the Triggers tab.
  5. At top right, click New.
  6. Replace all code in the box with the Apex Trigger code below. Make sure you replace *OBJECT_NAME* and *OBJECT_API_NAME* with the name and API name of your Custom Object.

    The Test Class part should be created as an Apex Class (Salesforce Setup > Custom Code > Apex Classes

    Click here to watch a short video of this step.

    trigger dc3*OBJECT_NAME*Trigger on *OBJECT_API_NAME*(after delete, after insert, after undelete, after update, before insert, before update) { 
    
    
        dupcheck.dc3Trigger triggerTool = new dupcheck.dc3Trigger(trigger.isBefore, trigger.isAfter, trigger.isInsert, trigger.isUpdate, trigger.isDelete, trigger.isUndelete);
        String errorString = triggerTool.processTrigger(trigger.oldMap, trigger.new); 
    
    
        if (String.isNotEmpty(errorString)) { trigger.new[0].addError(errorString,false); } 
    }
    // TEST CLASS BELOW
    @isTest
    private class TestDC*OBJECT_NAME* {
           static testMethod void Test_Insert*OBJECT_NAME*() {
        *OBJECT_API_NAME* p = new *OBJECT_API_NAME*();
        // Add all fields which are required to insert the record. For example;
        // p.Name = 'Testprice';
        // p.Price__c = 110.0;    
                insert p;
        system.assertEquals(true, p.Id != null);
       }
    }
    Delete

    This is an example of the code with the custom object 'Price'.

    trigger dc3PriceTrigger on PRICE__c(after delete, after insert, after undelete, after update, before insert, before update) { 
    
    
        dupcheck.dc3Trigger triggerTool = new dupcheck.dc3Trigger(trigger.isBefore, trigger.isAfter, trigger.isInsert, trigger.isUpdate, trigger.isDelete, trigger.isUndelete);
        String errorString = triggerTool.processTrigger(trigger.oldMap, trigger.new); 
    
    
        if (String.isNotEmpty(errorString)) { trigger.new[0].addError(errorString,false); } 
    }
    // TEST CLASS BELOW
    
    
    @isTest
    private class TestDCPrice {
           static testMethod void Test_InsertPrice() {
        Price__c p = new Price();
        // Add all fields which are required to insert the record. For example;
        // p.Name = 'Testprice';
        // p.Price__c = 110.0;    
                insert p;
        system.assertEquals(true, p.Id != null);
       }
    }
  7. Click 'Save'. The Trigger is created successfully.

Delete

As per Salesforce policy, you first need to create the Apex Trigger and Test Class in your sandbox and then deploy from sandbox to production.

Step 8 Enable and create the Search Index (mandatory)

See Search Index‍ for more detailed information about the Search Index.

  1. Go to DC Setup.
  2. On the left hand side, under Object Setup, select the Object you want to create a Search Index for.
  3. On the *Object* Settings tab, enable the Search Index option.
  4. At top right, enable Show Advanced Settings.
  5. Scroll down to the Index section and configure the Search Index settings.
    1638439476246-cf3134a770c3ffbcbf4b3813ddd7644e6fcd3b58 (1).png
  6. Go to the Index Batch tab.
  7. Click the Start button and select Create Search Index.

Creating the Search Index might take some time.

Step 9 Add fields for DC Job results export

To be able to export DC Job results of a job that ran on your custom object, add two fields to the Duplicate Check Duplicate object. See DC Job Export for Custom Objects‍ on how to set this up.

Delete

Now, make sure to create a scenario and apply the DC Check features to it. Learn more about scenarios in this article.