Hot Posts

6/recent/ticker-posts

Future method in salesforce

Future method in salesforce

What do you understand about the Future annotation (@future)?

A future operates in an asynchronous manner. You have the ability to invoke a future method to carry out lengthy operations, such as making callouts to external Web services or any operation that you wish to run independently and at its own pace. Each future method is placed in a queue and executes when system resources are accessible. This ensures that the execution of your code does not need to be delayed until a long-running operation is finished.

These methods with future annotation should be static and can only return a void type. The arguments mentioned should be primitive data types/collections of primitive data types/ arrays of primitive data types.

A method specified with @future annotation will be executed once Salesforce has available resources.

Contemplation of methods for the future:

Methods that are annotated with the future keyword must be static methods and can only return a void type. The specified parameters for these methods must be primitive data types, arrays of primitive data types, or collections of primitive data types. It is important to note that methods with the future annotation cannot accept sObjects or objects as arguments.

When invoking future methods, you can do so in the same way as any other method. However, it is important to remember that a future method cannot invoke another future method. Additionally, there is a limit of 50 method calls per Apex invocation.

Asynchronous calls, such as @future or executeBatch, that are called within a startTest and stopTest block do not count against the limits for the number of queued jobs. This means that you can make these asynchronous calls without worrying about exceeding your limits.

The maximum number of future method invocations allowed within a 24-hour period is either 250,000 or the number of user licenses in your organization multiplied by 200, whichever is greater. This limit ensures that the system can handle the workload efficiently.

To properly test methods that are defined with the future annotation, you should call the class containing the method within a startTest() and stopTest() code block. This allows the system to collect all asynchronous calls made after the startTest method. When the stopTest method is executed, all asynchronous processes are run synchronously, ensuring accurate testing.

What are the arguments against sobject in the future method?

It is not possible to pass sObjects as arguments to future methods due to the possibility of changes in the sObject between the time of method call and execution. This can result in the future method receiving outdated sObject values and potentially overwriting them. To handle existing sObjects in the database, it is recommended to pass the sObject ID (or collection of IDs) and use it to query for the most recent record.

What is the use of the future method in Salesforce?

There are numerous scenarios where future methods can be utilized in Salesforce to achieve various objectives.

  1. To circumvent the CPU time limit error, one can leverage the @Future method in Salesforce, which helps in avoiding the log running talk and exceeding the CPU time limit. For more information on how to avoid the Apex CPU Time Limit Exceeded error, refer to the documentation.
  2. Although callouts from triggers are not allowed in Salesforce, we can still invoke callouts from triggers by encapsulating them in @future methods. This approach helps in avoiding any potential issues that may arise due to callouts from triggers.
  3. The Mixed DML operation error occurs when you attempt to make changes to both Setup and non-Setup Objects in the same transaction. To avoid this error, you can use future methods to isolate DML operations on different sObject types.
  4. One of the benefits of using future methods in Salesforce is that it allows for higher governor limits, such as SOQL query limits and heap size limits. This can be particularly useful in scenarios where you need to process large amounts of data or perform complex operations.

Future method callouts using @future(callout=true)

Example of a future method that makes a callout to an external service. Notice that the annotation takes an extra parameter (callout=true) to indicate that callouts are allowed

global class FutureCalloutExample{ @future(callout=true) public static void doCalloutInFuture(String acc){ // Perform a callout to an external service } }

What is the difference between the future method and Queueable?

FUTURE APEX

  1. It is annotation based so we can use the same apex class to write the future method. Syntax: @future
  2. We cannot monitor the jobs
  3. we cannot call a future from another future or batch apex. The limit on future method for single apex invocation is 50.
  4. Future method supports only primitive datatypes

QUEUEABLE APEX

  1. It is a class which implements’ queueable interface. Syntax: public class CLASS_NAME implements Queueable{}
  2. We can monitor the jobs based on the job Id.
  3. We can chain the Queueable jobs and the stack depth in developer org is 5 and in enterprise edition you can chain 50 jobs.
  4. Queueable supports both primitive and non-primitive data types.

Future Method Best Practices

Below are some recommended guidelines for utilizing future methods in Salesforce.

1. Limit the number of future methods called for callouts. If there are more than 2,000 unprocessed requests from a single organization in the queue, any additional requests from the same organization will be delayed.

2. Optimize the execution time of future methods. Reduce the time taken for web service callouts and fine-tune the queries used in your future methods.

3. Conduct thorough testing of future methods at scale. To identify potential delays, test using an environment that generates the maximum number of future methods you expect to handle.

4. Consider using batch Apex instead of future methods to process large volumes of records.

Post a Comment

0 Comments