function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Domnic JohnsonDomnic Johnson 

Want a Trigger to Link Person Accounts to Leads

Hi, 

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)
​Hoping someone could help me out here.

Thanks 

 
Glyn Anderson 3Glyn Anderson 3
Domnic,  I think this might do what you're looking for.  It assumes that you won't ever be creating duplicate leads simultaneously (with same name, email or phone number).  Disclaimer:  This code is untested and may contain typos.

<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>
Domnic JohnsonDomnic Johnson
Glyn,

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.

 
Glyn Anderson 3Glyn Anderson 3
Duplicate Leads are fine.  The code may not work correctly if the duplicate Leads are created simultaneously, for example, by a data load, because it can only map one lead to each name, email or phone.  I hope it works for you.  If you find any problems, let me know.
Glyn Anderson 3Glyn Anderson 3
Dominic,  How did this work for you?  Did it solve the problem?   If so, please mark the question as solved.   Let me know if you need any more help.
Domnic JohnsonDomnic Johnson
Hi Glyn, 

I dint get to test it out yet, Once I do it will update you.