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
Jack InsJack Ins 

Relating Lead record to Contact record after update.

Hi all,

I need to create a trigger to relate a lead after update or insert to an existing contact record.  Both will have an identifier to make the relationship.  Just not sure how to write the trigger.  My code is attached.  I have started the trigger but I am now stuck.

 

Any help would be great.

Thanks Dwayne

trigger LeadCnsmrContactAssoc on Lead (after insert, after update) {
    List<Contact> cntList = new List<Contact>();  
        for (Lead l: Trigger.new)  {    
            if (l.ld_plcsc_Customer_Number__c != null)

                {      
                    try {        
                    Contact cnt = [select id, cnt_plcsc_CustNmbr_ExtID__c from Contact where cnt_plcsc_CustNmbr_ExtID__c =: l.ld_plcsc_Customer_Number__c limit 1];     
                    //cntList.Lead = l.Id;//
                    cntList.add(cnt);}
                    
                    catch (Exception e) {System.debug('Contact Record Does Not Exist.');}  
                   } 
                } 
    if (cntList.size() > 0)  
        update cntList;   
}

 

Best Answer chosen by Admin (Salesforce Developers) 
Jack InsJack Ins

Got it.  Was able to figure it out.

 

trigger LeadCnsmrContactAssoc on Lead (before insert, before update) {
    List<Contact> cntList = new List<Contact>();  
        for (Lead l: Trigger.new)  {    
            if (l.ld_plcsc_Customer_Number__c != null)

                {      
                    try {        
                    Contact cnts = [select id, cnt_plcsc_CustNmbr_ExtID__c from Contact where cnt_plcsc_CustNmbr_ExtID__c =: l.ld_plcsc_Customer_Number__c limit 1];     
                    l.Contact__c = cnts.Id;
                    cntList.add(cnts);}
                    
                    catch (Exception e) {System.debug('Contact Record Does Not Exist.');}  
                   } 
                } 
    if (cntList.size() > 0)  
        update cntList;   
}