Saturday, October 6, 2018

Future method in salesforce

Future method are annoted with @future annotation which indicates that the method will run
asynchronously(i.e in future when resources are available to it). Normal method runs as soon as
it executes in same thread but future method runs in different thread. Future method always require id's as parameter, Objects are not allowed as there is a possibility that when future method runs data might have changed which can lead to processing on wrong data.

When to use future methods?

  1. When you are having a callout to an external system.
  2. To avoid mixed dml error. (Mixed dml error occurs when you try to do dml operations on setup object(Ex: user role) and other objects in the same transaction).

Points to remember while using the future method:

1) The future method is always static with the void return type.
2) The future method always requires id's as parameter, Objects are not allowed.
3) Order of execution is not guaranteed with the future method.
4) When making a callout to external system future method is annotated with(callout=true).
5) Future method can’t invoke another future method.

Syntax of future method:


global class Myclass{

@future
public static void futureMethodName(list<object> id)
{
// Operations here
}
}

Test class for future method:


To test future method we need to call future method between test.startTest() and test.stopTest().
When test.startTest() runs data is collected asynchronously and as soon as a test.stopTest runs data collected is run synchronously.

test.startTest();

myClass.futureMethodName(id);

test.stopTest();

How to call a future method from Trigger?

Trigger:

trigger accountAsyncTrigger on Account (after insert, after update) {
  for(Account a: Trigger.new){
    // Invoke the @future method for each Account
    // This is inefficient and will easily exceed the governor limit of
    // at most 10 @future invocation per Apex transaction
    someClass.processAccount(a.id);
   }    
}

Apex:

global class someClass{
  @future
  public static void processAccount(Id accountId) {
// some code here
 }
}

Best way for future method invocation,

trigger accountAsyncTrigger on Account (after insert, after update) {
    //By passing the @future method a set of Ids, it only needs to be
    //invoked once to handle all of the data.
    someClass.processAccount(Trigger.newMap.keySet());
}

Let see interesting facts about future method in salesforce.

1Why we cannot pass objects as arguments in future method?

Object data might change between the time you call the future method and the time it actually executes. and hence we pass record id.

2) If downtime occurs and future method was running what will happen?

The Future method execution will be rolled back and will restart after downtime overs.

3) If the future method was queued before a service maintenance what will happen?

It will remains in queue and when maintenance is over and resources are available it will get execute.

9 comments:

  1. When to use future methods?
    .When you are having a callout to an external system....
    Please Explain me this point sir...

    ReplyDelete
    Replies
    1. when you make a service callout since we don't know how much time it will take to execute and moreover we don't want to stop our processing so we will use future method which will run assynchronous

      Delete
  2. Future method are annoted with @future annotation which indicates that the method will run
    asynchronously(i.e in future when resources are available to it).
    >>Explain this one too..

    ReplyDelete
    Replies
    1. hi its not only related to resource ...its to speedup the process ..like multiple job run same time paralel (asynchronous)

      Delete
  3. When test.startTest() runs data is collected asynchronously and as soon as a test.stopTest runs data collected is run synchronously.
    >>>What is exactly asynchronously & synchronously in this @future??

    ReplyDelete
  4. can we use future method inside batch apex class.

    ReplyDelete
  5. Importent point = you cannot call future method from another future method and also cannot call from batch.

    ReplyDelete