The asynchronous process is a very important feature of Salesforce apex. Asynchronous process helps us in doing our long-running operations in the background so that it will not impact application performance. This post is related to Salesforce interview question for asynchronous apex. Let us see the all-important questions of Asynchronous Apex
Q.1 What is Asynchronous Apex?
While working on the Salesforce application, we store huge data in our org and it will keep growing year by year. If we have to run some job that will take some time then in the case of synchronous apex we will get the limit error, heap size error, or timeout error. To avoid such issues, we can do those long-running or time-consuming operations using Asynchronous apex which will run in a separate thread and will not impact the main thread. So Asynchronous Apex is used to run the process in a separate thread at a later time.
Q. 2 Where we can use Asynchronous Apex?
Asynchronous Apex can be used in long-running or time-consuming operations like
- Sending Email to the user.
- Creating complex reports using Apex code
- Calling an external system for multiple records
- Schedule a job for a specific time
- Chaining apex code execution which might use API call
- Sharing Re-calculation
Q3. What are the benefits of Asynchronous processing?
Asynchronous process runs when the system is available for operation. It is not blocking users from doing any work in the application. This will give the below benefit to application.
- Increase User Efficiency
- More Heap Size and Higher Governer limit
- Better Scalability
Q.4 What are the types of Asynchronous Apex?
Currently, Salesforce supports four types of Asynchronous Apex.
- Batch Apex
- Future methods
- Queueable Apex
- Scheduled Apex
Q.5 Within what timeframe will an asynchronous request be processed after being enqueued?
Asynchronous processing has a lower priority than real-time interaction via the browser and API. So it will always run in the background when the resource is available to process asynchronous jobs. We can not determine when the job will run, it will be determined by the server. It can be immediate also.
Q. 6 Can you make a web service callout from a trigger?
We cannot call external web services synchronously from triggers, because calling a web service synchronously from triggers will hold up the database transaction until the callout is completed so we should use future methods to call external web services.
Q. 7 Can a future method call another non-future method to process tasks like callouts, and have those methods return data to the future method for further processing?
No, we can not call future methods from the future method.
Q. 8 What are advantages of Batch Apex?
- Every batch transaction starts with a new set of governor limits.
- The system itself divides the number of batches for records
- If one batch fails, the other batches will continue to be executed, and successful batches will still be committed to the database. Successful batches are not rolled back when one batch fails.
Q. 9 Why use Batch Apex in Salesforce instead of the normal Apex?
There are various reasons why Batch Apex is better than normal Apex.
- SOQL queries: Normal Apex uses 100 records per cycle to execute SOQL queries. Whereas, Batch Apex does the same in 200 records per cycle.
- Retrieval of SOQL queries: Normal Apex can retrieve 50,000 SOQL queries but, in Batch Apex, 50,000,000 SOQL queries can be retrieved.
- Heap size: Normal Apex has a heap size of 6 MB; whereas, Batch Apex has a heap size of 12 MB.
- Errors: When executing bulk records, Normal Apex classes are more vulnerable to encountering errors as compared to Batch Apex.
Q. 10 What are some best practices when implementing batch apex?
- Use normal Apex instead of Batch Apex when a small number of records need to run.
- Use extreme care to invoke a batch job from a trigger. The trigger should not add more batch jobs than the limit
- Methods declared as future aren’t allowed in classes that implement the Database.Batchable interface.
- Methods declared as future can’t be called from a batch Apex class.
- All methods in the class must be defined as global or public.
- Minimize web service callout times.
- Tune queries used in batch Apex code.
- Minimize the number of asynchronous requests created to minimize the chance of delays.
Q. 11 How many Schedulable Apex jobs can you have at one time? In other words, what is the maximum number of Apex classes that can be scheduled concurrently?
100 Schedulable Apex jobs can be scheduled.
Q. 12 Are synchronous web service callouts supported by Scheduled Apex?
No. However, if Scheduled Apex calls a Batch Apex job which then makes a web service callout, the callout will be supported.
Q. 13 What are best practices for future methods?
- every future method invocation adds one request to the asynchronous queue, avoid design patterns that add large numbers of future requests over a short period of time.
- Ensure that future methods execute as fast as possible.
- If using Web service callouts, try to bundle all callouts together from the same future method, rather than using a separate future method for each callout.
- Consider using Batch Apex instead of future methods to process a large number of records asynchronously.
Q. 14 Why sObject parameters not supported in future methods?
The sObject might change between the time we call the method and the time it executes that’s why sObjects can’t be passed as arguments to future methods. In this case, the future method will get the old sObject values and might overwrite them.
Q. 15 Can I chain a job that has implemented allowsCallouts from a Job that doesn’t have?
Yes, callouts are also allowed in chained queueable jobs.
Q. 16 Can I call Queueable from a batch?
Yes, But it is limited to just one System.enqueueJob call per execute in the Database.Batchable class. Salesforce has imposed this limitation to prevent explosive execution.
Q.17 In which scenario, we can’t call a future method from a batch Job?
Calling a future method is not allowed in the Execute method, But a web service can be called. A web service can also call an @future method. So, we can define a web service having a future method invocation and call the web service from the execute method of Batch Job.
Q.18 How Future method helps in avoiding Mixed DML errors?
There are 2 kinds of sObjects in salesforce.
- Non-Setup: Account, opportunity, etc
- Setup: User, groups,Queue etc
If we are performing DML on both kinds of sObject in a single transaction, the system doesn’t allow it and throws an exception called Mixed DML exception, stating that a transaction can not have a Mixture of DML operation(Setup and Non-Setup).
To resolve this error, we can put DML operations of a particular kind in the Future scope. Since both the DML operations are isolated from each other, the transaction doesn’t fail.
Q.19 Let’s say, we have run an apex batch to process 2000 records, and It is running with batch size 200. Now, while doing DML on 298th record, an error occurred, What will happen in that case?
In batches, If the first transaction succeeds but the second fails, the database updates made in the first transaction are not rolled back.
Since the batch size is 200, so the first batch will be processed completely and all data will be committed to DB. In seconds batch, if we are committing records using normal DML statements like insert, update then the whole batch will be rollbacked. So records 201 to 400 will not be processed.
If we use the Database DML operations like Database.insert with AllOrNone as false, then partial commit can happen and only 298th record will not be processed in that batch, and a total of 97 records will be processed. Also, the other batch execution will not stop.
Q.20 What is Database.QueryLocator & Iterable<sObject>?
In Database.QueryLocator, we use a simple query (SELECT) to generate the scope of objects. The governor limit for the total number of records retrieved by SOQL queries is bypassed, i.e. it can return up to 50 million records.
In Iterable<sObject>, we can create a custom scope for processing, which would be not possible to create using SOQL where clauses. the governor limit for the total number of records retrieved by SOQL queries is still enforced.
Similar Posts for Salesforce Interview Question
Salesforce Interview Question for Integration
Salesforce Apex Interview Question
Top 20 Salesforce Interview Question – Integration
Queueable Vs Batch Apex In Salesforce
Avoid Batch Apex and Use Queueable Class
[…] Salesforce Interview Question for Asynchronous Apex […]
[…] Whose Forests? Quiz 3 Build Scalable Solutions with Salesforce Salesforce Interview Question for Asynchronous […]
The interview questions & explanation is easy to understand . Thank you!!
Welcome, Sheetal. I am happy, it is easy to understand.