You are currently viewing Salesforce Developer Interview Questions Part11 – Batch Apex Continue

Salesforce Developer Interview Questions Part11 – Batch Apex Continue

Salesforce Developer Interview Questions – Asynchronous Apex – Batch Apex Continue

Batch Apex Continue

113. What is the maximum and minimum size of the optional parameter “scope”?

Answer:

Scope parameter can accept a max value of 2000.

Salesforce chunks the records returned by the QueryLocator into smaller batches of up to 2000 records.

If the “start” method of the batch class returns an iterable, the scope parameter values have no upper limit.

The optimal scope size is a factor of 2000 , for example 100, 200, 400 and so on.

The minimum size of the scope parameter is “1”

114. What is the batch size if we don’t use optional scope parameters?

Answer:

The default batch size is 200 records.

115. Do the batches of records execute in the order they are received from the start method?

Answer:

No . Batches of records are not guaranteed to execute in the order they are received from the start method.

116. What is Database.BatchableContext?

Answer:

All methods (start, execute and finish methods) in the batch apex class require a reference or parameter to the Database.BatchableContext object.

Database.Batchablecontext object is used to track the progress of the batch job.

getJobId is the instance method with Database.BatchableContext object

We can use getJobId and query asyncApexJob to get the status of the batch job.

Answer:

Yes. We can perform subquery to get the related records but with a subquery the batch job processing will be slower.

It is recommended to perform the subquery separately from within the execute method which allows the batch job to run faster.

118. Can we use FOR UPDATE in SOQL using Database.QueryLocator?

Answer:

No, we cannot use FOR UPDATE in SOQL using Database.QueryLocator, as it will throw an exception “Locking is implied for each batch execution and therefore FOR UPDATE should not be specified”

The reason we cannot use it is , if we query the entire object (like all account records) using SELECT and FOR UPDATE then we will lock all the account records as long as the batch is active.

119. What is “state” in batch apex? Or What is Database.Stateful?

Answer

Each execution of batch apex is considered a discrete transaction. For example , a batch apex job that contains 1000 records and is executed without the option parameters is considered five transactions of 200 records each.

If we specify database.stateful in the class definition , we can maintain state across these transactions.

When using Database.stateful, only instance member variables retain their values between transactions. Static member variables don’t retain their values and are reset between transactions.

120. Can you give an example of maintaining the state of a batch apex?

Answer:

We can maintain the state of a batch apex for counting or summarizing records as they’re processed.

For example, suppose our job processed opportunity records. We could define a method in “execute” to aggregate totals of the opportunity amounts as they were processed.

121. What will happen if we don’t use Database.Stateful?

Answer:

All static and instance member variables are set back to their original values or all the variables are reset for each transaction of the batch.

122. Can you write a sample batch apex with Database.Stateful?

Answer: 

123. Can we callouts in batch apex?

Answer:

Yes, we can callouts in batch apex. To use callout in batch apex, we have to specify Database.Callouts in the class definition.

Leave a Reply