i am currently using a inline VF page to perform an update on an Sobject. The inline Vf page a picklist field and a button which will update the sobject with the values entered in the picklist. Now, we all know there are lotzzz of examples on how to refresh the main browser window through a inline VF page.
But i need to refresh the main window only if there was no error during the update of the sobject which happens on the click of the button. If there is any validation rule or any other exceptions which prevents the update i am showing the pagemessage on screen.
Now how can i conditionally refresh the main browser based on the success of the sobject record.
Current Apex method that is written for the button(notice i am setting the boolean as true if there is any exception)
public pagereference updateDelegatedUser(){
try{
update caserec;
}catch (exception e)
{
haserror = true;
ApexPages.addmessages(e);
}
return null;
}
VF button
<apex:commandButton oncomplete="refresh();" value="Update" rerender="myform" status="waitingStatusLoad" action="{!updateDelegatedUser}"/>
<script>
function refresh()
{
document.getElementById('{!$Component.myform.btn}').style.visibility="hidden";
//need a condition check here for the below refresh
window.parent.location.replace('/{!$CurrentPage.parameters.id}');
}
</script>
Attribution to: Anil Shivaraj
Possible Suggestion/Solution #1
in yout try use Database.SaveResult SR = database.update(myAcct,false);
and check if
if(SR.Issucess){ hastrue=false; }
This ensures the update was true and there are no error messages !!! http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_dml_update.htm
Attribution to: Rao
Possible Suggestion/Solution #2
Add a boolean return method to your controller, which returns true if any error messages have been added
public boolean getHasErrorMessages{
return ApexPages.hasMessages;
}
In your VF Page, use this to control refresh
if({!hasErrorMessages <> true})
window.parent.location.replace('/{!$CurrentPage.parameters.id}');
Attribution to: techtrekker
Possible Suggestion/Solution #3
I guess I know your issue. Try to rewrite the code like this.
<apex:commandButton oncomplete="refresh();" value="Update" rerender="myJS" status="waitingStatusLoad" action="{!updateDelegatedUser}"/>
<apex:outputPanel id="myJS">
<script>
function refresh()
{
document.getElementById('{!$Component.myform.btn}').style.visibility="hidden";
//need a condition check here for the below refresh
if({!haserror==false})window.parent.location.replace('/{!$CurrentPage.parameters.id}');
}
</script>
Notice the outputPanel in the code and make sure the rerender attribute is refering to correct component i.e. outputpanel. By doing this you are asking for a ajax page refresh and the getter method fot the hasError is called and the latest data is pulled.
Attribution to: Avidev9
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/4962