Salesforce Developer Interview Questions – Asynchronous Apex – Batch Apex Continue2
Batch Apex Continue2
Table of Contents
124. Given a scenario , we have to process a batch apex with 1000 records with 200 records in each transaction and the last transaction failed while doing the DML operation. What will happen to the processed records?
If all the first 4 transactions succeed but the last fails, then the database updates made in the first 4 transactions are not rolled back.
Since the batch size is 200, the first 4 batches will be processed completely, and all the data will be committed to the database. In the last batch, if we are committing records using normal DML statements like insert, update then the whole batch will rollback. So, the records from 801 to 1000 will not be processed.
If we use the database DML operations like Database.insert with AllorNone as false, then a partial commit can happen and only the failed record will not be processed in that batch and the rest of the records will be processed.
125. Can we call a future method from a Batch apex?
No, we cannot call a future method from a batch apex.
126. How to stop or abort a Batch job?
Get the id of the batch job by running a SOQL query on AsyncApexJob and use a system.aboartJob(jobId) to abort the job.
127. What is the apex flex queue?
Normally an org can process 5 jobs at a time and if we add more then 5 jobs at a time then it goes into the apex flex queue.
Let’s say we have added 15 jobs at a time then 5 jobs will be processed, and 20 jobs will go into the apex flex queue.
128. What is the maximum limit of the apex flex queue?
Apex flex queue can hold up to 100 jobs at a time.
129. What would happen if we added more than 100 jobs like 125 jobs at a time ?
The first 5 jobs will be processed and next 100 will be queued and the rest of the 20 jobs will be rejected or not added into the queue.
130. What will be the status of the job in the apex flex queue?
All the jobs in the apex flex queue will have status as “Holding”
131. Can we change the order of already queued Batch jobs?
Only jobs having status as “Holding” can be moved. It can be done though UI of apex flex queue or we can use Apex Flex Queue Methods.
132. How can we schedule a batch apex?
We can schedule a Batch apex using system.scheduleBatch Method. We can schedule a batch for once at a future time. This method returns the scheduled job ID also called as CronTrigger Id
133. How can we test a batch apex?
When testing batch apex, we can test only one execution of the “execute” method. So, use the scope parameter of the “executeBatch” method to limit the number of records passed into the execute method to ensure that we aren’t running into governor limits.
Use the test methods startTest() and StopTest() around the executeBatch method to ensure that it finishes before continuing tests.
All asynchronous calls made after the startTest() method are collected by the system. When StopTest() is executed, all asynchronous processes are run synchronously. If we don’t include the executeBatch method within the starTest() and StopTest() methods, the batch job executes at the end of the test method.