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
James MooneyJames Mooney 

Update Lead [Company] field with [Name] from Account (matched by Contact [Email Address])

Hi All,

I'm very much an SF newbie, so hopefully this makes sense...

When a new lead is created (via Web2Lead etc), we need some kind of internal process or trigger to run and match the Lead [Email Address], to the [Email Address] of an existing Contact. If there is a match, we need to return the Contacts parent Account [Name], and update the Lead [Company] field.

So if we already have an account for 'Test Co', with a Contact associated with it being 'Joe Bloggs / joe@bloggs.com', and then get a new Lead with the Email Address 'joe@bloggs.com', we need the Lead [Company] field to be automatically updated to show 'Test Co'. 

Is this possible? If so, can anyone point me towards the best way to achieve this. I've had a look at creating a process, but that doesn't seem to offer the options i need. Do we need some custom coding? If so, as someone with PHP/JS experience, what is the best route to take.

Many thanks.
Tiago Armando CoelhoTiago Armando Coelho
Hi James, 

The request is feasible and can be done with the trigger.

trigger LeadBeforeInsert on Lead (before insert){

    //ensure that this isn't not a mass update process
    if(trigger.new.size() > 0 ){
        for( Lead l : trigger.new ) {
            // limit to 1 because I'm  assuming that you cannot have duplicated emails in the contacts
            Contact cont = [ Select Id, Account.Name from Contact where Email =: l.email limit 1];
            if(cont != null)
                l.company = cont.Account.Name;
        }
    }

}

Please note that this code isn't optimized and is simply an example.