@future method interview questions
Future methods are simple, which is exactly why interviewers use them to test the details: what you can pass in, what you cannot call from them, and how you fix a mixed DML error.
On this page (5 sections)
The basicsReal problems future methods solveFuture methods compared with QueueableTesting and troubleshootingBefore your interviewNew to this topic? Learn Apex step by step before you practise.
The @future annotation is the oldest way to run Apex in the background. New code usually uses Queueable Apex instead, but @future still appears in almost every developer interview, for two reasons: large orgs are full of it, and it is the classic fix for two real problems, mixed DML errors and callouts from triggers.
Interviewers use these questions to check that you know the rules and, more importantly, when you would choose something else.
The basics
01What is a future method, and why would you use one?
What is a future method, and why would you use one?
What the interviewer is checking: That you can name a real use, not just 'it runs asynchronously'. Mixed DML and trigger callouts are the two answers they hope to hear.
A future method is a static method marked @future that Salesforce runs later, in its own transaction, when resources are free. The calling code does not wait for it.
You use it for three jobs:
- Making a callout from a trigger, where callouts are not allowed directly.
- Avoiding a mixed DML error, by moving the setup-object update (for example a User) into its own transaction.
- Moving slow work out of the user's save, so the page responds faster. Future methods also get higher limits, for example 200 SOQL queries instead of 100.
02What are the rules a future method must follow?
What are the rules a future method must follow?
- It must be
staticand returnvoid. - Its parameters can only be primitive types (Id, String, Integer and so on) or collections of them. No sObjects or custom objects.
- It must say
@future(callout=true)if it makes a callout. - You cannot call a future method from another future method or from Batch Apex.
- There is no guaranteed order. Two future methods called one after the other can run in either order.
03Why can't you pass an sObject to a future method?
Why can't you pass an sObject to a future method?
Because the record could change between the time you call the method and the time it runs, which can be seconds or minutes later. If you passed the record itself, the method would work with old values and could overwrite newer changes.
So you pass the record IDs, and query the records at the start of the future method. That way the method always works with the current data.
@future(callout=true)
public static void syncAccounts(Set<Id> accountIds) {
List<Account> accs = [SELECT Id, Name, BillingCountry
FROM Account WHERE Id IN :accountIds];
// build the request and call the external system
}Common mistake: Saying it is just 'a platform restriction'. The reason, stale data, is what the interviewer wants.
04What are the limits on future methods?
What are the limits on future methods?
You can call up to 50 future methods in one transaction. Across the org, future methods share the daily asynchronous Apex limit with Queueable, Batch and Scheduled Apex: 250,000 executions a day, or 200 times the number of user licences if that is higher.
Inside the future method you get the higher asynchronous limits, such as 200 SOQL queries, a 12 MB heap and 60 seconds of CPU time.
Real problems future methods solve
05What is a mixed DML error, and how does a future method fix it?
What is a mixed DML error, and how does a future method fix it?
What the interviewer is checking: Hands-on experience. People who have hit this error explain it with a concrete example like this one.
Salesforce does not let one transaction change a setup object (such as User, Group, PermissionSetAssignment or GroupMember) and a non-setup object (such as Account or Contact) together, because sharing has to be recalculated. Doing so gives MIXED_DML_OPERATION.
A common example: when a Contact is marked as an employee, create a User for them. The fix is to do the Contact work in the trigger and pass the Contact IDs to a future method that creates the User in a separate transaction. A Queueable job works the same way.
06A trigger needs to send new accounts to an external system. Why use a future method, and what can go wrong?
A trigger needs to send new accounts to an external system. Why use a future method, and what can go wrong?
Triggers cannot make callouts, because the database transaction is still open and Salesforce will not hold it while it waits for another system. So the trigger collects the account IDs and passes them to a @future(callout=true) method.
What can go wrong:
- If 300 accounts arrive in one load, a careless trigger calls the future method once per record and hits the limit of 50. Call it once, with all the IDs.
- If the external system is down, the future method fails and nothing retries it. You need a way to log failures and resend, for example a status field on the record or a retry job.
- If the future method updates the same accounts, the trigger runs again and could call the future method again. Guard against that with
System.isFuture().
Common mistake: Calling the future method inside the trigger's for loop. It works with one record and fails on the first data load.
07How do you stop a future method from calling itself again through a trigger?
How do you stop a future method from calling itself again through a trigger?
When the future method updates records, the trigger on those records fires again. If the trigger calls the future method, you would try to call a future method from a future method, which throws an error. Check System.isFuture() in the trigger handler, and skip the async call when it is true. System.isBatch() and System.isQueueable() do the same for the other async types.
Future methods compared with Queueable
08What are the main differences between @future and Queueable Apex?
What are the main differences between @future and Queueable Apex?
What the interviewer is checking: Whether you would still choose @future for new work. A good answer is 'rarely, and here is when'.
| @future | Queueable | |
|---|---|---|
| Parameters | Primitives and collections only | Any type, including sObjects, through the class constructor |
| Job ID | None, so it is hard to track | System.enqueueJob() returns an ID you can query in AsyncApexJob |
| Chaining | Cannot call another future method | Can enqueue the next job from execute() |
| Callouts | callout=true | Implement Database.AllowsCallouts |
| Delay | Runs when resources are free | Can be delayed by 0 to 10 minutes |
For new code, Queueable is the better default. @future is still fine for a small, single job where none of these features are needed.
09Is @future deprecated? When would you still use it?
Is @future deprecated? When would you still use it?
No, it is not deprecated, and existing future methods keep working. Salesforce recommends Queueable for new work because it does everything @future does and more.
I would still accept @future for a short, simple job, such as one callout with a few IDs, where nobody needs to track the job or chain another job after it. When I touch old @future code for another reason, I usually move it to Queueable at the same time.
10How would you move an existing future method to Queueable?
How would you move an existing future method to Queueable?
- Create a class that implements
Queueable(andDatabase.AllowsCalloutsif it makes callouts). - Move the method body into
execute(QueueableContext ctx), and pass the data through the constructor. You can now pass records, not just IDs, though IDs are still safer for data that may change. - Replace the call with
System.enqueueJob(new MyJob(ids)), and store or log the returned job ID. - Update the tests, and check that callers do not enqueue it inside a loop: the limit for Queueable jobs from one synchronous transaction is 50.
Testing and troubleshooting
11How do you test a future method?
How do you test a future method?
Call the code that starts it between Test.startTest() and Test.stopTest(). When stopTest() runs, the future method runs too, so the lines after it can query the records and assert the result. If it makes a callout, register an HttpCalloutMock with Test.setMock() first.
12Users say a future method 'did not run'. How do you investigate?
Users say a future method 'did not run'. How do you investigate?
- Check Setup, Apex Jobs, or query
AsyncApexJobwithJobType = 'Future', to see whether it queued, failed or is still waiting. - If it failed, read the error in the job record and in the Apex exception email.
- If it never queued, check that the calling code reached it: an exception earlier in the transaction rolls back the whole transaction, and the future call with it.
- If many jobs are waiting, the org may be close to its daily async limit, or the queue is busy. Future methods have no guaranteed start time.
Common mistake: Forgetting that a future call is cancelled if the transaction that called it fails. The job is only queued when the calling transaction commits.
13Can a future method be called from a flow or a Visualforce page?
Can a future method be called from a flow or a Visualforce page?
Not directly from a flow: a flow calls Apex through an @InvocableMethod, which can then call the future method, though a flow can run work in the background on its own with an asynchronous path. From Visualforce, you cannot call a future method in a getter, setter or the controller constructor, but you can call one from an action method.
Before your interview
If you remember one thing, make it the reason behind each rule: IDs because data changes, no chaining because jobs could multiply without limit, no callouts in triggers because the transaction is still open. Interviewers forgive a forgotten number; they rarely forgive a rule you cannot explain.
- Compare every async option on our Async Apex questions page.
- Go deeper on the modern options: Queueable Apex and Batch Apex.
- Callouts in detail: integration interview questions.
- Then practise the whole round in a timed mock interview.