In the last How To Salesforce, we covered what to do when SOQL ignores null checks on required lookup fields.
In today’s post, we are going to cover a few key points when working with Future Methods. But before we do, let’s talk about what a Future Apex is. Future Apex is used to run processes in a separate thread, at a later time when system resources become available.
A Future Method runs in the background asynchronously. You can call a Future Method for executing long-running operations, such as a callout to external Web Services or any operation you’d like to run in its own thread and/or on its own time.
You can also use future methods to isolate DML operations on different sObject types to prevent the mixed DML error.
Future Methods are typically used for:
- Callouts to external Web services.
- Operations you want to run in their own thread
- Isolating DML operations on different sObject types to prevent the mixed DML error.
To define a future method, simply annotate it with the future annotation, as follows:
global class FutureClass
{
@future
public static void myFutureMethod()
{
// Perform some operations
}
}
Important points to be remembered:
- Methods with the future annotation must be static methods, and can only return a void type.
- The specified parameters must be primitive data types, arrays of primitive data types, or collections of primitive data types.
- Methods with future annotation cannot take sObjects or objects as arguments.
- We can’t call a future method from the future method.
Leave a Reply