Future Methods in Salesforce: An Overview

Future methods are is a set of code that runs in the background. It is basically an asynchronous process.

Don’t forget to check out: Asynchronous Apex

Synchronous:

synchronous means one job at a time

Asynchronous:

  • Asynchronous is something that runs in the background without having you wait for that task to finish.
  • If a Transaction has any long-running operations/statements, and if this operation is not having any dependency on the rest of the operations. Then we can run the operation independently from the rest of the operation by using future methods.

Example: Long-running operations can be a web service call, bulk DML Operations.

Why we use Future Method:

  • Handles the Mixed DML operation provide the ability to make long-running callout from Trigger. Run in its own thread

STRUCTURE OF THE FUTURE METHOD

@future
Public static void future method(){
    //Your Code Here
}

Rules to define a future method:

  1. All the future methods should have “@future” annotation.
  2. All future methods should be defined as static.
  3. All the future method should have void as a return type
  4. Only primitive variables can be passed as parameters.
  5. When we call the future method they will be added in Queue and from Queue they will be executed.
  6. If you want to invoke web services from the future method then define
    @future(callout=true)
  7. Any asynchronous job that is running in Salesforce will be registered with AsyncApexJob Object.
  8. How to track the status of future methods?
    • Write a SOQL query on Async Apex Job.
    • Declarative way to check the status.
      Setup -> Monitor-> Jobs->Apex Jobs
  9. Future methods can be used to increase the Governing Limits

    @future(limits=dml*2)

Drawbacks:

  1. Future Method will not return JobId in the apex code.
  2. The future method can not be called from another future method.
  3. The future method will not support Subjects as parameters.

Limits:

  1. Within the transaction, we can call 50 future methods.

Mixed DML Exception:

If you make a DML operation on setup objects and non-setup objects, in a single transaction then

it throws Error as “mixed DML Exception”

Setup Objects:

  1. User
  2. Group
  3. Group member
  4. Permission set
  5. Object Permissions
  6. User Role
  7. Permission Set Assignment
  8. Queue SObject
  9. Object Territory2 Assignment Rule
  10. Object Territory2 Assignment Rule Item.
  11. Rule Territory2 Association
  12. Setup Entity Access
  13. Territory2
  14. Territory2 Model
  15. User Territory2 Association
  16. User Territory
  17. Territory

Non-Setup Objects: Rest of the Objects are Non-setup Objects, Example,

Account,contact,customer__c etc.

Calling Future method and an Apex method combinedly from a single method.

public class FutureMethod {
    public void acc() {
        System.debug(“Getting ready”)
    }
}
@future
public static void acc1() {
    System.debug(“Car is being repaired”);
}
public void acc2() {
    System.debug(“Watching movie”)
}
public void FutureMethod(){
    acc();
    acc1();
    acc2();
}

In the above example, all the methods will execute one by one and the future method will execute in its own thread.

Put any one of the setup/Non-setup in the future method and call the future method from the normal method.

public class mde {
/*------------------Setup operation----------------------*/
    public void create(){
        profile p=[select id from profile where name='Field Executive'];
        userrole u1=[select id from userrole where name='HR_manager'];
        user u=new user();
        u.alias='kranti';
        u.Email='[email protected]';
        u.emailencodingkey='UTF-8';
        u.LanguageLocaleKey='en_US';
        u.Localesidkey='en_US';
        u.ProfileId=p.id;
        u.userroleid=u1.id;
        u.timezonesidkey='America/Los_Angeles';
        u.username='[email protected]';
        u.lastname='reddy';
        insert u;
        futurecall();
    }
/*------------------Non-Setup operation----------------------*/
    @future
    public static void futurecall(){
        account a=new account();
        a.name='silksmita';
        a.Email__c='[email protected]';
        insert a;
    }
}

In the above example, we put the Non- set up object in the future method to avoid mixed DML exception.

Popular Salesforce Blogs