
Have you ever been frustrated by duplicate records slipping through Salesforce’s built-in duplicate rules or matching rules? Or worse, have you seen workflows disrupted by unhandled duplicate-related exceptions? If so, you’re not alone. Duplicate records are a common challenge for Salesforce users, especially in scenarios where built-in rules can’t handle complex or nuanced conditions.
In today’s blog, we’ll explore an enhanced approach to detecting duplicate records programmatically using Apex. By integrating this solution with Flows, you can improve user experience and ensure smoother workflows without relying solely on Salesforce’s out-of-the-box functionality.
Challenge with Duplicate Detection in Salesforce
Duplicate rules and matching rules, while useful, often fall short in certain scenarios:
Limited Matching Rules: They may fail when only a subset of fields (e.g., FirstName and LastName) is provided.
Critical Field Issues: Salesforce’s pattern-based normalization and regex checks don’t work if critical fields are null.
Workflow Disruptions: Duplicate rules often block record creation, leading to unhandled exceptions like:
System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATES_DETECTED, Duplicate Alert: []
In such cases, administrators receive repeated error notifications, disrupting workflows and user experience. Here, we'll explore how to mimic Salesforce's duplicate detection mechanism programmatically using Apex and integrate it into Flows for an improved user interface.
Using Apex for Duplicate Detection
Salesforce provides the Datacloud.FindDuplicates API to detect duplicates programmatically. This method enables developers to go beyond traditional duplicate rules, offering a more flexible approach that integrates seamlessly with Flows.
Requirement Overview
Imagine you have duplicate rules in place to prevent duplicate record creation. However, users entering basic details (e.g., FirstName, LastName) in a Screen Flow may:
Encounter false positives due to multiple similar entries in production.
Struggle with complex scenarios like pattern matches or null-critical fields.
Solution: A custom Apex class that uses Datacloud.FindDuplicates to detect duplicates dynamically and return results to the Flow for improved decision-making.
Create a class: DuplicateCheckUtility
public class DuplicateCheckUtility {
@InvocableMethod(
label = 'Duplicate Check'
description = 'Returns the List<Boolean> respectively'
)
public static List<Boolean> checkDuplicatesForSObjectEntry(
List<SObject> objectsToCheck
) {
Boolean isDuplicateThere = checkDuplicatesForRecord(
new List<SObject>{ objectsToCheck[0] }
);
return new List<Boolean>{ isDuplicateThere };
}
public static Boolean checkDuplicatesForRecord(
List<SObject> objectsToCheck
) {
Boolean result = false;
Datacloud.FindDuplicatesResult[] results = Datacloud.FindDuplicates.findDuplicates(
objectsToCheck
);
for (Datacloud.FindDuplicatesResult findDuplicateResultObj : results) {
for (
Datacloud.DuplicateResult duplicateResultObj :
findDuplicateResultObj.getDuplicateResults()
) {
for (Datacloud.MatchResult matchResultObj : duplicateResultObj.getMatchResults()) {
for (Datacloud.MatchRecord matchRecord : matchResultObj.getMatchRecords()) {
result = true;
}
}
}
}
return result;
}
}
Key Features of the Class
checkDuplicatesForSObjectEntry Method
An @InvocableMethod for easy integration with Flows.
Processes a single record to identify duplicates, returning a Boolean result.
checkDuplicatesForRecord Method
Uses the Datacloud.FindDuplicates API to detect duplicates.
Iterates through match results and duplicate records to validate potential duplicates.
Test Class


How to Use in a Flow
Create the Flow:
Add a Screen element to collect the user’s input (e.g., FirstName, LastName).
Pass the input to the DuplicateCheckUtility.checkDuplicatesForSObjectEntry method via an Apex Action.
Display Results:
Use the Boolean result returned by the Apex method to inform the user about potential duplicates.
Provide options to view duplicates or proceed with record creation.
Benefits of This Approach
Proactive Detection: Identify duplicates before record creation, reducing workflow interruptions.
Customizable: Tailor detection logic to handle edge cases and complex scenarios.
Improved User Experience: Seamlessly integrate duplicate detection into Flows for better UI and decision-making.
Error Reduction: Minimize exceptions caused by blocked duplicate records.
Conclusion
By extending Salesforce's out-of-the-box functionality with Apex, you can overcome the limitations of duplicate rules and matching rules in complex scenarios. This approach ensures smoother workflows, prevents unnecessary exceptions, and improves the user experience for Flow-based data entry.
If you'd like to see the code and resources used in this project, you can access the repository on GitHub.To access the AVENOIRBLOGS repository, click here. Feel free to explore the code and use it as a reference for your own projects.
Happy Coding! You can leave a comment to help me understand how the blog helped you. If you need further assistance, please contact us. You can click "Reach Us" on the website and share the issue with me.
References
Blog Credit:
D. Dewangan
Salesforce Developer
Avenoir Technologies Pvt. Ltd.
Reach us: team@avenoir.ai
Comments