Four Gotchas When Using Future Calls in Apex in Salesforce

First, what is a future call?  Future is an annotation that can be added to a method to make that method run asynchronously.  It could look like this…

@future
static void doFutureWork(){
//Do the work
}

A great use-case for using a future call is if you do not want to wait for a web service call to complete.  Control of the screen can return quicker to the end-user and the web service call can be completed when resources are available.  With the web service callout you would have to add the “(callout=true)” text after the @future annotation.

The future call basically acts like a scheduled job that has been kicked off.  The method is placed in a queue and is kicked off as resources are available.

But this flexibility brings with it some gotchas…

1) A future call cannot make another future call and a future call cannot be made from a batch job.

With this limitation you have to be very careful in your code that a future call is not made from within a future call.  This can be very easy if the code is short or you are doing a simple web service call as described above.  But if you have a complex set of triggers or workflow rules in an organization it may not be that simple.

2) Future calls cannot be guaranteed to run in a certain order.

Just because one future call was made before another future call does not mean that the first one will complete first.  There are multiple queues that are running these future calls so the order cannot be guaranteed.  So if you need to guarantee that actions complete in a certain order then a batch or scheduled job may be the better solution.

3) Future calls can run concurrently.

It is possible that two future methods that were executed one after the other could basically run concurrently.  This means you may have to deal with record locking if these two methods could be updating the same record.  This may force you to use the ‘For Update’ method on your SOQL queries, which can be difficult to troubleshoot.

4) Future calls have limits

Salesforce has limits on how many future calls can be made so they cannot be used just anywhere.  There is a limit of 10 future calls per Apex invocation.  There is also the limit of future calls in a 24 hour period, which is 250,000 or the number of user licenses in your organization multiplied by 200, whichever is greater.  Here is a link to those limits.

Future calls have their place to solve very specific use cases.  But as Salesforce developers we have to be very careful in how we use them or we will end up with code that is difficult to debug and hard to manage.

How have you used future calls in your projects?

This entry was posted in Technology and tagged , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>