You need to sign in to do that
Don't have an account?

Want a Trigger to Link Person Accounts to Leads
Hi,
We have Person account enabled. What am trying to achieve is,
Thanks
We have Person account enabled. What am trying to achieve is,
- To have a trigger fire when the Account_View__c field is blank
- Every time a new lead is created in our system
- If Account_View__c is blank
- Search the existing accounts for matching Name, Email & Phone ( as its person account the field name are : Name, Phone, PersonEmail)
- Populate Account_View__c field (Agent can link this field and access leads account info)
Thanks
<pre>
trigger LinkPersonAccountToLead on Lead ( before insert )
{
Map<String,Lead> leadsByName = new Map<String,Lead>();
Map<String,Lead> leadsByEmail = new Map<String,Lead>();
Map<String,Lead> leadsByPhone = new Map<String,Lead>();
for ( Lead lead : Trigger.new )
{
if ( lead.Account_View__c != null ) continue;
leadsByName.put( lead.Name, lead );
if ( lead.Email != null ) leadsByEmail.put( lead.Email, lead );
if ( lead.Phone != null ) leadsByPhone.put( lead.Phone, lead );
}
for ( Account personAccout :
[ SELECT Id, Name, PersonEmail, Phone
FROM Account
WHERE ( IsPersonAccount = true
AND ( Name IN :leadsByName.keySet()
OR PersonEmail IN :leadsByEmail.keySet()
OR Phone IN :leadsByPhone.keySet()
)
)
]
)
{
if ( leadsByName.containsKey( personAccount.Name ) )
{
Lead lead = leadsByName.get( personAccount.Name );
lead.Account_View__c = personAccount.Id;
continue;
}
if ( leadsByEmail.containsKey( personAccount.PersonEmail ) )
{
Lead lead = leadsByEmail.get( personAccount.PersonEmail );
lead.Account_View__c = personAccount.Id;
continue;
}
if ( leadsByPhone.containsKey( personAccount.Phone ) )
{
Lead lead = leadsByPhone.get( personAccount.Phone );
lead.Account_View__c = personAccount.Id;
continue;
}
}
}
</pre>
We consider a lead duplicate when the name, email, phone and the project are the same. If the person registers for a multiple projects at the same time it's fine.
Thanks for helping out, will test it and let you know if it worked.
I dint get to test it out yet, Once I do it will update you.