In this post, we will try to cover the Future method.
Note: Here we will not talk about how Queueable is better than the future method, rather we will stick to knowing about the Future method.
First things first, What is Async Apex and why do we need Async Apex in the first place?
Async Apex is simply used to run your transaction in a different thread at a later point in time when resources are available, without your user actually waiting for it to finish.
Assume if we could make callouts from triggers, in synchronous transaction user would have to wait for the response to come back before he could actually move ahead and that would be a terrible experience.
Luckily we have Asynx Apex methods at our disposal for such things.
There could be a few more scenarios where we might want to use Async Apex.
For example :
- Making callouts to an external system.
- Mixed DML error (We will learn later with an example)
- Any complex calculation which might require more governor limits.
- We need to use @future annotation to mark any method as future.
- We can't have any return type on future methods because these methods are executed when resources are available in future time, if we were to have a return type then the transaction would stop there and wait for returned value.
- We can only pass primitive arguments to a future method for example String, Integer, List<String> etc.
- We cant pass non-primitive arguments like Sobjects as the state of records could change by the time future method is actually executed and we could end up overwriting the record.
- The best way would be to pass List<Id> and then query them to get the latest copy of the record.
- The order of execution of multiple future methods is not defined.
- We cant call a future method from another future method ie chaining is not allowed.If you try to do that you will face an error as "Future method cannot be called from a future or batch method"
- We also can't call from batch class, hence its always advisable to put a check before calling future method like below :
- When looking at debug logs you will see "FutureHandler" in the operation for debug logs, makes it easy to look at debug for future method specifically.
- We can call a future method from Queueable Class and can also call queueable class from future method, well if you are thinking can we also do Future-> Queueable -> Future then yes we can do it and no error will be thrown.
- As we discussed it's not allowed to pass non-primitive data types but still if we want to, we can simply serialize the data which will convert it into String then pass into the method.
- Similarly, if you want to pass wrapper to your future method you can serialize it and pass it then deserialize it inside the future method.
- Future methods are only called if a sync transaction succeeds, in case a sync transaction fails, the future method will never be even executed.
- But in case of future method fails sync transaction will not roll back, considering both are running in different threads any exception in the future transaction will only roll async transaction.
- Similarly in the case of the future method is being called inside another future method sync transaction will succeed and the only async transaction will fail and rollback.
No comments:
Post a Comment