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
DbjensenDbjensen 

Trigger not calling method

Hello - I have a class that adds an account and contact to a new lead - if the account and contact exists. I'm trying to call this from an After Insert trigger. However, when I insert the lead, nothing happens. I'm wondering if it's something to do with the trigger parameter but just not sure. Any help would be greatly appreciated. 

This is the method I'm trying to call from the trigger.  AddExistingAccountAndContactToLead.findExistingAccountContact(Trigger.new);

Here is the trigger: 
trigger MasterLeadTrigger on Lead (
    before insert, after insert, 
    before update, after update, 
    before delete, after delete) {
        
        if (Trigger.isBefore) {
            if (Trigger.isInsert) {
                SetLeadRecordType.leadsToUpdate(Trigger.new); 
                DuplicateLead.potentialDupes(Trigger.new);
                CloseExpiredLeads.closingExpiredLeads(Trigger.new);
            } 
            if (Trigger.isUpdate) {
                
            }
            if (Trigger.isDelete) {
                
            }
            
         if (Trigger.IsAfter) {
             if (Trigger.isInsert) {
                 AddExistingAccountAndContactToLead.findExistingAccountContact(Trigger.new);
                 //AddAccountandContactToLead.addRecodrsToLead(Trigger.New);
                } 
                if (Trigger.isUpdate) {
                    
                }
                if (Trigger.isDelete) {
                }
            }
        }
    }

Here is the class:
public class AddExistingAccountAndContactToLead {
    
    public static void findExistingAccountContact(List<Lead> nonDupe) {
        
        List<String> LastNameStreetAndZipCode = new List<String>();
        List<Account> existingAcctsList = new List<Account>();
        List<Contact> existingCtList = new List<Contact>();
        List<Lead_Submission__c> existingLSList = new List<Lead_Submission__c>();
        
        for(Lead newLead : nonDupe) {
                LastNameStreetAndZipCode.add(newLead.LastNameStreetAndZipCode__c);
        }
        
        System.debug('Addresses in LastNameStreetAndZipCode ' + LastNameStreetAndZipCode);
        
        Map<String, Account> mapOfAccounts = new Map<String, Account>();

        if(LastNameStreetAndZipCode.size() > 0) {
            Try{ 
        List<Account> exsistingAccount = [Select Id, LastNameStreetAndZipCode__c FROM Account
                                          WHERE LastNameStreetAndZipCode__c IN : LastNameStreetAndZipCode];
        
         System.debug('Matching account found ' + exsistingAccount);      
                
            existingAcctsList.addAll(exsistingAccount);
            } catch(Exception e) {
                System.debug('No existing accounts found ' + e);
            }
        }
        
        for(Account acct : existingAcctsList) {
            mapOfAccounts.put(acct.LastNameStreetAndZipCode__c, acct);
        }
        
        Map<String, Contact> mapOfContacts = new Map<String, Contact>();
        
        if(LastNameStreetAndZipCode.size() > 0) {
            Try{ 
        List<Contact> existingContact = [Select Id, LastNameStreetAndZipCode__c FROM Contact
                                         WHERE LastNameStreetAndZipCode__c IN :LastNameStreetAndZipCode];
                
        System.debug('Matching contact found ' + existingContact);        
        
        existingCtList.addAll(existingContact);
                } catch(Exception e) {
                System.debug('No existing contacts found ' + e);
            }
        }
        
        for(Contact ct : existingCtList) {
            mapOfContacts.put(ct.LastNameStreetAndZipCode__c, ct);
        }
        
        Map<String, Lead_Submission__c> mapOfLeadSubmission = new Map<String, Lead_Submission__c>();

        if(LastNameStreetAndZipCode.size() > 0) {
            Try{ 
        List<Lead_Submission__c> exsistingLeadSubmission = [Select Id, LastNameStreetAndZipCode__c FROM Lead_Submission__c
                                          WHERE LastNameStreetAndZipCode__c =: LastNameStreetAndZipCode];
        
         System.debug('Matching lead submission found ' + exsistingLeadSubmission);
                
            existingLSList.addAll(exsistingLeadSubmission);
            } catch(Exception e) {
                System.debug('No existing lead submissions found ' + e);
            }
        }
        
        System.debug('Lead Sbumission added to list ' + existingLSList);
        
        for(lead_submission__c leadSub : existingLSList) {
            mapOfLeadSubmission.put(leadSub.LastNameStreetAndZipCode__c, leadSub);
        }
        
        for(Lead newLead : nonDupe) {
            //if(newLead.Duplicate__c == false) {
                Account retrievedAcctId = mapOfAccounts.get(newLead.LastNameStreetAndZipCode__c);
                Contact retrievedCtId = mapOfContacts.get(newLead.LastNameStreetAndZipCode__c);
                Lead_Submission__c retrievedLsId = mapOfLeadSubmission.get(newLead.LastNameStreetAndZipCode__c);
                //System.debug('retrievedLsId = ' + retrievedLsId.Id);
                try {
                    newLead.Household__c = retrievedAcctId.Id;
                    newLead.Individual_Client__c = retrievedCtId.Id;
                    //newLead.Lead_Submission__c = retrievedLsId.Id;
                } catch(Exception e) {
                System.debug('No household id or contact id found ' + e);
                  }
            //}
        }
    }
}
Best Answer chosen by Dbjensen
Maharajan CMaharajan C
Yes you are correct issue is an update on the same record while your process is in the middle of executing so use the before event.

FYI : While use the before Events there is no need to perform any explicit DML Operation.

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
 

All Answers

Maharajan CMaharajan C
Hi Dbjensen,

