Salesforce Future Annotation

Salesforce Future Annotation, future annotation example, salesforce tutorial, future method in salesforce,salesforce future method,Avoiding Mixed DML Operations Exception Example

Salesforce future annotation is used to run apex methods in asynchronous mode, apex methods which are defined with future annotation will run when salesforce has resource available.

Example :-

global class ClassFuture{
@future 
  static void futureMethod(String a, Integer i) {
  System.debug('future Method called with: ' + a + ' and ' + i);
  // long-running code logic here
  }}

In more simple way, Future methods will run in unique thread which in turn have no dependency during its execution.Future methods accepts only primitive type parameters and non-primitve data types like sObjects are not accepted as parameters.

@future(callout =true) :-

You can also make a webservice callout from future method by specifying ‘callout=true’ and be default callout value is false.

 @future (callout=true)
 public static void futureCallout() {
 //Add code to perform callout
}

Future Method Limitations :-

  • Future methods don’t have any execution order
  • Future methods can’t be used in getter/setter methods and constructors.
  • You can’t call a future method from another future method and batch method, if you try accessing it then you will be running into below system error
'System.AsyncException: Future method cannot be called from a future or batch method'

Click here for an Example on avoiding MIXED_DML_OPERATION exception using Future annotation

Be the first to comment

Leave a Reply

Your email address will not be published.


*