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
abhishek_pandey1989@yahoo.comabhishek_pandey1989@yahoo.com 

Can we insert records in start method of batch class?????

I have I requirement of or you can say situation in which , I have reached a position in which i have to insert the records in the start method of batch Class ......Is it possible ???? If not why,. how to overcome .....If yes what should i keep in mind ..

 

Thanks 

Abhishek 

Best Answer chosen by Admin (Salesforce Developers) 
Avidev9Avidev9
I guess everything is written above clearly!
Let me quote you from there "The query itself can process upto 50million records"

All Answers

Avidev9Avidev9
Yes it is definately possible. Start method is generally used take care of tasks that needs to be executed before the actuall batch processing starts. You have to keep this in mind that governor limits apply for this region.
abhishek_pandey1989@yahoo.comabhishek_pandey1989@yahoo.com

So you mean to say  That 50 million record limit is overcome only in excute method and Database.getquerylocator of batch class, as you are saying limit applies in start method of i think 50,000 records.

Avidev9Avidev9
Generall limit for per transaction are applied. You cannot escape it. 50 million record limit is for the whole batch process and not for each batch it processes.

Each batch should follow the governor limits like, 100 soql, 50K records, 100 DML etc
Avidev9Avidev9

Have a look at this code

 

global class MyBatch implements Database.Batchable<sObject>{
 
   global MyBatch(){
        
   }

   global Database.QueryLocator start(Database.BatchableContext BC){
       //You can place your code here but it should follow the governor limits for EACH batch
//You can have a explicit query here that can pull 50K records(same with other methods)
return Database.getQueryLocator('Your Query');
/*the above query can retrieve 50 million records but they are broken into smaller batches, by default the batch size is 200. These smaller batches are fed to execute method*/ } global void execute(Database.BatchableContext BC,List<Sobject> scope){
//the scope variable gets a max of 200 records from the batch query //Per transaction governor limit applies
} global void finish(Database.BatchableContext BC){ //You can place your code here but it should follow the governor limits for EACH batch } }
abhishek_pandey1989@yahoo.comabhishek_pandey1989@yahoo.com
Didn't got this " for the whole batch process and not for each batch it processes." of yours if you can pls explain It will be very helpful .
abhishek_pandey1989@yahoo.comabhishek_pandey1989@yahoo.com

So where this 50 million limit comes in place .....and can my Database.getQueryLocator return upto 50 million records

Avidev9Avidev9
When you start a batch, the query in the start method fetches records and breaks them into smaller batches of 200 records. And serves the execute method with these batches of records.

The query itself can process upto 50million records.

Edited the batch class structure have a look.
abhishek_pandey1989@yahoo.comabhishek_pandey1989@yahoo.com
So u r saying Database.getQueryLocator can retrieve 50 million records and by passes its own limit of 10000 records , But no other Soql query in batch can retrieve more than 50000 records.
Just say yes or no!!! ... If anything more u have pls put it here .
Thanks
Abhishek
Avidev9Avidev9
I guess everything is written above clearly!
Let me quote you from there "The query itself can process upto 50million records"
This was selected as the best answer
Anubhuti Bansal 17Anubhuti Bansal 17
@Abhishek, that is true in start method of batch context.