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
David Kerr 5David Kerr 5 

Apex trigger on Lead to auto populate lookup field

I have the following code that I want to trigger when a new Lead comes into the system.  The trigger will auto populate the "Child Exist" Lookup field on the Lead object if its email matches with a user in the Child object.  If the two emails match, I want to have the Parent_Customer_ID__c of the Child user to auto-populate into the Child Exist field on the Lead.  If there is no match, it can be left blank.

trigger LeadExist on Lead (before insert) {

    Set<String> EmailId = new Set<String>();
    Map<String,String> MapuserId = new Map<String,String>();
    List<Child__c> usrlist = new List<Child__c>(); 
    for(Lead ld : trigger.new)
    {
        If(ld.Email != null)
        {
            EmailId.add(ld.Email);
        }
    }
    
    If(EmailId.size() > 0)
    {
        usrlist = [Select Parent_Customer_ID__c, Email__c from Child__c Where Email__c IN: EmailId];     
    }    
    if(usrlist.size() > 0)
    {
         for(Child__c c : usrlist)
        {
            if(!MapuserId.containsKey(c.Email__c))
            {
                MapuserId.put(c.Email__c, c.Parent_Customer_ID__c);  
            }
            
        }
    }
    
    for(Lead ld : trigger.new)
    {
        ld.Email= MapuserId.get(ld.Child_Exist__c);   
    }

}



Currently, I'm able to save the trigger and create a test lead & child.  However, when I save the test Lead, the email address is not being saved -- once I update the lead after it has been created, the email address will save.  The Parent_Customer_ID__c still won't auto populate.  Any help here will be greatly appreciated!
Best Answer chosen by David Kerr 5
Gaurish Gopal GoelGaurish Gopal Goel
Hi David, Okay you can make this change and make sure that "Parent_Customer_ID__c" field is a LookUp(Child__c) otherwise it will throw an error.
trigger LeadExist on Lead (before insert) 
{
    Set<String> EmailId = new Set<String>();
    Map<String,String> MapuserId = new Map<String,String>();
    List<Child__c> usrlist = new List<Child__c>(); 
    for(Lead ld : trigger.new)
    {
        If(ld.Email != null)
        {
            EmailId.add(ld.Email);
        }
    }
    
    If(EmailId.size() > 0)
    {
        usrlist = [Select Id,Parent_Customer_ID__c, Email__c from Child__c Where Email__c IN: EmailId];     
    }    
    if(usrlist.size() > 0)
    {
         for(Child__c c : usrlist)
        {
            if(!MapuserId.containsKey(c.Email__c))
            {
                MapuserId.put(c.Email__c, c.Parent_Customer_ID__c);  
            }
        }
    }
    
    for(Lead ld : trigger.new)
    {
        if(MapuserId.containsKey(ld.Email))
		{
			ld.Child_Exist__c = MapuserId.get(ld.Email);
		}
    }
}

Please do not forget to mark this thread as SOLVED and answer as the BEST ANSWER if it helps address your issue. Thanks.

All Answers

Gaurish Gopal GoelGaurish Gopal Goel
Hi David, Try this:
trigger LeadExist on Lead (before insert) 
{
    Set<String> EmailId = new Set<String>();
    Map<String,String> MapuserId = new Map<String,String>();
    List<Child__c> usrlist = new List<Child__c>(); 
    for(Lead ld : trigger.new)
    {
        If(ld.Email != null)
        {
            EmailId.add(ld.Email);
        }
    }
    
    If(EmailId.size() > 0)
    {
        usrlist = [Select Id,Parent_Customer_ID__c, Email__c from Child__c Where Email__c IN: EmailId];     
    }    
    if(usrlist.size() > 0)
    {
         for(Child__c c : usrlist)
        {
            if(!MapuserId.containsKey(c.Email__c))
            {
                MapuserId.put(c.Email__c, c.Id);  
            }
        }
    }
    
    for(Lead ld : trigger.new)
    {
        if(MapuserId.containsKey(ld.Email))
		{
			ld.Child_Exist__c = MapuserId.get(ld.Email);
		}
    }
}



 
David Kerr 5David Kerr 5
Hi Gaurish,

Almost there!  The lookup field does populate, but it is showing me the name of the Child Name for that user.  I'm interested in the Parent_Customer_ID__c of the child user.
Maharajan CMaharajan C
trigger LeadExist on Lead (before insert) {

    Set<String> EmailId = new Set<String>();
    Map<String,String> MapuserId = new Map<String,String>();
    List<Child__c> usrlist = new List<Child__c>(); 
    for(Lead ld : trigger.new)
    {
        If(ld.Email != null)
        {
            EmailId.add(ld.Email);
        }
    }
    
    If(EmailId.size() > 0)
    {
        usrlist = [Select Parent_Customer_ID__c, Email__c from Child__c Where Email__c IN: EmailId];     
    }    
    if(usrlist.size() > 0)
    {
         for(Child__c c : usrlist)
        {
            if(!MapuserId.containsKey(c.Email__c))
            {
                MapuserId.put(c.Email__c, c.Parent_Customer_ID__c);  
            }
            
        }
    }
    
    for(Lead ld : trigger.new)
    {
        ld.Child_Exist__c = MapuserId.get(ld.Email);      ///// use the Child Exist field Api Name of lead object dont use the ld.Email
  }

}

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Raj
Gaurish Gopal GoelGaurish Gopal Goel
Hi David, Okay you can make this change and make sure that "Parent_Customer_ID__c" field is a LookUp(Child__c) otherwise it will throw an error.
trigger LeadExist on Lead (before insert) 
{
    Set<String> EmailId = new Set<String>();
    Map<String,String> MapuserId = new Map<String,String>();
    List<Child__c> usrlist = new List<Child__c>(); 
    for(Lead ld : trigger.new)
    {
        If(ld.Email != null)
        {
            EmailId.add(ld.Email);
        }
    }
    
    If(EmailId.size() > 0)
    {
        usrlist = [Select Id,Parent_Customer_ID__c, Email__c from Child__c Where Email__c IN: EmailId];     
    }    
    if(usrlist.size() > 0)
    {
         for(Child__c c : usrlist)
        {
            if(!MapuserId.containsKey(c.Email__c))
            {
                MapuserId.put(c.Email__c, c.Parent_Customer_ID__c);  
            }
        }
    }
    
    for(Lead ld : trigger.new)
    {
        if(MapuserId.containsKey(ld.Email))
		{
			ld.Child_Exist__c = MapuserId.get(ld.Email);
		}
    }
}

Please do not forget to mark this thread as SOLVED and answer as the BEST ANSWER if it helps address your issue. Thanks.
This was selected as the best answer
David Kerr 5David Kerr 5
Hi Gaurish,

The trigger works great!  I have a quick follow up question -- I tested out changing the email of the lead after the initial match between Child and Lead.  The Child Exist field is still populated after I create the mismatch in emails.  Would it be recommended to do an after update on the trigger?
Gaurish Gopal GoelGaurish Gopal Goel
Hi David,

Yes you are right, you need to write more logic for After Update event in which you can check whether the email has been changed or not. And if the email is changed and a mismatch then you can update the look up field to blank or null.