What is Future Method In Salesforce
- Abhilash Banpurkar
- Feb 9, 2023
- 3 min read
In This Blog We Will Discussed Complete Information Salesforce Future Method Its Advantages Limitation Syntax , the "future" method is a way to run time-consuming processing asynchronously in the background, without blocking the user interface or affecting performance.
The future Method Run Processes in a Seperate Thread When System
Resources are Available.
The future method is annotated with the "@future" keyword and must be defined as a static method. The method runs in its own thread, separate from the calling context, and can return its result using callbacks or other asynchronous programming techniques.
Advantages of Future Method
Advantages of using the "future" method in Salesforce include:
Improved Performance: The future method runs in its own thread, allowing the user interface to remain responsive while the background processing is taking place.
Asynchronous Processing: The future method runs asynchronously, meaning that the processing is not completed in real-time. This is ideal for time-consuming processing, such as data updates, that can be done in the background without affecting the user experience.
Increased Governor Limits: Future methods have higher governor limits compared to other Apex code, meaning that you can process more data in a single call.
Better Resource Management: By running the processing in a separate thread, the future method can take advantage of the system's resources more effectively, improving overall performance.
Improved Error Handling: The future method can handle errors separately from the main thread, making it easier to manage exceptions and improve the overall stability of the system.
Overall, the use of the future method in Salesforce can lead to improved performance, increased processing capabilities, and more efficient resource utilization.
Why Future Method is Static in Salesforce
In Salesforce, the "future" method is defined as a static method because it runs in its own thread, separate from the main execution context. A static method can be invoked without having an instance of the class, making it the perfect candidate for running as a background process.
When a future method is executed, Salesforce creates a new instance of the class, allowing it to run in its own thread, separate from the main execution context. The static method is executed within this new instance, without having access to the instance-specific state of the original class.
By defining the future method as a static method, Salesforce can ensure that the method can be executed without having access to the instance-specific state, making it ideal for running as a background process.
This design choice also provides a clean separation between the main execution context and the background processing, allowing for better resource utilization and improved performance.
Syntax of Future Method
Syntax: - Methods are annotated with @future annotation.
Future methods should be static and can only return void type.
It supports parameters and that should be primitive data types, arrays of primitive data types, or collection.
sObjects or List of sObjects cannot be used as parameters to future methods.
Syntax:
public class ExampleFuture{
@future
public static void futureMethod(Id (PrimDatatype)/List (NonPrimDatatypes)){
}
}
Example:
public class ExampleFuture {
@future
public static void insertUserWithRole(String uname, String al, String em, String lname) {
Profile p = [SELECT Id FROM Profile WHERE Name='Standard Platform User'];
UserRole r = [SELECT Id FROM UserRole WHERE Name='COO'];
// Create new user with a non-null user role ID User u = new User(alias = al, email=em, emailencodingkey='UTF-8', lastname=lname, languagelocalekey='en_US',
localesidkey='en_US', profileid = p.Id, userroleid = r.Id, timezonesidkey='America/Los_Angeles', username=uname);
insert u;
}
}
public class MixedDMLFuture { public static void useFutureMethod() {
Account acc = new Account(Name='Acme654');
insert acc;
// This next operation (insert a user with a role)
// can't be mixed with the previous insert unless
// it is within a future method.
// Call future method to insert a user with a role.
ExampleFuture.insertUserWithRole('aks', 'wisdSfdc', 'wisdom.sfdc@gmail.com','Wisdom');
}
}
Best Practice of Future :
1. Future methods should not be used for processing large amounts of data. In that case, Batch Apex should be used.
2. Future methods should not call another Future methods.
3. Future methods do not necessarily execute in the same order as they are called.
4. Future methods cannot be used inside Visualforce controllers getter, setter, constructor methods used only in APEX Class.
When to Use Future Method
callout to External Web Services.
Process that Needs to Executed in a Separate or their own Thread.
Isolating DML Operation on different sObject types to Prevent the mixed DML error.
Questions on the Future Method
1.Create a future Method to Count the number of contacts associated with the account.List of Account ids will be Passed to the future method.Ensure proper code coverage as well.


Commenti