In the Apex Class you have to perform the DML Operation because In the trigger you are calling the class in the After event so u have to add the Dml statements in your class like below.

public class AddExistingAccountAndContactToLead {
    
    public static void findExistingAccountContact(List<Lead> nonDupe) {
    
        List<Lead> LeadstoUpdate = new List<Lead>();
        
        List<String> LastNameStreetAndZipCode = new List<String>();
        List<Account> existingAcctsList = new List<Account>();
        List<Contact> existingCtList = new List<Contact>();
        List<Lead_Submission__c> existingLSList = new List<Lead_Submission__c>();
        
        for(Lead newLead : nonDupe) {
                LastNameStreetAndZipCode.add(newLead.LastNameStreetAndZipCode__c);
        }
        
        System.debug('Addresses in LastNameStreetAndZipCode ' + LastNameStreetAndZipCode);
        
        Map<String, Account> mapOfAccounts = new Map<String, Account>();

        if(LastNameStreetAndZipCode.size() > 0) {
            Try{ 
        List<Account> exsistingAccount = [Select Id, LastNameStreetAndZipCode__c FROM Account
                                          WHERE LastNameStreetAndZipCode__c IN : LastNameStreetAndZipCode];
        
         System.debug('Matching account found ' + exsistingAccount);      
                
            existingAcctsList.addAll(exsistingAccount);
            } catch(Exception e) {
                System.debug('No existing accounts found ' + e);
            }
        }
        
        for(Account acct : existingAcctsList) {
            mapOfAccounts.put(acct.LastNameStreetAndZipCode__c, acct);
        }
        
        Map<String, Contact> mapOfContacts = new Map<String, Contact>();
        
        if(LastNameStreetAndZipCode.size() > 0) {
            Try{ 
        List<Contact> existingContact = [Select Id, LastNameStreetAndZipCode__c FROM Contact
                                         WHERE LastNameStreetAndZipCode__c IN :LastNameStreetAndZipCode];
                
        System.debug('Matching contact found ' + existingContact);        
        
        existingCtList.addAll(existingContact);
                } catch(Exception e) {
                System.debug('No existing contacts found ' + e);
            }
        }
        
        for(Contact ct : existingCtList) {
            mapOfContacts.put(ct.LastNameStreetAndZipCode__c, ct);
        }
        
        Map<String, Lead_Submission__c> mapOfLeadSubmission = new Map<String, Lead_Submission__c>();

        if(LastNameStreetAndZipCode.size() > 0) {
            Try{ 
        List<Lead_Submission__c> exsistingLeadSubmission = [Select Id, LastNameStreetAndZipCode__c FROM Lead_Submission__c
                                          WHERE LastNameStreetAndZipCode__c =: LastNameStreetAndZipCode];
        
         System.debug('Matching lead submission found ' + exsistingLeadSubmission);
                
            existingLSList.addAll(exsistingLeadSubmission);
            } catch(Exception e) {
                System.debug('No existing lead submissions found ' + e);
            }
        }
        
        System.debug('Lead Sbumission added to list ' + existingLSList);
        
        for(lead_submission__c leadSub : existingLSList) {
            mapOfLeadSubmission.put(leadSub.LastNameStreetAndZipCode__c, leadSub);
        }
        
        for(Lead newLead : nonDupe) {
            //if(newLead.Duplicate__c == false) {
                Account retrievedAcctId = mapOfAccounts.get(newLead.LastNameStreetAndZipCode__c);
                Contact retrievedCtId = mapOfContacts.get(newLead.LastNameStreetAndZipCode__c);
                Lead_Submission__c retrievedLsId = mapOfLeadSubmission.get(newLead.LastNameStreetAndZipCode__c);
                //System.debug('retrievedLsId = ' + retrievedLsId.Id);
                try {
                    newLead.Household__c = retrievedAcctId.Id;
                    newLead.Individual_Client__c = retrievedCtId.Id;
                    //newLead.Lead_Submission__c = retrievedLsId.Id;
                    LeadstoUpdate.add(newLead);
                } catch(Exception e) {
                System.debug('No household id or contact id found ' + e);
                  }
            //}
        }
        
        Update LeadstoUpdate;
    }
}

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
Asif Ali MAsif Ali M
Trigger.isAfter is inside the Trigger.isBefore block. close the blocks properly and your afterInsert code will execute.
 
if (Trigger.isBefore) {
      if (Trigger.isInsert) {
          SetLeadRecordType.leadsToUpdate(Trigger.new);
          DuplicateLead.potentialDupes(Trigger.new);
          CloseExpiredLeads.closingExpiredLeads(Trigger.new);
      }
      if (Trigger.isUpdate) {}
      if (Trigger.isDelete) {}
  }
  if (Trigger.IsAfter) {
      if (Trigger.isInsert) {
          AddExistingAccountAndContactToLead.findExistingAccountContact(Trigger.new);
          //AddAccountandContactToLead.addRecodrsToLead(Trigger.New);
      }
      if (Trigger.isUpdate) {}
      if (Trigger.isDelete) {}
  }

 
DbjensenDbjensen
Hi ​Raj - After making the updates, it says it can't update because the record is locked. Looks like I have to move it to a before insert. Does that sound right? 
Maharajan CMaharajan C
Yes you are correct issue is an update on the same record while your process is in the middle of executing so use the before event.

FYI : While use the before Events there is no need to perform any explicit DML Operation.

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
 
This was selected as the best answer
DbjensenDbjensen
Fantastic. I have removed the DML and changed to "before" and it's working as expected. Thanks so much for the help.