This blog will take you through 20 scenario-based interview questions about future method scenarios in Salesforce. We’ve curated these questions to give you practical insights into handling asynchronous processing challenges in Apex development discussions.
Firstly, asynchronous processing means it runs in a separate thread in the background and doesn’t provide immediate results. It runs whenever the resources are available.
Before starting with Interview questions, let’s understand the future method.
What is the Future Method?
It runs asynchronously, running in a separate thread whenever resources are available, and it helps in preventing mixed DML errors.
Next, use @future annotations to declare a method as the future method. It helps us to run long-running operations and prevents us from hitting the governor limit.
Syntax:
Let’s move forward with future method scenario-based interview questions.
Question 1. What is mixed DML error and when do we encounter it?
Answer – We encounter this error whenever we are performing DML operations on the Setup and Non-Setup objects in a single transaction due to which sharing is getting recalculated then only we face this error. Let’s take a look at the below code to understand it clearly.
Here, we are inserting Account and User both in a single transaction and we are assigning user-role to that user due to which sharing is calculated as a result mixed DML will be introduced.
Question 2. What are the ways to avoid mixed DML errors?
Answer – To avoid this error we need to convert this process from synchronous to asynchronous. The possible way is to make it a Future Method. It will separate the transaction which help us to avoid this error.
Question 3. You have a requirement to update a large number of records asynchronously in Salesforce. How would you accomplish this?
Answer – To accomplish this scenario I would implement this process asynchronously to avoid hitting the governor limit. In the asynchronous process, I would try it with the future method first if not possible then move to other asynchronous processes we have in Salesforce like Queueable, Batch, and Schedulable.
Question 4. What is the limit on the number of Future Method invocations per transaction?
Answer – In Salesforce, the limit on the number of Future Method invocations per transaction is 50.
Question 5. Can you call a Future Method from another Future Method?
Answer – As of now chaining is not possible, it is a limitation for the future method. We cannot call the future from the future.
Question 6. How do you pass parameters to a Future Method?
Answer – We can pass parameters in future which are of primitive type like Set<Id> or List<Id>. Non-primitive types like List of sObjects are not allowed in future methods because it is an asynchronous process, the sObject can change between the time you call the method and the time it executes. It may be possible that the argument will receive the older version and can overwrite it.
Below is the screenshot of the error if we try to pass primitive types parameter in future methods.
Question 7. How can we track the progress of a Future Method?
Answer – It is not possible to track the progress of future methods as it doesn’t return the JobId.
Question 8. Is it possible to perform DML operations within a Future Method?
Answer – Yes, we can perform the DML operation in future methods. However, we need to follow the best practices and try to avoid hitting the governor’s limit.
Question 9. Can we call Future from Batch?
Answer – No, we cannot call future methods from batch apex. As they operate differently. Mixing these can lead to unexpected behaviour.
Question 10. Why do future methods not return anything?
Answer – As the future is asynchronous, it will execute later when resources are available. So, to avoid any issue related to transactions salesforce doesn’t allow us to return anything in future.
Question 11. How to perform callout in Future Method?
Answer – To do a callout we have to use one extra parameter i.e. (callout=true) while annotating it.
Syntax:
Question 12. Can you schedule a Future Method to execute periodically?
Answer – No, we cannot schedule the future method. They are designed for one-time asynchronous execution triggered by specific events or method invocations
Question 13. Can you enqueue a Future Method from a trigger?
Answer – Yes, we can call the future method from trigger. However, we need to consider the governor’s limit.
Question 14. How do you test a Future Method in Apex?
Answer – To test the asynchronous process we need to call our method in between the Test.startTest() and Test.stopTest() because all asynchronous calls made after the startTest method are collected by the system. When stopTest is executed, all asynchronous processes are run synchronously.
Below is the sample code about how can we test the future method.
Syntax
Question 15. Suppose, we have one class in which I am iterating on records of length 100. In the iteration, I am calling the future method. What will be the outcome?
Answer – As we have a limit i.e. Future Method invocations per transaction is 50, So it will throw an error at 51th records.
Question 16. Suppose I have one trigger working on an update operation on the Contact object and I have written a future call in the operation. Now I have one batch class running on Contact’s records and performing DML on them. Will the future method be invoked after the DML?
Answer – As soon as my DML operation is performed through Batch or Future, It will throw the exception like the Future method cannot be called from a Future or Batch Method.
Question 17. Considering the above scenario how can we handle the above exception?
Answer – To handle the above exception we have the following ways:
- try-catch
- Using System method – We have System.isBatch() and System.isFuture() methods that we can use to avoid this error. Below is an example
Question 18. How many future methods can be defined in a class?
Answer – There is no restriction on it. we can call any number of future methods.
Question 19. What are the limitations of future methods?
Answer – The following are the limitations of future method
- It won’t process large sets of data. To achieve this, Batch Apex will be a good option.
- It doesn’t support chaining.
- It only supports the primitive types.
- Tracing is not possible with future method
- It can’t be called from the Batch class.
Question 20. Is there any other approach to calling a future method from a Batch Class?
Answer – As we can’t directly call a future method from the execute method but we can use a web service that will invoke the future method and that web service will be called from the execute method of the Batch class. So, in that way, we can call the future from a Batch.
Also Read – Batch Apex interview questions and answers
Conclusion
In this blog, we’ve explored a range of future method scenario-based interview questions that have provided invaluable insights. With this knowledge, you’re poised to confidently tackle asynchronous processing challenges and excel in your Salesforce development journey.
One thought on “Future Method Scenario Based Interview Questions in Salesforce”