function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sfdc startsfdc start 

plz help me in batch apex.....

1. what is batch apex..?
2.y u r going to batch appex..?
3.step to do batch apex..?
4.what is  Database.Stateful..? eaht the use for it..
5.if record are fail how we find..>
6.what is schedule apex and use and difference between them..?
 
Best Answer chosen by sfdc start
ashish jadhav 9ashish jadhav 9
Hello,

I'm also new to salesforce but would like to give some answers here, you can get more information from other experts as I get.

1. What is batch apex/ why to use it?
  • Batch apex is nothing but a class called as batch job which is used to process such a operation that may cause governer limit.
  • Say for ex. you've 1 million records in account object and then you've to update all of them then you can go for batch apex to avoid governer limit.
  • batch job will be divided into multiple chunks and process seperately. Chunk size can contain max 200 records.
  • default batch size is 200, but you can define your own value which is less than 200.
2. step to do batch apex?
  • you can implement batch job by using database.batchable interface.
  • class should be global and need to implement 3 methods in batch job 1. start 2. execute & 3. finish.
  • in start method we're gathering the data to be process, in execute method we're actually perform our business logic.
  • in finish either you can send mail to concern people stating that batch job finished or do whatever you want to do after you done with your batch apex.
3. step to do batch apex?
  • you need to follow below syntax to write a batch class.
  • declare your class with global keyword and use database.batchable interface.
    1. ​​​global class testbatch implements database.batchable {
    2. // Declare start method as below
    3. }
    4. global database.querylocator start (database.BatchableContext BC) {
    5. // get the data to be process
    6. }
    7. global void execute (database.BatchableContext BC, List<Sobject> ListNameHere) {
    8. // your business logic to be executed here based on start method
    9. }
    10. global void finish (database.BatchableContext BC) {
    11. // Send mail to concern people stating that batch job has finished with the execution.
    12. }
    13. }
4. what is database.stateful?
  • say for example you've created a variable to count the number of record processed then your code will be like this
  • global class implements Database.Batchable<sObject>{
        
        public String query = 'Select id from Account' ;
        
        public Integer total = 0;
        
        
        global Database.QueryLocator start(Database.BatchableContext BC) {
            return Database.getQueryLocator(query);
        }

           global void execute(Database.BatchableContext BC, List<sObject> scope) {
            
            total += scope.size();
            //your magic ....

        }
        
        global void finish(Database.BatchableContext BC) {

            system.debug('Total record processed:' + total);
            
        }
        
    }
  • Now the output of the above batch job is Total record processed: 0  because Apex Batch reset attributes on each iteration. 
    The solution for that is to include the interface Database.Stateful in order to keep the variable status over the whole execution.
    Finally, you’ll have something like this:
  • global class implements Database.Batchable<sObject>, Database.stateful{
        
        public String query = 'Select id from Account' ;
        
        public Integer total = 0;
        
        
        global Database.QueryLocator start(Database.BatchableContext BC) {
            return Database.getQueryLocator(query);
        }

           global void execute(Database.BatchableContext BC, List<sObject> scope) {
            
            total += scope.size();
            //your magic ....

        }
        
        global void finish(Database.BatchableContext BC) {

            system.debug('Total record processed:' + total); //now goes like a charm!

            
        }
        
    }
5. if record are fail how we find?
  • I think you can use exception handling to capture failed record.
6. what is schedule apex and use and difference between them..?
  • schedule apex is basically used to run apex class on specific schedule.
  • an apex class if you want to run at 12AM daily then simply include schedulable interface in your apex class.
  • then go to setting apex classes and schedule accordingly.

I hope this will help... rest other expert will tell you.
 

 

All Answers

ashish jadhav 9ashish jadhav 9
Hello,

I'm also new to salesforce but would like to give some answers here, you can get more information from other experts as I get.

