I have VF page shown in the service console.
When Apex command button is clicked it is invoking controller action method.
OnComplete on the command button executes the JS function which refreshes the Tab in the service console.
Controller action method does validation and if validation fails it creates entry to be shown in ApexPageMessage and returns null.
Ideal Behavior
When Command button is clicked – action method is called, if validation fails,
Apex Page Message should display the error on the page and OnComplete JS should not be invoked.
ISSUE
When command button is clicked – action method is called and if validation fails, the pagemessage is not shown on the Page. Instead JS function is called.
How do I make sure PageMessage are displayed when OnComplete Js is used.
NOTE
Everything works as expected if I remove the "OnComplete" JS from Commandbutton.
Markup
<apex:commandButton value="ClickMe" action="{!Save}" oncomplete="refreshConsoleTab('{!IsValid}');"/>
Code
public PageReference Save()
{
if(validationFailure)
{
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Validation failed'));
return null;
}
else
{
//DML statements ;
return null;
}
}
Best Answer
When you specify the
oncomplete
attribute, VF is going to perform a Javascript AJAX postback rather than a full page post back to Salesforce. In the full page postback scenario, the entire page is rendered again, including your error messages. In the ajax postback, you've got to be explicit about which parts of the page you want rerendered when that action completes.If you add a
rerender="messages"
or whatever the id is of your<apex:pageMessages />
tag to thecommandButton
, when the AJAX post is finished Salesforce will rerender the messages element on the page and show the (now updated) validation error message.