Saturday, October 6, 2018

Future method in salesforce

A future method is a way to execute a block of code asynchronously in the background. Future methods are annotated with the @future annotation, which indicates that the method will run
asynchronously (i.e., in the future when resources are available to it). Methods that are not future methods run as soon as
It executes in same thread, but future methods run in different thread. Future methods always require ID's as parameters. Objects are not allowed as there is a possibility that when future methods run, data might have changed, which can lead to processing the wrong data.

When should I use future methods?

 

  1. When you are having a callout to an external system.
  2. To avoid mixed-dml errors, a mixed-dml error occurs when you try to do dml operations on the setup object (ex., the 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 a parameter; objects are not allowed.
3) The order of execution is not guaranteed with the future method.
4) When making a callout to an external system, the future method is annotated with (callout=true).
5) A future method can’t invoke another future method.

Syntax of future methods:


global class Myclass{

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

Test class for future methods:


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

test.startTest();

myClass.futureMethodName(id);

test.stopTest();

How do I 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's limit of

    // at most 50 @future invocations per Apex transaction

    someClass.processAccount(a.id);

   }    

}

Apex:

global class someClass{

  @future

  public static void processAccount(Id accountId) {

// some code here

 }

}

 

The 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's see some interesting facts about future methods in Salesforce.

1) Why can't we pass objects as arguments in the future method?

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

2) If downtime occurs and a future method is running, what will happen?

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

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

It will remain in queue, and when maintenance is over and resources are available, it will be executed. 

12 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
  6. it is not recommended to call future method from trigger as an org can have at max 50 future methods execution at at time but think of a scenario if trigger has more than 50 records to be processed in that case we might get an exception.

    ReplyDelete
    Replies
    1. pass a set of id to future method in that case.

      Delete
  7. 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);
    }
    } how we can write test class for tigger which has future method

    ReplyDelete