Searching existing contacts based on a lead

Searching existing contacts based on a lead

I went to the London user group last year and quite a few people were interested in an app which when your looking at a lead automatically searches for existing records within salesforce. So I’ve just created a quick Salesforce app to allow you to search existing contacts based on a lead. It only searches contacts at the moment but I’ll get it to search other objects too if people are interested in it. Its also limited to just searching based on email address at the moment as well, but again if people are interested i’ll expand it.

Lead EmailAddress = [Select l.Email from Lead l WHERE l.Id = :this.thisLead.Id];

System.debug('getContacts Lead: ' + EmailAddress);

contactObj = [Select
c.Name,
c.AccountId,
c.Email,
c.FirstName,
c.Id,
c.LastName,
c.Salutation,
c.Title
from Contact c
WHERE c.Email = :EmailAddress.Email ];

return contactObj;
}
}

Create a searchContacts Page:
<apex:page standardController="Lead" extensions="searchContacts">
<apex:pageBlock >
<apex:pageBlockTable value="{!contacts}" var="item">
<apex:column value="{!item.Name}" />
<!--            <apex:column value="{!item.Id}" />-->
<apex:column value="{!item.AccountId}"/>
<apex:column value="{!item.Title}" />
<apex:column value="{!item.Email}" />
</apex:pageBlockTable>
</apex:pageBlock>
</apex:page>

In the page above i’ve just pulled out Name, AccountId, Title & Email fields, but if you have custom fields or other fields you want to pull out just copy one of the apex:column lines and change the field name (!item.xxxxx)

Next all you need to do is add the visualforce page to the Lead page layout, also don’t forget if you have different profiles that you go in to them and give access to the visualforce page otherwise other users won’t be able to see the visualforce page.

Bingo!