You need to sign in to do that
Don't have an account?
yaro2
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!
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 .
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!
I'm pretty sure you can't do this, since future runs async.
Regards
I think so too.
Ok, then I'll send e-mail notification...
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