Future Method Asynchronou...

What is Asynchronous Apex?

Salesforce Asynchronous Apex is used to run processes in a separate thread, at a later time when the system resources become available. An asynchronous process is a process or function that executes in the background without the user having to wait for the task to finish.Salesforce provides different options to run asynchronous jobs – Future methods, Batch Apex, Apex Scheduler etc.

Future Methods:

Future Method is one of the options to run asynchronous jobs.We can use future methods for any operation we’d like to run asynchronously in its own thread in the background when system resources become available.Future methods provide higher governor limits

When to use the Future method?

Future methods are typically used for:

  1. Callout to external web services.(Calling web services from triggers.)
  2. Operations which you want to run in their own thread.
  3. Isolating DML operations on different subject types to prevent the mixed DML error.

Limitations of the Future method:

  1. 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.
  2. Future methods won’t necessarily execute in the same order they are called.
  • Why can we not pass Objects as arguments in future Methods?

The reason why Salesforce objects cannot be passed as arguments to future methods is that the object can change between the time you call the method and the time it actually executes.Because future methods are executed when system resources become available

Future Method syntax:

Future methods must be static and can only return void type.Specified parameters can only be primitive data types, arrays of primitive data types or collection of primitive data types Future methods don’t take standard or custom objects as arguments rather we can pass the list of Ids to process asynchronously. Future methods are always annotated with the @future annotation

For example:

global class SomeClass {

 @future

public static void some future method(List<Id> recordIds) {   List<Account> accounts = [Select Id, Name from Account Where Id IN :recordIds]; }

How to call a web service using the future method?

To make a callout to external web service, You create an apex class with the future method marked with @future(callout=true)

For example:

public class SMSUtils {

    // Call async from triggers, etc, where callouts are not permitted.

    @future(callout=true)

    public static void sendSMSAsync(String fromNbr, String toNbr, String m) {

        String results = sendSMS(fromNbr, toNbr, m);

        System.debug(results);

    }

    // Call from controllers, etc, for immediate processing

    public static String sendSMS(String fromNbr, String toNbr, String m) {

        // Calling 'send' will result in a callout

        String results = SmsMessage.send(fromNbr, toNbr, m);

        insert new SMS_Log__c(to__c=toNbr, from__c=fromNbr, msg__c=results);

        return results;
    }}
  • Calling Future methods from triggers:

We cannot call external web services synchronously from triggers because calling a web service synchronously from triggers will open the connection for the lifetime of the callout However, we can call the web service by using future methods i.e. asynchronously.

Write a callout in a class using the future method.

public class SendAccountUsingRESTAPI {
           @future(callout=true)
          public static void callcreateAcc (String accName, String accId)
    {
    System.debug('Created Account Name:'+accName);
   System.debug('Created Account Id:'+accId);
    }
 }

Then call that class from the trigger.

trigger SendAccount on Account(after insert)
  { 
   for(Account a : Trigger.new)
   {
  SendAccountUsingRESTAPI.callcreateAcc(a.Name, a.Id);
    }
  }
  • Can we call a future method from batch Apex?

No, We cannot call future methods directly from batch apex but we can call a  web service from batch class and that web service can call the @future method. Also, we can call the future method from finish method in the batch class.

 

[ssba]

Post a Comment

Your email address will not be published. Required fields are marked *