1. What is batch apex/ why to use it?
  • Batch apex is nothing but a class called as batch job which is used to process such a operation that may cause governer limit.
  • Say for ex. you've 1 million records in account object and then you've to update all of them then you can go for batch apex to avoid governer limit.
  • batch job will be divided into multiple chunks and process seperately. Chunk size can contain max 200 records.
  • default batch size is 200, but you can define your own value which is less than 200.
2. step to do batch apex?
  • you can implement batch job by using database.batchable interface.
  • class should be global and need to implement 3 methods in batch job 1. start 2. execute & 3. finish.
  • in start method we're gathering the data to be process, in execute method we're actually perform our business logic.
  • in finish either you can send mail to concern people stating that batch job finished or do whatever you want to do after you done with your batch apex.
3. step to do batch apex?
  • you need to follow below syntax to write a batch class.
  • declare your class with global keyword and use database.batchable interface.
    1. ​​​global class testbatch implements database.batchable {
    2. // Declare start method as below
    3. }
    4. global database.querylocator start (database.BatchableContext BC) {
    5. // get the data to be process
    6. }
    7. global void execute (database.BatchableContext BC, List<Sobject> ListNameHere) {
    8. // your business logic to be executed here based on start method
    9. }
    10. global void finish (database.BatchableContext BC) {
    11. // Send mail to concern people stating that batch job has finished with the execution.
    12. }
    13. }
4. what is database.stateful?
  • say for example you've created a variable to count the number of record processed then your code will be like this
  • global class implements Database.Batchable<sObject>{
        
        public String query = 'Select id from Account' ;
        
        public Integer total = 0;
        
        
        global Database.QueryLocator start(Database.BatchableContext BC) {
            return Database.getQueryLocator(query);
        }

           global void execute(Database.BatchableContext BC, List<sObject> scope) {
            
            total += scope.size();
            //your magic ....

        }
        
        global void finish(Database.BatchableContext BC) {

            system.debug('Total record processed:' + total);
            
        }
        
    }
  • Now the output of the above batch job is Total record processed: 0  because Apex Batch reset attributes on each iteration. 
    The solution for that is to include the interface Database.Stateful in order to keep the variable status over the whole execution.
    Finally, you’ll have something like this:
  • global class implements Database.Batchable<sObject>, Database.stateful{
        
        public String query = 'Select id from Account' ;
        
        public Integer total = 0;
        
        
        global Database.QueryLocator start(Database.BatchableContext BC) {
            return Database.getQueryLocator(query);
        }

           global void execute(Database.BatchableContext BC, List<sObject> scope) {
            
            total += scope.size();
            //your magic ....

        }
        
        global void finish(Database.BatchableContext BC) {

            system.debug('Total record processed:' + total); //now goes like a charm!

            
        }
        
    }
5. if record are fail how we find?
  • I think you can use exception handling to capture failed record.
6. what is schedule apex and use and difference between them..?
  • schedule apex is basically used to run apex class on specific schedule.
  • an apex class if you want to run at 12AM daily then simply include schedulable interface in your apex class.
  • then go to setting apex classes and schedule accordingly.

I hope this will help... rest other expert will tell you.
 

 

This was selected as the best answer
Nitin SharmaNitin Sharma
Go through below link, it will help. 
http://amitsalesforce.blogspot.in/2016/02/batch-apex-in-salesforce-test-class-for.html

Thanks,
Nitin Sharma
TK1234TK1234
Hi Ashis,

Excellent explanation !

I am currently working on the same concept.... can you please see if you can provide any inputs on this....

I want to insert new 60k records using batch apex....I want to pass this number 60k from my visualforce as input.. (so the number i want to insert might vary)

however... In Batch APEX.... all i have seen example for update... get the data as query in Database.QueryLocator and execute the logic to update in execut method....

Now that I want to insert fresh new records... what would be the scope here?

 global void execute(Database.BatchableContext BC, List<Account> scope

I want to pass this 60k in loop and create records with sequence format like opp1,opp2,opp3.... opp60000... somthing like this.....

please let me know if my ques is not clear....