Examples & Use Cases
After Index Search Plugin
Base Example Code
global class BaseAfterIndexSearchPlugin implements dupcheck.dc3PluginInterface {
// Allowed event type for this plugin.
private static final Set<String> implementedEvents = new Set<String>{ 'INDEX_SEARCH_AFTER' };
// Check if the plugin should process the given event.
global Boolean isAvailable(dupcheck.dc3Plugin.PluginEventType eventType) {
return implementedEvents.contains(eventType.name());
}
// Main entry point for plugin execution.
global Object execute(dupcheck.dc3Plugin.PluginEventType eventType, Object eventData) {
// Process only INDEX_SEARCH_AFTER events.
if (eventType.name() == 'INDEX_SEARCH_AFTER') {
return handleIndexSearchAfter((dupcheck.dc3PluginModel.IndexSearchAfterInput) eventData);
}
return null;
}
// Base implementation that returns the output without modifying candidate records.
// Insert your custom logic in the section indicated below.
public dupcheck.dc3PluginModel.IndexSearchAfterOutput handleIndexSearchAfter(dupcheck.dc3PluginModel.IndexSearchAfterInput input) {
// Create the output using the input defaults.
dupcheck.dc3PluginModel.IndexSearchAfterOutput output = new dupcheck.dc3PluginModel.IndexSearchAfterOutput(input);
// CUSTOM LOGIC: Add your code here to modify the candidate record list.
// Example:
// output.foundRecords.add('recordId');
// output.foundRecords.remove('recordId');
return output;
}
}
Example 1: Add records to the candidate list
The plugin checks if the object prefix indicates a Lead (by checking for ‘00Q
’). If so, it queries the source Lead record and then retrieves and adds the IDs of all matching Leads (based on Email and LastName) to the candidate list. This allows you to augment the candidate records used for scoring duplicates using custom logic.
global class AfterIndexSearch_Add implements dupcheck.dc3PluginInterface {
global Object execute(dupcheck.dc3Plugin.PluginEventType eventType, Object eventData) {
switch on eventType {
when INDEX_SEARCH_AFTER {
return indexSearchAfter((dupcheck.dc3PluginModel.IndexSearchAfterInput) eventData);
}
when else {
return null;
}
}
}
private static Set<String> implementedEvents = new Set<String>{ 'INDEX_SEARCH_AFTER' };
global Boolean isAvailable(dupcheck.dc3Plugin.PluginEventType eventType) {
return dc3GenericIndexAfterSearch.implementedEvents.contains(eventType.name());
}
public dupcheck.dc3PluginModel.IndexSearchAfterOutput indexSearchAfter(dupcheck.dc3PluginModel.IndexSearchAfterInput input) {
dupcheck.dc3PluginModel.IndexSearchAfterOutput output = new dupcheck.dc3PluginModel.IndexSearchAfterOutput(input);
// Log the object prefix for debugging.
system.debug('Object Prefix: ' + input.objectPrefix);
// Check if the object is a Lead.
if (input.objectPrefix == '00Q') {
// Log input and output details before processing.
system.debug('Index Search After Input: ' + input);
system.debug('Index Search After Output (initial): ' + output);
// Query the source Lead record using the Id provided in the input.
Lead sourceLead = [SELECT Id, FirstName, LastName, Email FROM Lead WHERE Id = :String.valueOf(input.objectData.get('Id'))];
system.debug('Source Lead: ' + sourceLead);
// Retrieve all Lead records that share the same Email and LastName as the source Lead.
for (Lead ld : [SELECT Id FROM Lead WHERE Email = :sourceLead.Email AND LastName = :sourceLead.LastName]) {
// Add each matching Lead's Id to the candidate record list.
output.foundRecords.add(ld.Id);
}
// Log the output after candidate records have been modified.
system.debug('Index Search After Output (final): ' + output);
}
// Return the modified output to be used in the duplicate score calculation.
return output;
}
}
Example 2: Remove records from the candidate list
This plugin examines each candidate Opportunity (identified by the ‘006’ prefix) and queries their StageName
. If an Opportunity is in a final stage (“Closed Won” or “Closed Lost”), the plugin removes its Id from the candidate list. This ensures that closed Opportunities are never considered as duplicate records.
global class AfterIndexSearch_Remove implements dupcheck.dc3PluginInterface {
private static final Set<String> implementedEvents = new Set<String>{ 'INDEX_SEARCH_AFTER' };
global Boolean isAvailable(dupcheck.dc3Plugin.PluginEventType eventType) {
return implementedEvents.contains(eventType.name());
}
global Object execute(dupcheck.dc3Plugin.PluginEventType eventType, Object eventData) {
if (eventType.name() == 'INDEX_SEARCH_AFTER') {
return filterOpportunities((dupcheck.dc3PluginModel.IndexSearchAfterInput) eventData);
}
return null;
}
// This method filters out candidate Opportunity records that have a StageName
// of "Closed Won" or "Closed Lost" by removing them from the candidate list.
public dupcheck.dc3PluginModel.IndexSearchAfterOutput filterOpportunities(dupcheck.dc3PluginModel.IndexSearchAfterInput input) {
// Initialize the output with the default candidate records.
dupcheck.dc3PluginModel.IndexSearchAfterOutput output = new dupcheck.dc3PluginModel.IndexSearchAfterOutput(input);
// Identify candidate records that are Opportunity records.
// Opportunity record Ids typically start with the prefix '006'.
Set<Id> opportunityIds = new Set<Id>();
for (Id candidateId : output.foundRecords) {
if (String.valueOf(candidateId).startsWith('006')) {
opportunityIds.add(candidateId);
}
}
// If there are any Opportunity candidates, query them to check their StageName.
if (!opportunityIds.isEmpty()) {
List<Opportunity> opportunities = [
SELECT Id, StageName
FROM Opportunity
WHERE Id IN :opportunityIds
];
// Loop through the queried Opportunities.
// Remove those that are "Closed Won" or "Closed Lost" from the candidate list.
for (Opportunity opp : opportunities) {
if (opp.StageName == 'Closed Won' || opp.StageName == 'Closed Lost') {
output.foundRecords.remove(opp.Id);
System.debug('Removed Opportunity with Id ' + opp.Id +
' due to StageName: ' + opp.StageName);
}
}
}
// Return the modified output with unwanted candidate records filtered out.
return output;
}
}
Updated 17 days ago