You need to sign in to do that
Don't have an account?
Dbjensen
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);
}
//}
}
}
}
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);
}
//}
}
}
}
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
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
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