0

I have the following code:

@future(callout=true)
public void methodABC(){
CalloutClass.calloutmethod();
}

The check for error is done in the calloutmethod.If callout has failed then apex page message is added.However this doesn't work with future calls it seems as I am getting the error: ApexPages.addMessage can only be called from a visualforce page.

  • 3
    The short answer is that it's not possible. If you want the callout to happen async, you could use a class that implements the queueable interface and use the job id to poll for completion. – martin Feb 27 '17 at 1:43
  • 4
    Just call it synchronously. – Adrian Larson Feb 27 '17 at 2:00
5

Here is how I have addressed this with standard detail pages/standard controllers/triggers

  1. Use a custom picklist field on the object Callout_Status__c with values {Pending, Completed, Error
  2. Use another custom field on the object Callout_Error__c (type Text)
  3. Both fields are optionally not shown on the page layout
  4. Use a custom image formula field that displays, when Callout_Status__c = Pending a friendly message in green "Callout in progress, wait a few seconds and refresh the page" Use similar messages for Completed and Error (green/red, resp.) The error use case can include the error message.
  5. Before the future call is made, update the object field Callout Status__c to Pending. Thus, when the page is refreshed, after the Save, the image formula displays
  6. The future class, upon completion of the callout updates the object to either Callout_status__c = Completed or Callout_Status__c = Error. In the latter case, the field Callout_Error__c is set to the callout's error

Although future classes aren't guaranteed to run right away, they tend to run pretty quickly after being invoked so a few seconds wait for the user

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.