Salesforce Technical Questions and answers

How to Log Lead Inserts Without Duplicates?

Spread the love

Question

I have a requirement to log every attempt to insert a Lead into a custom object called Lead_Log__c. This log needs to be created regardless of whether the Lead insert is successful or fails validation. To achieve this, I initially used a before insert trigger on the Lead object. However, I faced challenges with handling partial success scenarios, especially when inserting more than 200 records.

Since logs need to be created even if the Lead fails validation, I decided to use a platform event to decouple log creation from the main Lead transaction. However, I encountered issues with duplicate logs when the trigger executes multiple times due to partial success or retry mechanisms. For instance, if the trigger runs twice in a single transaction, the platform event is fired twice, leading to duplicate logs.

Here’s the implementation I started with:

public without sharing class LeadTriggerHandler {
  public void beforeInsert() {
    List<Lead_Log_Event__e> logEvents = new List<Lead_Log_Event__e>();
    // Create platform event instances for each Lead
    EventBus.publish(logEvents);
  }
}

public without sharing class LeadLogEventTriggerHandler {
  public void afterInsert() {
    List<Lead_Log__c> logs = new List<Lead_Log__c>();
    // Process platform events and create logs
    Database.insert(logs);
  }
}

To address the issue of duplicate logs, I attempted to use a static variable to track whether the platform event has already been published. However, this approach did not work because static variables reset after each execution of 200 records in a batch, leading to the possibility of duplicate log entries.

public without sharing class LeadTriggerHandler {
  private static Boolean firstTry = true;

  public void beforeInsert() {
    List<Lead_Log_Event__e> logEvents = new List<Lead_Log_Event__e>();
    if (firstTry) {
      EventBus.publish(logEvents);
    }
    firstTry = false;
  }
}

Another approach to prevent duplicates involves using a hashing mechanism, such as hashCode(), to create a unique identifier for each Lead. This ensures that the same Lead is not logged multiple times even if the trigger fires more than once. Below is an example using hashCode():

public without sharing class LeadTriggerHandler {
  private static Set<Integer> hashCodes = new Set<Integer>();

  public void beforeInsert() {
    List<Lead_Log_Event__e> logEvents = new List<Lead_Log_Event__e>();
    for (Lead lead : Trigger.new) {
      Integer hashCode = lead.hashCode();
      if (hashCodes.add(hashCode)) {
        Lead_Log_Event__e event = new Lead_Log_Event__e();
        // Populate event fields based on Lead data
        logEvents.add(event);
      }
    }
    EventBus.publish(logEvents);
  }
}

The hashCode() method generates a unique identifier for each Lead, and using the Set.add() method ensures that each Lead is processed only once during the transaction. This solution effectively avoids creating duplicate logs.

Alternatively, you can generate a checksum for each Lead by serializing its fields and using a cryptographic function like SHA-256. This checksum can then be stored on the log object to prevent duplicate logs. Here’s an example implementation:

This checksum-based approach allows you to uniquely identify each Lead, even when partial successes occur, and it ensures that duplicate logs are not created.

Answer

To log every attempt to insert a Lead into a custom Lead_Log__c object without creating duplicate logs, especially when handling partial success scenarios or bulk inserts of more than 200 records, there are two effective solutions:

  1. Using hashCode(): By generating a unique hash code for each Lead record in the before insert trigger, and storing it in a static Set<Integer>, you can track whether the Lead has already been processed. The Set.add() method ensures that each record is logged only once during the transaction. This approach prevents logging duplicates when the same Lead is encountered multiple times due to retries or partial success during batch processing.
    • How it works: The hashCode() method is called on each Lead record, creating a relatively unique identifier based on the content of the Lead object. The Set.add() method checks if this hash code already exists in the set. If it does not exist, it means this is the first time the Lead is being processed, and the log event is created. If the hash code exists, it means the Lead has already been processed, and the log creation is skipped.
    • Advantages: This method works well because it is lightweight and doesn’t require the Lead record’s ID to be available, making it suitable for the before insert trigger where records don’t yet have IDs. It also handles bulk inserts efficiently without generating duplicate logs.
  2. Using a Checksum: This method involves serializing the Lead object into a JSON string and then generating a checksum (e.g., using SHA-256). This checksum is unique to the data within the Lead and is used to detect whether a Lead has already been logged. If a Lead with the same checksum is encountered again, the log creation is skipped.
    • How it works: The Lead object is serialized to a JSON string, which is then hashed using the SHA-256 algorithm. The resulting hash is stored as a checksum on the Lead_Log__c object. Before creating a new log, the system checks whether the checksum already exists to ensure no duplicates are created.
    • Advantages: The checksum method is more robust, providing a lower chance of collisions (i.e., two different Leads generating the same checksum). It also works well for situations where you want a more cryptographically secure approach.

Both methods ensure that logs are created for each attempt to insert a Lead without duplication, even when the process is interrupted by partial success or bulk inserts of more than 200 records. The choice of method depends on the complexity and performance needs of your system.

Summing Up

To log every attempt to insert a Lead into a Lead_Log__c object without creating duplicates, especially during partial success inserts or bulk processing (over 200 records), two effective approaches are available:

  1. Using hashCode(): A unique hash code is generated for each Lead record in the before insert trigger. A static Set<Integer> tracks the hash codes, ensuring that each Lead is processed only once, preventing duplicates even during retries or partial successes.
  2. Using a Checksum: Serialize the Lead object into a JSON string, then generate a SHA-256 checksum to uniquely identify the record. This checksum is stored and compared to avoid creating duplicate logs.

Both methods ensure that logs are created for each insert attempt without duplication, and they efficiently handle bulk inserts and partial success scenarios. The choice between hashCode() and checksum depends on performance and security needs.

Job-Oriented Salesforce Training with 100% Money Back Assurance

Our Salesforce Course is designed to provide a deep understanding of the Salesforce platform, giving you the essential skills to succeed in the CRM industry. The curriculum covers important modules like Salesforce Admin, Developer, and AI, combining theoretical knowledge with hands-on application. Through practical exercises and real-world project experience, you’ll gain the expertise needed to tackle complex business problems with Salesforce solutions. Our experienced instructors ensure you acquire the technical skills and industry insights necessary to thrive in the Salesforce ecosystem.

In addition to building technical proficiency, our Salesforce training in Hyderabad offers personalized mentoring, certification exam guidance, and interview preparation to enhance your career opportunities. You’ll have access to extensive study materials, hands-on project experience, and one-on-one support throughout your learning journey. By the end of the program, you’ll be prepared for certification exams and equipped with the problem-solving abilities and practical experience that employers value. Start your Salesforce career with us and unlock a world of exciting possibilities. Sign up for a Free Demo!

Open Chat
1
Dear Sir/Madam
How can I help you?