How can I notify users of errors that are encountered within a future method?

Outside of a future method I'd usually resort to ApexPages.addMessage(), but this isn't possible without the browser session.

For example, I might need the user to return to a specific contact record to address an invalid field (that was only identified after a callout).


I found a Discussion Board post by Bob Buzzard that had some suggested solutions.

You can't do this as the @future request is disconnected from any browser session. There's a couple of alternatives I can think of:

  1. Post a chatter message to the user

  2. Send an email to the user

  3. Create a custom object/setting, add the message to that and write a visualforce page for the sidebar that displays the message

  • Hi @DanielBallinger, I am notifying user as per #3, but I dont get updated value of custom setting in getInstance until future call finishes. What can be wrong? – Sanjivani Sep 1 '14 at 7:00
  • @Sanjivani There is a cache that sits behind getInstance. It would appear that it doesn't get updated until the transaction that updated the custom setting is completed. – Daniel Ballinger Sep 1 '14 at 8:27
  • hmm any solution/workaround for that? – Sanjivani Sep 1 '14 at 12:29
  • @Sanjivani You could try a direct SOQL query against the custom setting (without using FOR UPDATE). That could potentially perform the dirty read of the value before the future method transaction completes. Of course, the timing of the future method will vary, so it might be difficult to always get the value when you want it. – Daniel Ballinger Sep 1 '14 at 21:17

You could have your Visualforce page poll periodically to see if the future action is complete, and then pull any error information out of a database object. This would require some Javascript that will call your controller's status check method after a reasonable amount of time.

  • This seems like a good option for giving richer feedback to the user with the trade-off of being more complex to implement. – Daniel Ballinger Sep 17 '12 at 22:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.