Activity Forums Salesforce® Discussions Why future method is void and static in Salesforce?

  • Aman

    Member
    July 17, 2017 at 6:42 am

    Hello Shubham,

    Future methods will run in the future. You don't want your synchronous code waiting an unknown period of time for an asynchronous bit of code to finish working.

    The whole point is you want to clearly separate what happens now from what happens in the future.

    By only returning void, you can't have code that waits for a result.

    Future method by definition is static so that variables with this method is associated to the class and not the instance and you can access them without instantiating the class.

     

  • Shubham

    Member
    July 17, 2017 at 12:08 pm

    Thanks Aman jain

     

  • Parul

    Member
    September 17, 2018 at 6:47 am

    The future method is void and static because:

    use of future methods to isolate DML operations on different sObject types to prevent the mixed DML error. Each future method is queued and executes when system resources become available. That way, the execution of your code doesn’t have to wait for the completion of a long-running operation. A benefit of using future methods is that some governor limits are higher, such as SOQL query limits and heap size limits

    NOTE :-
    1) Methods with the future annotation must be static methods
    2) can only return a void type
    3) The specified parameters must be primitive data types, arrays of primitive data types, or collections of primitive data types
    4) Methods with the future annotation cannot take sObjects or objects as arguments.
    5) You can invoke future methods the same way you invoke any other method. However, a future method can’t invoke another future method
    6) No more than 50 method calls per Apex invocation
    7) Asynchronous calls, such as @future or executeBatch, called in a startTest, stopTest block, do not count against your limits for the number of queued jobs
    8) The maximum number of future method invocations per a 24-hour period is 250,000 or the number of user licenses in your organization multiplied by 200, whichever is greater
    9) To test methods defined with the future annotation, call the class containing the method in a startTest(), stopTest() code block. All asynchronous calls made after the startTest method are collected by the system. When stopTest is executed, all asynchronous processes are run synchronously

     

     

    Thanks

  • shradha jain

    Member
    September 17, 2018 at 10:27 am

    Hi,

    Future methods can be accessed from any class and not depend on any class instance hence it is declared as Static.Also Static keyword make it as utility method rather than normal class method. Future method runs on it own thread, so it cannot return any values to previous instance.

  • shariq

    Member
    September 17, 2018 at 11:41 am

    Hi,

    The reason why future methods are static and having a void return type is :
    Void return type is to ensure that you synchronous code doesn't wait for a value that will be return by the future method. Also, you are not sure when will the future method execute.
    Future method by definition is static so that variables with this method is associated to the class and not the instance and you can access them without instantiating the class.

    Hope this helps!

Log In to reply.

Popular Salesforce Blogs