2

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).

2 Answers 2

5

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

4
  • 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
    Commented Sep 1, 2014 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. Commented Sep 1, 2014 at 8:27
  • hmm any solution/workaround for that?
    – Sanjivani
    Commented Sep 1, 2014 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. Commented Sep 1, 2014 at 21:17
1

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.

1
  • This seems like a good option for giving richer feedback to the user with the trade-off of being more complex to implement. Commented Sep 17, 2012 at 22:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .