In this blog we will learn about @future annotation. In salesforce there is several method to run the process asynchronously. @future annotation is one of the way. It run the process in separate thread.

Key Points:

  1.  The method annoted with @future is known as future method.
  2.  Future method :
    1.  can only return void type.
    2.  must be static method.
    3.  cannot take objects and sOjects as parameter.
    4.  allow callout when callout = true is specified.(Default is callout = false).
      public class exampleClass
      {
          /**
          * Webkul Software.
          *
          * @category  Webkul
          * @author    Webkul
          * @copyright Copyright (c) 2010-2016 Webkul Software Private Limited (https://webkul.com)
          * @license   https://store.webkul.com/license.html
          */
      
          @future (callout = true)
          public static void exampleCallout()
          {
            //Write your code here.
          }
      }
  3.  Specified parameter must be primitive data types, arrays of primitive data types, or collections of primitive data types.
  4.  Method may or may not be executing in same order as it is called.
  5.  It cannot be used in visualforce controller either in getMethodName() or in setMethodName() nor in constructor.
  6.  Nested future calling is not allowed means you cannot call a future method in another future method niether in trigger.

Where To Use :

  1. When you have to make callout to external web-services after DML operation from trigger because it hold the database connection open for lifetime.
  2. Due to sort of resource some method need to run in their own thread, so that it can use that resources when they are available, there you can use future annotation.
  3. Due to limited user’s access on record sometimes DML operation on certain sObject cannot be mixed with DML operation on another sObjects. At that time we need to change the transaction of both DML operation. Here we can use future method.

GOVERNOR Limits

  1. Maximum number of methods with the future annotation allowed per Apex invocation is 50.
  2. No more than 200 method calls per Salesforce.com license per 24 hours

 

Example:

Below is an example of future method. In this example you will create an custom field name Number_Of_Contacts___c of type number in Account sObject and update this field using future method as explained below.

  1. You have to create an custom field from Setup>> Customize>>Accounts>>Fields. Create new custom field name Number_Of_Contacts__c and type should be Number.
  2. Now create an apex class AccountProcessor to update the number of Contacts in each Account.
    public class AccountProcessor {
      
        /**
        * Webkul Software.
        *
        * @category  Webkul
        * @author    Webkul
        * @copyright Copyright (c) 2010-2016 Webkul Software Private Limited (https://webkul.com)
        * @license   https://store.webkul.com/license.html
        */
        
        @future
        public static void countContacts (List<Id> accountIds)
        {
            List<Account> accountList = new List<Account>();
            for(Id accId : accountIds)
            {
                Account acc = [Select Name from Account where Id =: accId];
                acc.Number_of_Contacts__c = [Select count() from Contact where AccountId =: accId];
                accountList.add(acc);
            }
            update accountList;
        }
        
        public AccountProcessor ()
        {
            List<Account> accList = [Select Id from Account];
            List<Id> accountIds = new List<Id>();
            for(Account acc: accList)
            {
                accountIds.add(acc.Id);
            }
            AccountProcessor.countContacts(accountIds);
        }
    }
  3. Now open Execute Anonymous window (Ctrl+E) and execute the following code.
    AccountProcessor a = new AccountProcessor();

Now, go to Accounts and check the updated record. You will find total number of contact associated with that account in Number_Of_Account custom field.

Output:

 

Support

That’s all for @future Annotation, still if you have any further query , feel free to contact us, we will be happy to help you https://wedgecommerce.com/contact-us/.

 

Leave A Reply

Please verify that you are not a robot.

Tell us about Your Company

How can we help you with your business?




    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home