function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
neeedhelpneeedhelp 

@future catch exception should be displayed in Vf Page

My code is

 

public pagereference save(){
   //some code 
     for(integer i=0;i<10;i++){
         futureprocess(strl,i);
     }
     return null;
}

@future(callout=true)
public static void futureProcess(integer i,list<string> strlist){
    try{
       //some code
     }catch(exception e){
        if(apexpages.currentpage()!=null){ //this condition never satisfies
            ApexPages.Message errMsg = new ApexPages.Message(ApexPages.severity.ERROR, e.getMessage());  
                ApexPages.addMessage(errMsg);              
         }
     }
}

 In catch block the condition never sets to true and hence unable to display the Error Message.Any Ideas on this

bob_buzzardbob_buzzard

@future executes asynchronously, so isn't tied to any Visualforce page that may have queued it for execution.  

 

The way I've handled this in the past is to have the @future method write its result into a custom object or custom setting, and have the page poll until a value appears which it then displays.

neeedhelpneeedhelp

Thanks for your Reply Bob

Here their is one more problem.If I remove this

 if(apexpages.currentpage()!=null){ 

}

whenever enter into the catch block Ill be getting an error saying that system.FinalException:ApexPages.addMessage can only be called from a Visualforce page

 

Can you post an example on this so that it can be more helpful.

Thanks in advance

bob_buzzardbob_buzzard

I can't post an example I'm afraid - this was for a customer and they own the code.

neeedhelpneeedhelp
I'm not asking for the entire code Bob .I just wanna a snippet of how to use the code say something like if I enter into catch block how should I catch the exception(a sample of code here) and how to reflect that code in Visualforce Page
bob_buzzardbob_buzzard

I understand that, but I don't have access to the code any more as it belongs to the customer.  I'd have to code up the example from scratch, and I don't have time to do that I'm afraid.