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
Galeeb SKGaleeb SK 

Batch Apex execution

Question 1:
Hai ,i have a batch apex of size 200,it can be divided into 4 batches each batch is 50.if batch1 is executed ,if it commit to the database? incase if all batches i.e 4 batches are executed after that it commit to the database? Tell me resonable Answer.....if all batchs are executed first after that it commit to the database ok,in that if first batch is executed, where it be stored before  commit to the database?Tell me suitable Answer.... 
Another Question 2:
   if i have 4 batches ,if they are commited to database in that  an error is raised in batch 3,if only batch 3 eliminated OR all batches are eliminated into the database? Tell me with proper scenario with proper code
Best Answer chosen by Galeeb SK
Abhishek BansalAbhishek Bansal
Hi Galeeb,

Just explaining your queries :

Question 1:
Hai ,i have a batch apex of size 200,it can be divided into 4 batches each batch is 50.if batch1 is executed ,if it commit to the database? incase if all batches i.e 4 batches are executed after that it commit to the database? Tell me resonable Answer.....if all batchs are executed first after that it commit to the database ok,in that if first batch is executed, where it be stored before  commit to the database?Tell me suitable Answer.... 

Answer : In order to divide your batch into 4 batches you have to execute the following script in Developer Console or you have to write a scheduler class to run your batch :
Syntax : 

YourBatchClassName b = new YourBatchClassName ();
//Parameters of ExecuteBatch(context,BatchSize)
database.executebatch(b,50);
Now once you execute this statement your batch will automatically run for 4 times and process 50 records at a single time.
Anything which should be commited to database is done in every single run by batch itself.



Another Question 2:
 if i have 4 batches ,if they are commited to database in that  an error is raised in batch 3,if only batch 3 eliminated OR all batches are eliminated into the database? Tell me with proper scenario with proper code

Answer : As your batch will run for 4 times with 50 records in a single run So lets suppose your 1st batch is running and there is a error in any record than your 1st batch will be failed and everything is rolled back.
Than your 2nd batch will run and if no error is there than all 50 records of 2nd batch will be processed completely.
Same thing will happen for 3rd and 4th batch.


Please let me know if you still need any help.

Regards,
Abhishek

 

All Answers

Abhishek BansalAbhishek Bansal
Hi Galeeb :

Let me clear out your queries regarding Batch :
  1. Batches are used to perform a certain action on objects when the nuber of records are greater and can not be handled through a script Or if we want to perform a certain action on records after a particulat interval of time.
  2. Batch divides the number of records into chunks and perform action accordingly. 
  3. In the start method of your batch you will query all the required records related to an Object.In your case you have 200 records.
          Example : Start Method :
          global Database.QueryLocator start(Database.BatchableContext BC){
              return Database.getQueryLocator('Select id from Contact'); //Suppose this query returns 200 revcords.
          }

     4. In Execute Method you will perform the action whic you want to do on the records.

         Example : Execute Method :
        // Execute Logic global
        void execute(Database.BatchableContext BC, List<sObject>scope){
            // Logic to be Executed batch wise
           delete scope;// Suppose you are deleting your records.
         }
     
      5. At last we have finish method which is used to track that whether a batch is run successfully or  not. We can send a mail this method to               the user informing him about the error/success.

Syntax to divide Batch records into chunks.

ExampleBatchClass b = new ExampleBatchClass();
//Parameters of ExecuteBatch(context,BatchSize)
database.executebatch(b,10);


In the above example 10 is the number of records processed in a single chunk.
So if you have 200 records than there will be 20 batches processed.

Since batch is used to perform task in chunks so any which should be commited to Database should be done within the chunks.
If any record in a batch is failed than all the records of that batch is also failed.

Please let me know if you need more information on this.

Regards,
Abhishek


    

 
Galeeb SKGaleeb SK
I did'nt get your point Abhishek tell me once again
Abhishek BansalAbhishek Bansal
Hi Galeeb,

Just explaining your queries :

Question 1:
Hai ,i have a batch apex of size 200,it can be divided into 4 batches each batch is 50.if batch1 is executed ,if it commit to the database? incase if all batches i.e 4 batches are executed after that it commit to the database? Tell me resonable Answer.....if all batchs are executed first after that it commit to the database ok,in that if first batch is executed, where it be stored before  commit to the database?Tell me suitable Answer.... 

Answer : In order to divide your batch into 4 batches you have to execute the following script in Developer Console or you have to write a scheduler class to run your batch :
Syntax : 

YourBatchClassName b = new YourBatchClassName ();
//Parameters of ExecuteBatch(context,BatchSize)
database.executebatch(b,50);
Now once you execute this statement your batch will automatically run for 4 times and process 50 records at a single time.
Anything which should be commited to database is done in every single run by batch itself.



Another Question 2:
 if i have 4 batches ,if they are commited to database in that  an error is raised in batch 3,if only batch 3 eliminated OR all batches are eliminated into the database? Tell me with proper scenario with proper code

Answer : As your batch will run for 4 times with 50 records in a single run So lets suppose your 1st batch is running and there is a error in any record than your 1st batch will be failed and everything is rolled back.
Than your 2nd batch will run and if no error is there than all 50 records of 2nd batch will be processed completely.
Same thing will happen for 3rd and 4th batch.


Please let me know if you still need any help.

Regards,
Abhishek

 
This was selected as the best answer
Galeeb SKGaleeb SK
Thank you Abhishek Bansal ,can give your mail id ,i have any doubt's  forward to you  because iam new  salesforce learner my mail id is skgaleeb777@gmail.com
Abhishek BansalAbhishek Bansal
Its my pleasure helping you.
I have already provided my general information in my Profile.
Its better to post your queries here so that others can also help you but in case of any urgency you can contact me :)

Regards,
Abhishek.