@future annotation in Salesforce

@future annotation in Salesforce

     Use the future annotation to identify methods that are executed asynchronously. When you specify future, the method executes when Salesforce has available resources.

     For example, you can use the future annotation when making an asynchronous Web service callout to an external service. Without the annotation, the Web service callout is made from the same thread that is executing the Apex code, and no additional processing can occur until the callout is complete (synchronous processing). 
Note:
     1. Methods with the future annotation must be static methods and can only return a void type.
     2. The specified parameters must be primitive data types, arrays of primitive data types, or collections of primitive data types.
     3. Methods with the future annotation cannot take sObjects or objects as arguments.

Things to Remember

  1. Future methods are a great tool, but with great power comes great responsibility. Here are some things to keep in mind when using them:
  2. Methods with the future annotation must be static methods, and can only return a void type.
  3. The specified parameters must be primitive data types, arrays of primitive data types, or collections of primitive data types; future methods can’t take objects as arguments.
  4. Future methods won’t necessarily execute in the same order they are called. In addition, it’s possible that two future methods could run concurrently, which could result in record locking if the two methods were updating the same record.
  5. Future methods can’t be used in Visualforce controllers in getMethodName(), setMethodName(), nor in the constructor.
  6. You can’t call a future method from a future method. Nor can you invoke a trigger that calls a future method while running a future method. See the link in the Resources for preventing recursive future method calls.
  7. The getContent() and getContentAsPDF() methods can’t be used in methods with the future annotation.
  8. You’re limited to 50 future calls per Apex invocation, and there’s an additional limit on the number of calls in a 24-hour period. 

Cheers!!!

Leave a Reply