Created
January 9, 2022 11:31
-
-
Save rahulmalhotra/141e8275e008fdd949ffa0b7fadfd4b3 to your computer and use it in GitHub Desktop.
Code used in "How many batches will be executed? 20000 records & 250 batch size | Salesforce Batch Apex Interview" (https://youtu.be/LgHxpR_6Vrs)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-> 'Create a Batch Class to opt out contact records from email, call and fax whose Unsubscribe Date has passed' | |
20,000 records -> total | |
200 records | |
How many batches? | |
-> 20,000 / 200 -> 100 batches in total | |
// * Execute batch | |
Database.executeBatch(new UpdateContactsBatch()); | |
Database.executeBatch(new UpdateContactsBatch(), 75); | |
Secret Mechanism behind Batch Apex | |
----------------------------------- | |
20,000 records -> total | |
250 records | |
Total number of batches = 20,000 / 250 = 80 batches | |
=> 100 batches | |
// * Hypothetical implementation | |
class Database { | |
public static Id executeBatch(Database.Batchable batchableClassInstance) { | |
ApexJob job = new ApexJob(); -> '7070p00000qU6iW' | |
Database.QueryLocator queryLocatorInstance = batchableClassInstance.start(job); | |
setqueryOptions(400); | |
QueryResult result = query(queryLocatorInstance.getQuery()); | |
List<sObject> records = result.records; | |
// * How many records will be returned? -> 400 records | |
batchableClassInstance.execute(job, first 250 records); | |
batchableClassInstance.execute(job, next 150 records); | |
// * First 400 records | |
while(!result.done) { | |
result = queryMore(result.queryLocator); | |
records = result.records; | |
// * next 400 records | |
batchableClassInstance.execute(job, first 250 records); | |
batchableClassInstance.execute(job, next 150 records); | |
} | |
batchableClassInstance.finish(job); | |
} | |
public static Id executeBatch(Database.Batchable batchableClassInstance, Integer scope) { | |
ApexJob job = new ApexJob(); -> '7070p00000qU6iW' | |
Database.QueryLocator queryLocatorInstance = batchableClassInstance.start(job); | |
setqueryOptions(2000); | |
QueryResult result = query(queryLocatorInstance.getQuery()); | |
List<sObject> records = result.records; | |
// * How many records will be returned? -> 2000 records | |
// batchableClassInstance.execute(job, first 500 records); | |
// batchableClassInstance.execute(job, next 500 records); | |
// batchableClassInstance.execute(job, next 500 records); | |
// batchableClassInstance.execute(job, next 500 records); | |
Integer totalRecordsProcessed = 0; // * 750 records | |
List<sObject> recordsToProcess = new List<sObject>(); | |
while(records.size() > totalRecordsProcessed) { | |
recordsToProcess.add(records.get(totalRecordsProcessed)); | |
totalRecordsProcessed++; | |
if(Math.mod(totalRecordsProcessed,scope)==0) { | |
batchableClassInstance.execute(job, recordsToProcess); // * 500 records as scope is 500 | |
recordsToProcess.clear(); | |
} | |
} | |
if(!recordsToProcess.isEmpty()) { | |
batchableClassInstance.execute(job, recordsToProcess); // * 250 records however scope is 500 | |
recordsToProcess.clear(); | |
} | |
// * First 400 records | |
while(!result.done) { | |
result = queryMore(result.queryLocator); | |
records = result.records; | |
// * next 400 records | |
// batchableClassInstance.execute(job, first 200 records); | |
// batchableClassInstance.execute(job, next 200 records); | |
totalRecordsProcessed = 0; | |
while(records.size() > totalRecordsProcessed) { | |
recordsToProcess.add(records.get(totalRecordsProcessed)); | |
totalRecordsProcessed++; | |
if(Math.mod(totalRecordsProcessed,scope)==0) { | |
batchableClassInstance.execute(job, recordsToProcess); // * 500 records as scope is 500 | |
recordsToProcess.clear(); | |
} | |
} | |
if(!recordsToProcess.isEmpty()) { | |
batchableClassInstance.execute(job, recordsToProcess); // * 250 records however scope is 500 | |
recordsToProcess.clear(); | |
} | |
} | |
batchableClassInstance.finish(job); | |
} | |
} | |
class ApexJob implements Database.BatchableContext { | |
public Id getJobId() { | |
} | |
public Id getChildJobId() { | |
} | |
} | |
1 <= batchSize <= 100 -> retrieveChunkSize = 100 | |
101 <= batchSize <= 400 -> retrieveChunkSize = 400 -> | |
401 <= batchSize <= 2000 -> retrieveChunkSize = 2000 | |
-> 500 records | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Code for UpdateContactsBatch class can be found here: https://gist.github.com/rahulmalhotra/89df8331054be604546bb22e1d6e5ee4