Salesforce Developer Future Methods Best Practices
// Salesforce - Developer - Future Methods - Best Practices: Since every future method invocation adds one request to the asynchronous queue, avoid design patterns that add large numbers of future requests over a short period of time. If your design has the potential to add 2000 or more requests at a time, requests could get delayed due to flow control. Here are some best practices you want to keep in mind: 1. Ensure that future methods execute as fast as possible. 2. If using Web service callouts, try to bundle all callouts together from the same future method, rather than using a separate future method for each callout. 3. Conduct thorough testing at scale. Test that a trigger enqueuing the @future calls is able to handle a trigger collection of 200 records. This helps determine if delays may occur given the design at current and future volumes. 4. Consider using Batch Apex instead of future methods to process large number of records asynchronously. This is more efficient than creating a future request for each record. 1. Methods with the future annotation must be static methods, and can only return a void type. 2. The specified parameters must be primitive data types, arrays of primitive data types, or collections of primitive data types; future methods can’t take objects as arguments. 3. Future methods won’t necessarily execute in the same order they are called. In addition, it’s possible that two future methods could run concurrently, which could result in record locking if the two methods were updating the same record. 4. Future methods can’t be used in Visualforce controllers in getMethodName(), setMethodName(), nor in the constructor. 5. You can’t call a future method from a future method. Nor can you invoke a trigger that calls a future method while running a future method. See the link in the Resources for preventing recursive future method calls. 6. The getContent() and getContentAsPDF() methods can’t be used in methods with the future annotation. 7. You’re limited to 50 future calls per Apex invocation, and there’s an additional limit on the number of calls in a 24-hour period.
page revision: 0, last edited: 26 Mar 2017 07:35