Concepts or coding lessons of Salesforce that you can implement easily

How To Get Total Amount Of Records Processed In Batch Job In 10 Minutes And Still Look Your Best

how to do it in salesforce
Want count total amount of records which are processed in apex batch job?

Yeah, you can do it. Just implement Database.Stateful in your batch class and then you can have instance variables that don't lose state between batches.


Sample Code : 

global class implements Database.Batchable<sobject>, Database.stateful{

 public String query = 'Select id, Stage from Opportunity' ; 
 public Integer totalAmtOfRecords = 0; 

 global Database.QueryLocator start(Database.BatchableContext BC) {
    return Database.getQueryLocator(query);
 }

 global void execute(Database.BatchableContext BC, List<sobject> scope) {  
    totalAmtOfRecords += scope.size();
    //your logic ...
 }

 global void finish(Database.BatchableContext BC) {
    // Below line print Total amount of records processed in this Batch Job
    system.debug('Total amount of records processed :' + totalAmtOfRecords);  
 }

}


The totalAmtOfRecords stores only the initial state of the class. During execution of the batch job, yYou cannot use it to pass information between instances of the class. 
For example, if you change the value of totalAmtOfRecords in execute, the second chunk of processed records can’t access the new value. Only the initial value is accessible.

Below are the some important things you need to keep in mind while working on a batch class: 


1. Each execution of a batch Apex job is considered an individually separate transaction.
For example, a batch Apex job that contains 10,000 records and is executed without the optional scope parameter is considered fifty transactions of 200 records each.

2. If you specify Database.Stateful in the class, you can maintain state across these transactions. When using Database.Stateful, only instance member variables hold or retain their values between transactions. Static member variables do not hold their values & values are reset between transactions.

3. Maintaining state is useful for counting records as they’re processed.
For example, suppose your job processed opportunity records. You could define a sample method in execute function to perform logic like aggregate totals of the opportunity amounts as they were processed. You can use this count and send mail in final method to the users with job processed count and failure batch job count.

4. If you don’t implement Database.Stateful, all static and instance member variables are reset that is set back to their original values.

Always keep in mind that if your batch iteration seizes too large a number of records than you may still run into governing limits.

Below are the governor limits for Batch Apex:


  • Database.QueryLocator object returns a maximum of 50 million records in one transaction. If more than 50 million records are returned, the batch job is directly terminated or stop working and marked as Failed.
  • You can queue or active 1 to 5 batch jobs concurrently.
  • Apex flex queue holds up to 100 Holding batch jobs at a time.
  • You can submit a maximum of 5 batch jobs, in a running test.
  • Per 24-hour period, 250,000 of batch Apex method executions can be executed, or the number of user licenses in your org multiplied by 200—whichever is a greater number. Method executions contain executions of the start (), execute (), and finish () methods.
  • At a time, the batch Apex start () method can have up to 15 query cursors open per user. The batch Apex executes and finish methods each has a limit of 5 open query cursors per user.
  • If no size is specified with the optional scope parameter of Database.executeBatch, Salesforce divide the records returned by the start method into batches of 200 records. The system then passes each batch which of 200 records to the execute method. Apex governor limits are reset for each execution of execute () method.
  • Maximum 10 callouts can be implemented in the start, execute, and finish methods each.
  • Only one Apex batch job's start method can run at a time in an salesforce org. Apex Batch jobs that haven’t started yet remain in the queue until they are started. Note that if more than one job is running, this limit does not cause any batch job to terminate (fail) and execute methods of batch Apex jobs still run in parallel.

Enjoy! If you have any questions, comments, please feel free to let me know. 
As always, please feel free to get in touch me as I would be more than happy to assist you with any of your Salesforce development needs.


Next post: What Is return type of a SOSL search ,Really?

loading...

No comments:

Post a Comment