1. If a transaction has any long running operations/statements, and if this operations is not having any dependency on rest of the operation then we can run the operation independent from the rest of the operation by using future method.
Ex: Long running operations like: Webservices calls, Bulk DML operations.
Best practices of future methods:
----------------------------------------
All the future methods should have @future annotation
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
--> When we invoke the future method they will be added queue and from the queue they will be executed.
--> If you want to invoke webservices from the future method then define
@future(callout-true)
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
10) Any Asynchronous job that is running in salesforce will be registered with AsyncApexJob object.
11)How to track the status of the future method
a. Write a soql query on AsyncApexJob
b. Declarative way to check the status
Setup
|--- Moniter
|--- Jobs
|--- Apex Jobs
12) Future methods can be used to increase the governing limits
@future(limits = dml*2)
13) Future method will not return any JOBID in the apex code.
14) Future method will handle Mixed DML operations and Bulk DML operations
NOTE:- The reason why sObjects can’t be passed as arguments to future methods is because the sObject might change between the time you call the method and the time it executes. In this case, the future method will get the old sObject values and might overwrite them. To work with sObjects that already exist in the database, pass the sObject ID instead (or collection of IDs) and use the ID to perform a query for the most up-to-date record. The following example shows how to do so with a list of IDs
Usecases: When we have a requirement to pass sobject as parameter to future method how will you do?
a. Pass recordid instead of record
b. Serialize the sobject as string using JSON/XML and pass string aas parameter.
Ex: Long running operations like: Webservices calls, Bulk DML operations.
Best practices of future methods:
----------------------------------------
All the future methods should have @future annotation
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
--> When we invoke the future method they will be added queue and from the queue they will be executed.
--> If you want to invoke webservices from the future method then define
@future(callout-true)
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
10) Any Asynchronous job that is running in salesforce will be registered with AsyncApexJob object.
11)How to track the status of the future method
a. Write a soql query on AsyncApexJob
b. Declarative way to check the status
Setup
|--- Moniter
|--- Jobs
|--- Apex Jobs
12) Future methods can be used to increase the governing limits
@future(limits = dml*2)
13) Future method will not return any JOBID in the apex code.
14) Future method will handle Mixed DML operations and Bulk DML operations
NOTE:- The reason why sObjects can’t be passed as arguments to future methods is because the sObject might change between the time you call the method and the time it executes. In this case, the future method will get the old sObject values and might overwrite them. To work with sObjects that already exist in the database, pass the sObject ID instead (or collection of IDs) and use the ID to perform a query for the most up-to-date record. The following example shows how to do so with a list of IDs
Usecases: When we have a requirement to pass sobject as parameter to future method how will you do?
a. Pass recordid instead of record
b. Serialize the sobject as string using JSON/XML and pass string aas parameter.
Nice
ReplyDelete