In Salesforce usually we write a trigger for an object, and then from the trigger we call a method on a class. If we need to make call out to an external web service from the trigger we get “System.CalloutException: Callout from triggers are currently not supported”. @future method with callout=true is the way to make this happen.
@future method executes asynchronously. When you specify @future annotation, the method executes on a separate thread when Salesforce has available resources. The future call basically acts like a scheduled job that has been kicked off. The method is placed in a queue and is kicked off as resources are available.
@future has many purposes:
- Making external callouts from Apex trigger.
- Allow us to control the governor limit by starting a new/separate thread.
- Avoid MIXED_DML_OPERATION error, by separating a thread to performing DML on Setup and NonSetup objects.
See the example below. You can use the @future annotation with callout=true when making an asynchronous Web service callout to an external service.
@future(callout=true) static void doCallout(){ //do callout }
Key points to remember about @future method :
- No more than 10 @future method calls per Apex invocation.
- You cannot call a @future method from another @future method.
- The parameters specified must be primitive data types, arrays of primitive data types, or collections of primitive data types.
- Methods with the future annotation cannot take sObjects or objects as arguments.
- Methods with the future annotation cannot be used in Visualforce controllers in either getMethodName or setMethodName methods, nor in the constructor.
- @future(callout = true) means that the method has ability to invoke external web services.
Author:
![]() |
AJOMON JOSEPH Senior Salesforce Architect ![]() ![]() ![]() ![]() |