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 similar to Index,Match formula in Excel

I'm new to Apex & trying to figure out a trigger that will match the email of a Lead & a Child user and give me the Id in one object.  If a lead already exists as a child, I want the child Id.  Otherwise, the field in the Lead object should be left blank.  Any idea how to approach this?
David Kerr 5David Kerr 5
A Lookup Relationship between Child/Lead exists and I want to auto-fill this field on a new Lead if they already exist
Maharajan CMaharajan C
Not understandable can you please explain clearly so that you will get some help
David Kerr 5David Kerr 5
Hi Maharajan,

Sure thing.  I want to identify if any Leads that come into the system already exist as a Child user.  The trigger would fire and check if there is a match between the emails of the Lead and Child.  If there is a match, I want to pull the ID of the Child user, if not, it would be left blank.  I've created a lookup relationship between Child and Lead and I want to autofill this field.
David Kerr 5David Kerr 5
Hi Raj,

Looks like the code saved fine.  However, I'm getting the following error when I create a test Lead and a test Child user:

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger LeadExist caused an unexpected exception, contact your administrator: LeadExist: execution of BeforeInsert caused by: System.StringException: Invalid id: (*Parent_Customer_ID__c*): External entry point


The Parent_Customer_ID__c is the custom field that I want to pull the value for if the email of the Lead matches the email of the Child user.  That value should auto-populate on my looku  Here is my code:

trigger LeadExist on Lead (before insert) {

    Set<String> EmailId = new Set<String>();
    Map<String,Id> MapuserId = new Map<String,Id>();
    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.Email);
    }

}

I appreciate your assistance on this!
Maharajan CMaharajan C
Hi David,

Please use the below updated code:

trigger LeadExist on Lead (before insert) {

    Set<String> EmailId = new Set<String>();
    Map<String,Id> MapuserId = new Map<String,Id>();
    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];     // Check did you need the Parent_Customer_ID__c or ID of the child user record Id
    }    
    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);  ///Use the c.Id  if you want the ID of the child user record Id or if you want the Parent_Customer_ID__c from the child user object keep it as line exist
            }
            
        }
    }
    
    for(Lead ld : trigger.new)
    {
        ld.Parent_Customer_ID__c = MapuserId.get(ld.Email);   /// In which field you want to populate the ID in Lead Object use that API Name
    }

}

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
Maharajan CMaharajan C
And also check if Parent_Customer_ID__c  is Salesforce Id or not if this not the Lookup field or any Salesforce Id Field then change     Map<String,Id> MapuserId = new Map<String,Id>();    --->      Map<String,String> MapuserId = new Map<String,String>();
 
David Kerr 5David Kerr 5
The Parent_Customer_ID__c is part of the Child object, which is why I'm getting a Variable does not exist error on the last line.  The lookup field on the Lead Object that I would like to fill automatically is Child_Exist__c.  Additionally, I'm still receiving the same error:

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger LeadExist caused an unexpected exception, contact your administrator: LeadExist: execution of BeforeInsert caused by: System.StringException: Invalid id: TestID_1: External entry point


TestID_1 is the Parent_Customer_ID_c value for the Child whose email matches a Lead with the same Email.  



trigger LeadExist on Lead (before insert) {

    Set<String> EmailId = new Set<String>();
    Map<String,Id> MapuserId = new Map<String,Id>();
    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)
    {
        Id.Email= MapuserId.get(ld.Child_Exist__c);   
    }

}
 
David Kerr 5David Kerr 5
Update: I have the following code which saves fine & I'm able to enter a test lead.  However, the email address does not save after creating the lead, but I can edit the lead after insert and it will save.  The Child_Exist__c lookup field on the Lead object still does not auto-populate with the Parent_Customer_ID__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.Email= MapuserId.get(ld.Child_Exist__c);   
    }

}