You need to sign in to do that
Don't have an account?
PhoenixRising12
Apex Trigger and Trigger Helper Class isn't firing
I have an Apex Trigger that's not firing. This Trigger is supposed to call a helper class which then merges Leads into existing accounts if the email already exists in the Salesforce org. I have the trigger and Class here, what can I change to make it work properly without errors?
Trigger:
trigger DuplicateMergeTrigger on Lead (after Insert) {
if (Trigger.isInsert){
Set<Id> LeadIds1 = new Set<Id>();
for (Lead myLead : Trigger.New){
if(myLead.Isconverted == FALSE && myLead.College_Corps__c == TRUE && myLead.Status == 'New'){
LeadIds1.add(myLead.Id);
}
if(LeadIds1.size() > 0){
LeadConversionUtils.ConvertLeads(Trigger.new);
}
}
}
}
Helper Class
public class LeadConversionUtils {
public static void ConvertLeads(List<Lead> leadIds){
LeadStatus convertedStatus = [SELECT Id, MasterLabel FROM Leadstatus WHERE IsConverted = TRUE LIMIT 1];
List<Lead> LeadList = new List<Lead>();
LeadList = [SELECT Id, Email FROM Lead WHERE IsConverted = FALSE];
List<String> LeadEmails = new List<String>();
for (Lead VarL : LeadList){
if(VarL.Email != NULL){
LeadEmails.add(VarL.Email);
}
}
List<String> AccountEmails = new List<String>();
List<Id> AccountIds2 = new List<Id>();
for(Account VarA : [SELECT Id, PersonEmail FROM Account WHERE PersonEmail IN :LeadEmails]){
AccountEmails.add(VarA.PersonEmail);
AccountIds2.Add(VarA.Id);
}
if(AccountIds2.size() > 0){
Id AccountId = AccountIds2[0];
for (lead MyLead : leadList){
if(myLead.Email != NULL && !AccountEmails.contains(myLead.Email)){
Database.LeadConvert lc = new database.LeadConvert();
lc.setLeadId(MyLead.Id);
lc.setAccountId(AccountId);
lc.setConvertedStatus(convertedStatus.MasterLabel);
lc.setDoNotCreateOpportunity(TRUE);
}
}
}
}
}
Trigger:
trigger DuplicateMergeTrigger on Lead (after Insert) {
if (Trigger.isInsert){
Set<Id> LeadIds1 = new Set<Id>();
for (Lead myLead : Trigger.New){
if(myLead.Isconverted == FALSE && myLead.College_Corps__c == TRUE && myLead.Status == 'New'){
LeadIds1.add(myLead.Id);
}
if(LeadIds1.size() > 0){
LeadConversionUtils.ConvertLeads(Trigger.new);
}
}
}
}
Helper Class
public class LeadConversionUtils {
public static void ConvertLeads(List<Lead> leadIds){
LeadStatus convertedStatus = [SELECT Id, MasterLabel FROM Leadstatus WHERE IsConverted = TRUE LIMIT 1];
List<Lead> LeadList = new List<Lead>();
LeadList = [SELECT Id, Email FROM Lead WHERE IsConverted = FALSE];
List<String> LeadEmails = new List<String>();
for (Lead VarL : LeadList){
if(VarL.Email != NULL){
LeadEmails.add(VarL.Email);
}
}
List<String> AccountEmails = new List<String>();
List<Id> AccountIds2 = new List<Id>();
for(Account VarA : [SELECT Id, PersonEmail FROM Account WHERE PersonEmail IN :LeadEmails]){
AccountEmails.add(VarA.PersonEmail);
AccountIds2.Add(VarA.Id);
}
if(AccountIds2.size() > 0){
Id AccountId = AccountIds2[0];
for (lead MyLead : leadList){
if(myLead.Email != NULL && !AccountEmails.contains(myLead.Email)){
Database.LeadConvert lc = new database.LeadConvert();
lc.setLeadId(MyLead.Id);
lc.setAccountId(AccountId);
lc.setConvertedStatus(convertedStatus.MasterLabel);
lc.setDoNotCreateOpportunity(TRUE);
}
}
}
}
}
The below line is missing in your handler class:
Database.LeadConvertResult lcr = Database.convertLead(lc);
https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_dml_convertLead.htm
Updated helper Class:
Thanks,
Maharajan.C
All Answers
Thanks,
The below line is missing in your handler class:
Database.LeadConvertResult lcr = Database.convertLead(lc);
https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_dml_convertLead.htm
Updated helper Class:
Thanks,
Maharajan.C