I have a VF page and extensions for it where I perform update operation. If it is successful or not I want to display page message – on VF I have
<apex:pageMessages />
and in Apex controller I have
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Update successful'));
However, when I click button which invoke my update operation I need to perform as well page refreshing. So I have
oncomplete="location.reload();"
on my button. This causes that I cannot see my page messages since the page is refreshed immediately. Any help would be appreciated.
EDIT: some more code
public PageReference update() {
// my logic
try {
update myMap.values();
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Update successful'));
return null;
} catch(DmlException ex) {
ApexPages.Message errorMessage = new ApexPages.Message(ApexPages.Severity.ERROR, ex.getDmlMessage(0));
ApexPages.addMessage(errorMessage);
return null;
}
}
<apex:page standardController="MyObject__c" extensions="MyController" standardStylesheets="true" sidebar="false" docType="html-5.0">
<html xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<apex:stylesheet value="{!URLFOR($Resource.SLDS214, '/assets/styles/salesforce-lightning-design-system.min.css')}" />
<div class="slds">
<apex:pageMessages />
//...
<button onclick="callUpdate(); return false;" class="slds-button slds-button--brand"> Save </button>
<apex:actionFunction name="UpdateAction" action="{!update}" reRender="panel" oncomplete="setTimeout(location.reload(),5000)" />
</div>
<script>
function callUpdate() {
UpdateAction();
}
</script>
Best Answer
instead of reloading immediately, you could set a timeout:
Feel free to adjust the 1000 value to whatever you like (1000 is 1 second).
You could also have this be an actionFunction that simply reRenders, which would clear the message:
As for why your messages are not appearing:
It seems that you're reRendering a part of the page that does not include the apex:pageMessages. They're not rendered because the element wasn't targeted for rerendering.