You need to sign in to do that
Don't have an account?
Raghu Cheruvu 1
Count not being maintained in batch apex
Hi,
I am trying to maintain count in batch apex. i am using database.stateful.. but the count is not being maintained... below is the code:-
global class Batch_ConvertLead implements Database.Batchable<sObject>, Database.Stateful {
public String query;
public String rank = 'Ghost App';
global integer Count;
global database.querylocator start(Database.BatchableContext BC){
query = 'SELECT Id, AccountId__c FROM Lead WHERE isconverted = false AND Rank__c = :rank and AccountId__c != null';
return Database.getQueryLocator(query);
Count=0;
}
global void execute(Database.BatchableContext BC, List<sObject> scope){
List<Lead> leadsToConvert = new List<Lead>();
list<Database.LeadConvert> leadConverts = new list<Database.LeadConvert>();
for(sObject leadconv : scope){
leadsToConvert.add((Lead) leadconv);
}
for(lead le :leadsToConvert){
for(Integer i=1;i<leadsToConvert.size();i++){
Database.LeadConvert lc = new database.LeadConvert();
lc.setLeadId(le.Id);
lc.setConvertedStatus('Converted');
lc.setDoNotCreateOpportunity(true);
lc.setAccountId(le.AccountId__c);
leadConverts.add(lc);
Count = count + 1;
}
}
Database.LeadConvertResult[] lcrList = Database.convertLead(leadconverts, true);
for(Database.LeadConvertResult result : lcrlist) {
if(!result.isSuccess()) {
System.debug(result.getErrors());
}
}
}
global void finish(Database.BatchableContext BC){
AsyncApexJob asyJob = [SELECT Id,Status,CompletedDate,TotalJobItems, NumberOfErrors, JobItemsProcessed,CreatedBy.Email FROM AsyncApexJob WHERE Id = :BC.getJobId()];
//String[] toAddresses = new String[] {system.label.Ireland_EmailD_BatchApex};
String[] toAddresses = new String[] {'janardhanan.jagadeesan@hotchalk.com'};
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(toAddresses);
String subject = 'Lead Convert Batch';
mail.setSubject(subject+ '<' + asyJob.status + '>');
mail.setPlainTextBody('The batch Apex job processed ' + asyJob.TotalJobItems + ' batches with a total of ' + count + ' records with '+ asyJob.NumberOfErrors + ' failures.');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
I am trying to maintain count in batch apex. i am using database.stateful.. but the count is not being maintained... below is the code:-
global class Batch_ConvertLead implements Database.Batchable<sObject>, Database.Stateful {
public String query;
public String rank = 'Ghost App';
global integer Count;
global database.querylocator start(Database.BatchableContext BC){
query = 'SELECT Id, AccountId__c FROM Lead WHERE isconverted = false AND Rank__c = :rank and AccountId__c != null';
return Database.getQueryLocator(query);
Count=0;
}
global void execute(Database.BatchableContext BC, List<sObject> scope){
List<Lead> leadsToConvert = new List<Lead>();
list<Database.LeadConvert> leadConverts = new list<Database.LeadConvert>();
for(sObject leadconv : scope){
leadsToConvert.add((Lead) leadconv);
}
for(lead le :leadsToConvert){
for(Integer i=1;i<leadsToConvert.size();i++){
Database.LeadConvert lc = new database.LeadConvert();
lc.setLeadId(le.Id);
lc.setConvertedStatus('Converted');
lc.setDoNotCreateOpportunity(true);
lc.setAccountId(le.AccountId__c);
leadConverts.add(lc);
Count = count + 1;
}
}
Database.LeadConvertResult[] lcrList = Database.convertLead(leadconverts, true);
for(Database.LeadConvertResult result : lcrlist) {
if(!result.isSuccess()) {
System.debug(result.getErrors());
}
}
}
global void finish(Database.BatchableContext BC){
AsyncApexJob asyJob = [SELECT Id,Status,CompletedDate,TotalJobItems, NumberOfErrors, JobItemsProcessed,CreatedBy.Email FROM AsyncApexJob WHERE Id = :BC.getJobId()];
//String[] toAddresses = new String[] {system.label.Ireland_EmailD_BatchApex};
String[] toAddresses = new String[] {'janardhanan.jagadeesan@hotchalk.com'};
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(toAddresses);
String subject = 'Lead Convert Batch';
mail.setSubject(subject+ '<' + asyJob.status + '>');
mail.setPlainTextBody('The batch Apex job processed ' + asyJob.TotalJobItems + ' batches with a total of ' + count + ' records with '+ asyJob.NumberOfErrors + ' failures.');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}