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
yaro2yaro2 

How to show message from @future method?

Hi All,

 

I have Visualforce controller class. There is called @future method from Save method. And I need to show error message from @future method on the same page where "Save" button were pressed, similar to ApexPages.addMessage().

Is it possible?

 

Thank you!

 

 

 

Shilpa_SFShilpa_SF

Hi,

 

    You can show the Page messages for the future method.

 

     In the controller class catch the exception (custom or standard depends on your requirement)and add it to the page      

     messages in the catch block .

yaro2yaro2

Could you please show an example, because ApexPages.addMessages() doesn't work in @future methods:

"ApexPages.addMessage can only be called from a Visualforce page".

 

Thank you!

ascuccimarraascuccimarra

I'm pretty sure you can't do this, since future runs async.

 

Regards

yaro2yaro2

I think so too.

Ok, then I'll send e-mail notification...

Biswojeet Ray 11Biswojeet Ray 11

Hi Yaro,
 
You can not do this. Because @future is asynchronous. 
You can do "database.DML" operation. So that you can get the particular error with record Id (If you are putting bulk record operation.)
Then you can frame an email with error and particular record data and send.
Code - 
        if(!taskCompleted.isEmpty()) { 
            Database.SaveResult[] updateResults = database.update(taskCompleted, false);
            for(Integer i=0;i<updateResults.size();i++){
                if (updateResults.get(i).isSuccess()){
                    updateResults.get(i).getId();
                }else if (!updateResults.get(i).isSuccess()){
                    Database.Error error = updateResults.get(i).getErrors().get(0);
                    String failedDML = error.getMessage() + error.getFields(); //To get record error and field name
                    taskCompleted.get(i); //To get Record Id
                    newInvoicesMap.get(taskCompleted.get(i).WhatId).adderror(failedDML);
                }
             }              
        }

 

Kindly let me know if it helps you and please mark as Best Answer.

Thanks and Regards,
Biswojeet