Note: This sample is very basic and just displays the ids of duplicates found when saving a Lead from a custom VisualForce Page.
VisualForce Page: LeadCreation.page
<apex:page standardController="Lead" extensions="LeadCreationController">
<apex:form >
<apex:pageMessages escape="false" />
<apex:pageBlock >
<apex:pageBlockButtons >
<apex:commandButton value="Save" action="{!saveLead}" />
</apex:pageBlockButtons>
<apex:pageBlockSection >
<apex:inputField value="{!Lead.FirstName}" />
<apex:inputField value="{!Lead.Phone}" />
<apex:inputField value="{!Lead.LastName}" />
<apex:inputField value="{!Lead.Email}" />
<apex:inputField value="{!Lead.Company}" />
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
Apex Class: LeadCreationController.cls
public with sharing class LeadCreationController
{
public Lead newLead;
public LeadCreationController( ApexPages.StandardController stdController )
{
this.newLead = (Lead)stdController.getRecord();
}
public pageReference saveLead()
{
// Look for duplicates before trying to save.
List<CRMfusionDBR101.DB_Api.Matches> matchSets = CRMfusionDBR101.DB_Api.findMatches( newLead );
if ( matchSets.isEmpty() )
{
// No matches were found, try to save.
try
{
insert newLead;
}
catch ( Exception e )
{
ApexPages.addMessages( e );
return null;
}
return new pageReference( '/' + newLead.id );
}
else
{
// Matches were found, just display the list of matching ids in an error
// message as a basic example.
String errorMessage = 'Duplicate leads found: ';
for ( CRMfusionDBR101.DB_Api.Matches matchSet : matchSets )
for ( Id leadId : matchSet.matchedIds )
errorMessage += leadId + ', ';
// Trim the trailing ', ';
errorMessage = errorMessage.subString( 0, errorMessage.length() - 2);
// Return the error.
ApexPages.Message pageMessage = new ApexPages.Message( ApexPages.Severity.ERROR, errorMessage );
ApexPages.addMessage( pageMessage );
return null;
}
}
}