• #20965
    Salesforce | Shubham Shubham #221
    Forcetalks

    Why future method is void and static in Salesforce?

    Why future method is void and static in Salesforce?

     

    #20980
    Salesforce | Aman Aman #4
    Forcetalks

    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.

     

    #21023
    Salesforce | Shubham Shubham #221
    Forcetalks

    Thanks Aman jain

     

    #34709
    Salesforce | Parul Parul #2
    Forcetalks

    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

    #34728
    Forcetalks

    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.

    #34750
    Salesforce | shariq shariq #1
    Forcetalks

    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!

Viewing 6 posts - 1 through 6 (of 6 total)

Please to reply to this topic.

CONTACT US

We're not around right now. But you can send us an email and we'll get back to you, asap.

Sending

About Us

Forcetalks is a Salesforce collaboration platform for coders & developers, geeks & nerds, consultants & business heads, admins & architects, managers & marketers and of course the business owners. A community where you can learn from, where you can contribute to. For you. For Salesforce. Read More...

Copyright 2019 Forcetalks. All Right Reserved.

Log in with your credentials

or    

Forgot your details?