How can I optimize a Salesforce duplicate report?

187    Asked by Dadhijaraj in Salesforce , Asked on May 24, 2024

 I have been asked for a particular task that is related to the optimization of a salesforce duplicate report for a particular sales team. How can I approach this particular task considering the workflow of my team, data integrity needs, and the potential impact on the productivity of the sales team? 

 In the context of Salesforce, here are the steps given:-

Identify the duplicate criteria

First, you would need to determine the criteria for identifying duplicates such as e-mail address, phone number, etc.

Use duplicate rules

Now you can create and configure the duplicate rules in the Salesforce for the purpose of preventing the creation of the duplicate records based on your identified criteria. You can try to specify actions to take when duplicates are detected.

Using the matching rules

Try to define the matching rules that can specify how Salesforce compares records to identify the duplicates. You can use the standard matching rules or create custom ones based on your specific needs.

Running duplicate jobs

You can try to schedule or manually run duplicate jobs in Salesforce to find and also merging the existing duplicate records based on your Configured duplicate and matching rules.

Customize the duplicate report

You can customize the duplicate report in Salesforce to display the relevant information about the detected duplicates, including fields like record IDs, duplicate rules names, and duplicate scores.

Here is the example given of how you can use the approach of the coding by using apex trigger for automating the duplicate merging:-

Trigger MergeDuplicates on Lead (after insert, after update) {
    List leadsToMerge = new List();
    // Iterate through newly inserted or updated leads
    For (Lead lead : Trigger.new) {
        // Check if lead meets criteria for duplicate merging
        If (lead.Email != null && lead.FirstName != null && lead.LastName != null) {
            List potentialDuplicates = [SELECT Id FROM Lead WHERE Email = :lead.Email AND FirstName = :lead.FirstName AND LastName = :lead.LastName AND Id != :lead.Id];
            If (!potentialDuplicates.isEmpty()) {
                // Add lead to the list for merging
                leadsToMerge.add(lead);
            }
        }
    }
    // Merge duplicate leads
    If (!leadsToMerge.isEmpty()) {
        Database.MergeResult[] mergeResults = Database.merge(leadsToMerge, false);
        // Handle merge results and log any errors or successes
    }
}

Here is the example given in java programming language –

Import com.sforce.soap.enterprise.EnterpriseConnection;
Import com.sforce.soap.enterprise.SaveResult;
Import com.sforce.soap.enterprise.sobject.Lead;
Import com.sforce.ws.ConnectorConfig;
Public class SalesforceDuplicateManagement {
    Private static final String USERNAME = “your_username”;
    Private static final String PASSWORD = “your_password”;
    Private static final String SECURITY_TOKEN = “your_security_token”;
    Private static final String AUTHENDPOINT = https://login.salesforce.com/services/Soap/c/52.0;
    Public static void main(String[] args) {
        ConnectorConfig config = new ConnectorConfig();
        Config.setUsername(USERNAME);
        Config.setPassword(PASSWORD + SECURITY_TOKEN);
        Config.setAuthEndpoint(AUTHENDPOINT);
        Try {
            EnterpriseConnection connection = new EnterpriseConnection(config);
            // Query Salesforce for leads with potential duplicates
            String query = “SELECT Id, Email, FirstName, LastName FROM Lead WHERE Email != null AND FirstName != null AND LastName != null”;
            QueryResult queryResult = connection.query(query);
            SObject[] records = queryResult.getRecords();
            // Iterate through leads and check for duplicates
            For (SObject record : records) {
                Lead lead = (Lead) record;
                String email = lead.getEmail();
                String firstName = lead.getFirstName();
                String lastName = lead.getLastName();
                // Example logic to identify and merge duplicates
                If (email != null && firstName != null && lastName != null) {
                    // Query Salesforce for potential duplicates based on criteria
                    String duplicateQuery = “SELECT Id FROM Lead WHERE Email = ‘” + email + “’ AND FirstName = ‘” + firstName + “’ AND LastName = ‘” + lastName + “’”;
                    QueryResult duplicateResult = connection.query(duplicateQuery);
                    SObject[] duplicateRecords = duplicateResult.getRecords();
                    // Merge duplicate leads
                    If (duplicateRecords.length > 1) {
                        Id[] idsToMerge = new Id[duplicateRecords.length];
                        For (int I = 0; I < duplicateRecords xss=removed xss=removed>

Here is the example given in python programming language:-

From simple_salesforce import Salesforce
# Salesforce credentials
Username = ‘your_username’
Password = ‘your_password’
Security_token = ‘your_security_token’
Domain = ‘login’ # Or use ‘test’ for sandbox
# Connect to Salesforce
Sf = Salesforce(username=username, password=password, security_token=security_token, domain=domain)
# Query Salesforce for leads with potential duplicates
Query = “SELECT Id, Email, FirstName, LastName FROM Lead WHERE Email != null AND FirstName != null AND LastName != null”
Leads = sf.query_all(query)[‘records’]
# Dictionary to store leads grouped by criteria
Leads_by_criteria = {}
# Iterate through leads and group by criteria
For lead in leads:
    Email = lead[‘Email’]
    First_name = lead[‘FirstName’]
    Last_name = lead[‘LastName’]
    Criteria = (email, first_name, last_name)
    If criteria not in leads_by_criteria:
        Leads_by_criteria[criteria] = []
    Leads_by_criteria[criteria].append(lead[‘Id’])
# Iterate through grouped leads and merge duplicates
For criteria, lead_ids in leads_by_criteria.items():
    If len(lead_ids) > 1:

        Try:

            Merge_result = sf.Lead.merge(lead_ids)
            If merge_result[‘success’]:
                Print(f”Leads merged successfully: {‘, ‘.join(lead_ids)}”)

            Else:

                Print(f”Failed to merge leads: {‘, ‘.join(lead_ids)}”)        Except Exception as e:
            Print(f”Error merging leads {‘, ‘.join(lead_ids)}: {str€}”)


Your Answer