Advantages and Drawbacks of @future Annotation in Apex
It was a relief when Salesforce had come up with @future
annotation.
There were some bottlenecks that @future
was solving and soon it also started showing its age (drawbacks :p). I will save that for another day, maybe.
The bottom rule is, we will convert Synchronous Apex to Asynchronous Apex using @future
annotation which is one of the variants of Asynchronous Apex that Salesforce provides.
Advantages of @future annotation
- We can make a callout from Trigger using
@future
annotation - When there are
CPU time limit exceptions
andSOQL 101 Exceptions
we can use@future
and push the business logic that's not of that priority to get executed in Asynchronous mode. - We can increase the request timeout using
@future
annotation - Scenarios like deleting a record on click of a checkbox and updating a record can only be configured using
@future
annotation.
Drawbacks with @future annotation
- We cannot invoke a future method within another @future method
- Future methods cannot be used in Batch Apex.
- @future method cannot return any data back.
- We cannot pass complex data types(List, Set, and Map), custom data types to a future method. We always have to send primitive data types.