+ Start a Discussion
Arthur Almeida 12Arthur Almeida 12 

Batch Apex: class executed multiple times at once?

I have a batch apex class, and this class makes a SELECT with a limit of 1000.
after that, this class creates one campaign using insert, and processes others things, whatever.

the question is when I execute my class, it creates five campaigns in the same hour... how is this possible?
my class creates only one campaign.

the batch apex is executed multiple times at once when the volumetry of data is big?
Best Answer chosen by Arthur Almeida 12
Arthur Almeida 12Arthur Almeida 12
By default, Batchable Apex processes the results from the start method in batches of 200. Presuming you're saying that you create one campaign in the execute method, then you would indeed have five campaigns. If you are using Database.executeBatch, you can provide an optional second parameter to specify a larger processing size, up to 2000.
Database.executeBatch(new MyBatchClass(), 2000);
In this case, with a limit of 1000 records queried, you would end up with just one execute call instead of 5.
 
 User-added image

User-added image
 
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_batch_interface.htm
 
https://salesforce.stackexchange.com/questions/378869/batch-apex-class-executed-multiple-times-at-once/378870#378870

All Answers

VinayVinay (Salesforce Developers) 
Hi Arthur,

Debug logs should narrow down the scenerio why batch apex is running more than once.  Also check below mentioned check points.

https://salesforce.stackexchange.com/questions/216432/guarantee-only-a-single-asynchronous-job-runs-at-a-time/216472#216472

Thanks,
Arthur Almeida 12Arthur Almeida 12
By default, Batchable Apex processes the results from the start method in batches of 200. Presuming you're saying that you create one campaign in the execute method, then you would indeed have five campaigns. If you are using Database.executeBatch, you can provide an optional second parameter to specify a larger processing size, up to 2000.
Database.executeBatch(new MyBatchClass(), 2000);
In this case, with a limit of 1000 records queried, you would end up with just one execute call instead of 5.
 
 User-added image

User-added image
 
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_batch_interface.htm
 
https://salesforce.stackexchange.com/questions/378869/batch-apex-class-executed-multiple-times-at-once/378870#378870
This was selected as the best answer