You need to sign in to do that
Don't have an account?
Michael Clarke 16
Auto Convert Lead Error (Bulkified)
I have bulkified my Auto Lead Convert apex class and written a trigger.
It works fine for one Lead updated via the UI.
However when I create Leads in the Open Execute Anaopnymous Window I get an error.
Trigger:
Your help solving this would be very much appreciated.
It works fine for one Lead updated via the UI.
However when I create Leads in the Open Execute Anaopnymous Window I get an error.
Trigger:
trigger LeadTrigger on Lead (before insert, after insert, before update, after update, before delete, after delete) { If(trigger.isAfter && (trigger.isInsert || trigger.isUpdate)) { List<Id> LeadIds = new List<Id>(); Integer i = 0, l = 0; system.debug('Trigger size: '+trigger.new.size()); for(Lead thisLead:trigger.new) //loop through all inserted or updated leads { l = l + 1; system.debug('Check that Lead is not converted and meets criteria.'); if(!thisLead.IsConverted && thisLead.LeadSource == 'Web' && thisLead.Rating == 'Hot') //if current lead matches criteria { system.debug('Lead '+i+': '+thisLead.Id+' is not converted and meets criteria.'); LeadIds.add(thisLead.Id); //add Lead to LeadIds list i = i + 1; } else { system.debug('Lead'); } } system.debug(l+' Leads inserted or updated.'); if(LeadIds.size()>0) { AutoMassConvertLeads.LeadAssign(LeadIds); } system.debug(i+' Leads converted.'); } }Bulkified Auto Convert Lead Apex Class:
Public class AutoMassConvertLeads { @InvocableMethod public static void LeadAssign(List<Id> LeadIds) { system.debug('Select the Status Label for a converted Lead'); LeadStatus CLeadStatus= [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true Limit 1]; system.debug('Create Database.LeadConvert List'); List<Database.LeadConvert> MassLeadconvert = new List<Database.LeadConvert>(); for(id currentlead: LeadIds){ system.debug('Create Database.LeadConvert for current Lead'); Database.LeadConvert Leadconvert = new Database.LeadConvert(); system.debug('Set Lead Id for Lead Conversion for current Lead'); Leadconvert.setLeadId(currentlead); system.debug('Change status of the Lead to converted for current Lead'); Leadconvert.setConvertedStatus(CLeadStatus.MasterLabel); //Leadconvert.setDoNotCreateOpportunity(TRUE); //Remove this line if you want to create an opportunity from Lead Conversion system.debug('Add current Lead Conversion to Lead Conversion List'); MassLeadconvert.add(Leadconvert); } Boolean LCR_Success = TRUE; Integer Count_LCR_Success = 0; Integer Count_LCR_Failures = 0; if (!MassLeadconvert.isEmpty()) { system.debug('Convert all Leads and store in LeadConvert Result List lcr'); List<Database.LeadConvertResult> lcrs = Database.convertLead(MassLeadconvert); system.debug('Confirm Success of Lead conversion'); for(Database.LeadConvertResult lcr:lcrs){ if(lcr.isSuccess()){ Count_LCR_Success = Count_LCR_Success + 1; } else { Count_LCR_Failures = Count_LCR_Failures + 1; } } system.debug('Lead Conversion process complete'); system.debug(Count_LCR_Success+' successes'); system.debug(Count_LCR_Failures+' failures'); } } }Execute Anonymous Window:
Lead newLead = new Lead(); List<Lead> newLeads = new List<Lead>(); newLead.FirstName = 'TestFirstName11'; newLead.LastName = 'TestLastName11'; newLead.Company = 'TestCompany11'; newLead.LeadSource = 'Web'; newLead.Rating = 'Warm'; newLead.Status = 'Open - Not Contacted'; newLead.Email = 'test11@testdwr.com.zz'; system.debug('Lead Company 11: '+newLead.Company); newLeads.add(newLead); system.debug('newLead11 added to newLeads'); system.debug(newLeads[0]); newLead.FirstName = 'TestFirstName22'; newLead.LastName = 'TestLastName22'; newLead.Company = 'TestCompany22'; newLead.LeadSource = 'Web'; newLead.Rating = 'Hot'; newLead.Status = 'Open - Not Contacted'; newLead.Email = 'test22@testdwr.com.zz'; newLead.Rating = 'Hot'; newLeads.add(newLead); system.debug('newLead22 added to newLeads'); system.debug(newLeads[1]); newLead.FirstName = 'TestFirstName33'; newLead.LastName = 'TestLastName33'; newLead.Company = 'TestCompany33'; newLead.LeadSource = 'Web'; newLead.Rating = 'Hot'; newLead.Status = 'Open - Not Contacted'; newLead.Email = 'test33@testdwr.com.zz'; newLead.Rating = 'Hot'; newLeads.add(newLead); system.debug('newLead33 added to newLeads'); system.debug(newLeads[2]); insert newLeads;Log:
Your help solving this would be very much appreciated.
This worked.