+ Start a Discussion
buggs sfdcbuggs sfdc 

BATCH APEX TO COUNT THE LEADHISTORY YEARLY WISE

HI 
can anyone help me out in writing me a bacth apex to count the LeadHistory data on yearly basis,i have a requirment to count the total number of (Createddate Column )LeadHistory data yearly wise, through developer console i can able to count the total number of LeadHistory,but when am trying to count it on yearly basis,the server is timing out,so i hope for the yearly count we have to write a batch apex ..suppose i want last 5 years data so here we have to create a table with 5 coloumns and we can update that count in that particular table,please advise me wheather am on right track?? thanks in advances..really apprecaite quick responses.
Tammer SalemTammer Salem
How often is this batch going to run? Are you storing the results somewhere?

(It might be easier to use [select count() from Lead where ...] and set the where condition between the desired dates, but this will be a slow process (so batch run in the evening and store the result somewhere). Otherwise use a report?
buggs sfdcbuggs sfdc

HI Tameer 

Thanks for the reply,can you help me out with the following code to solve with batch apex.here am trying to query from LeadHistory Table and want to show that count on another table(Project__c),so here am created 2 columns like X2011__c,X2012_c, and need to show that count in this particular columns,please help me out in resolving this issue.

global class LeadHisCount implements Database.Batchable <sobject> {

global Database.queryLocator start(Database.BatchableContext bc) {

return Database.getQueryLocator('select calendar_year(createddate), calendar_month(createddate) from LeadHistory group by calendar_year(createddate), calendar_month(createddate) order by calendar_year(createddate),calendar_month(createddate)');

}

global void execute(Database.BatchableContext bc, LIST< Project__c > LHis) {

List< Project__c > depUPdLst = new List< Project__c >();

for(Project__c LH: LHis) {

Project__c LHT = (Project__c) LH;//Type Casting.

//LHT.X2011__c = 'Count1';

//LHT.X2012__c = 'Count';

depUPdLst.add(LHT);

 }

if(depUPdLst.size() > 0)

update depUPdLst;

}

global void finish(Database.BatchableContext bc) {

//Post Commit logic like sending emails for the success or failures of the batches.

AsyncApexJob a = [select id, Status, NumberOfErrors,JobItemsProcessed,TotalJobItems, CreatedBy.Email from AsyncApexJob where id =: bc.getJobId()];

Messaging.singleEmailMessage mail = new Messaging.singleEmailMessage();   

mail.setToAddresses(new String[]{a.CreatedBy.Email});

mail.setSubject('Batch Class Result');

mail.setHtmlBody('The batch Apex job processed ' + '<b>' + a.TotalJobItems + '</b>' +

' batches with '+ '<b>' + a.NumberOfErrors + '</b>' + ' failures.');

//sendEmail methods

Messaging.sendEmail(new Messaging.singleEmailMessage[]{mail});

/*** Scheduling in minutes or hours ***/

/*//Create object for schedulable class

SchedulableUsage su = new SchedulableUsage();

//Preparing chron_exp

Datetime sysTime = System.now();

sysTime = sysTime.addminutes(6);

String chron_exp = '' + sysTime.second() + ' ' + sysTime.minute() + ' ' +

sysTime.hour() + ' ' + sysTime.day() + ' ' + sysTime.month() + ' ? ' + sysTime.year();            

System.schedule('Dep Update'+sysTime.getTime(),chron_exp, su);*/

}  

}
Tammer SalemTammer Salem
you could simply get the size of the list. I assume here that you are looping on every record in the query and adding the total size to the column in that record. What doesn't make sense here is that there is no range (I would assume that you want to query between two dates, rather than getting all records back). Also remember you have limits in the amount of records you can query (50,000)
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_gov_limits.htm
 
global void execute(Database.BatchableContext bc, LIST< Project__c > LHis) {

List< Project__c > depUPdLst = new List< Project__c >();

for(Project__c LH: LHis) {

    Project__c LHT = (Project__c) LH;//Type Casting.

    LHT.X2011__c = LHis.size();

    LHT.X2012__c = LHis.size();

    depUPdLst.add(LHT);

 }


 
buggs sfdcbuggs sfdc
Thanks a tonn tammer for quick reply.but my LeadHistory is more than 1.5 crore so now how and where can this count??can you please help  me out in finding a solution for it.