You need to sign in to do that
Don't have an account?
Gus proto
convert trigger into batch apex class
How do I convert this trigger into a batch apex class so i can process over 5 millions record,
Trigger AutoConverter on Lead (after insert,after update) { LeadStatus convertStatus = [ select MasterLabel from LeadStatus where IsConverted = true limit 1 ]; List<Database.LeadConvert> leadConverts = new List<Database.LeadConvert>(); for (Lead lead: Trigger.new) { if (!lead.isConverted ) { Database.LeadConvert lc = new Database.LeadConvert(); lc.setLeadId(lead.Id); lc.setDoNotCreateOpportunity(TRUE); lc.setConvertedStatus(convertStatus.MasterLabel); String leadOwner = lead.OwnerId; if(leadOwner.startsWith('00G')){ Lc.OwnerId = '00535000001ZOZN'; } leadConverts.add(lc); } } if (!leadConverts.isEmpty()) { List<Database.LeadConvertResult> lcr = Database.convertLead(leadConverts); } }
I am including 2 codes one with schedule and the other one without a schedule.
This includes schedulable as well.
global class AutoConverterBatch implements Database.Batchable<SObject>, Database.RaisesPlatformEvents, Schedulable {
private final String CONVERTED_STATUS = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1].MasterLabel;
// Empty constructor
global AutoConverterBatch() {
}
// Start Method
global Database.QueryLocator start(Database.BatchableContext ctx){
String query = 'SELECT Id FROM Lead WHERE ConvertedContactId = null';
return Database.getQueryLocator(query);
}
// Excute Method
global void execute(Database.BatchableContext ctx, List<Lead> leads){
List<Database.LeadConvert> leadConverts = new List<Database.LeadConvert>();
// Loop to iterate over the list of Leads
for(Lead lead : Leads) {
Database.LeadConvert lc = new Database.LeadConvert();
lc.setLeadId(lead.Id);
lc.setDoNotCreateOpportunity(TRUE);
lc.setConvertedStatus(CONVERTED_STATUS);
String leadOwner = lead.OwnerId;
// Condition to check for the owner Id
if(leadOwner.startsWith('00G')){
lead.OwnerId = '00535000001ZOZN';
}
leadConverts.add(lc); //adding to list
}
if (!leads.isEmpty()) {
Database.convertLead(leadConverts, true);
}
}
//Finish Method
public void finish(Database.BatchableContext ctx){
}
// Schedle Apex
global void execute(System.SchedulableContext SC) {
Database.executeBatch(new AutoConverterBatch(), 200);
}
}
If you don't need schedule part then consider this code
global class AutoConverterBatch implements Database.Batchable<SObject>, Database.RaisesPlatformEvents {
private final String CONVERTED_STATUS = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1].MasterLabel;
// Empty constructor
global AutoConverterBatch() {
}
// Start Method
global Database.QueryLocator start(Database.BatchableContext ctx){
String query = 'SELECT Id FROM Lead WHERE ConvertedContactId = null';
return Database.getQueryLocator(query);
}
// Excute Method
global void execute(Database.BatchableContext ctx, List<Lead> leads){
List<Database.LeadConvert> leadConverts = new List<Database.LeadConvert>();
// Loop to iterate over the list of Leads
for(Lead lead : Leads) {
Database.LeadConvert lc = new Database.LeadConvert();
lc.setLeadId(lead.Id);
lc.setDoNotCreateOpportunity(TRUE);
lc.setConvertedStatus(CONVERTED_STATUS);
String leadOwner = lead.OwnerId;
// Condition to check for the owner Id
if(leadOwner.startsWith('00G')){
lead.OwnerId = '00535000001ZOZN';
}
leadConverts.add(lc); //adding to list
}
if (!leads.isEmpty()) {
Database.convertLead(leadConverts, true);
}
}
//Finish Method
public void finish(Database.BatchableContext ctx){
}
}
String query = 'SELECT Id, OwnerId
FROM Lead WHERE ConvertedContactId = null';
Thanks