- July 14, 2017 at 11:46 am #20965Shubham #221
Why future method is void and static in Salesforce?
Why future method is void and static in Salesforce?July 17, 2017 at 6:42 am #20980Aman #4
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.July 17, 2017 at 12:08 pm #21023September 17, 2018 at 6:47 am #34709Parul #2
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
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
ThanksSeptember 17, 2018 at 10:27 am #34728shradha jain #14
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.September 17, 2018 at 11:41 am #34750shariq #1
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!
Please Register to reply to this topic. /