==================
Future Methods
==================
1. If the process contains long running statements, and if these statements are not
having
any dependency on rest of the operation, then we can run those statements
independent from the
rest of the transaction.
Example of long running operations
Ex: Webservice callouts, Bulk DML Operations
What do you mean by bulk DML?
Performing DML on huge list of records
2. Rules to define future methods:
1. All the future methods should have @future annotation
@future
public static void futureCall(parameters){
2. All the future methods should be defined as static
3. All the future methods should have void as the return type
4. Only primitive variables can be passed as parameters. Cannot accept
sObject as parameters.
5. When the future method is invoked, then they would be added in a
queue and from that queue
they will be executed.
6. To make a webservice call from apex, then parameter callout=true
must be added to future
annotation,
@future(callout = true)
7. Any asynchronous job that is running in Salesforce would be
registered with AsyncApexJob
object (Registration means a row would be inserted in this
object)
8. How to track the status of the future method
a. Write a soql query on AsyncApexJob
b. Declarative way to check the status
Setup
|----Monitor
|------Jobs
|-----Apex
Jobs
9. Future methods can be used to increase the governor limits
@future -> (limits = soql * 2)
10. We cannot call one future method from another future method.
Scenario: Update all the accounts having blank phone number to 9999999999
public class FutureExample {
public static void function1(){
statement1;
updateAccounts();
statement2;
@future()
public static void updateAccounts() {
List<Account> accList = [Select Id, Name, Phone From Account Where
Phone = null];
for(Account acc: accList){
[Link] = '9999999999';
}
update accList;
Note: All asynchronous methods are executed when [Link]() is called.
//data preparation = 90 SOQL
[Link]();
//actual testing - calling out a method in the main class - fresh limit of 100 soql
queries
[Link]();
//assertion = 10 soql