2

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>
1

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.

1
  • fantastic avi...u nailed the problem:). Yes, now i am able to get the latest value for the boolean on rerendering with a outputpanel. Thanks a lot...problem solved. – Anil Shivaraj Dec 1 '12 at 16:49
1

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}');
5
  • 1
    To be safe, you might want the parameterized version of hasMessages and pass in ApexPages.Severity.ERROR to make sure that you are only testing for error messages. – Peter Knolle Nov 30 '12 at 17:57
  • Thanks techtrekker for your reply, i tried the hasMessages as u suggested too but for some reason the boolean value not getting updated to true even when the page messages. Hasmessages works fine if i use it in an onclick,ondblclick etc but when i use in oncomplete the latest boolean value is not getting updated but instead its still showing false. – Anil Shivaraj Nov 30 '12 at 18:22
  • Below alert is also showing the boolean as false even with page messages: function refresh() { document.getElementById('{!$Component.myform.btn}').style.visibility="hidden"; alert('The Value of Boolean is '+ {!hasErrorMessages}); window.parent.location.replace('/{!$CurrentPage.parameters.id}'); document.getElementById("content").innerHTML = "Refreshing window, please wait..."; } – Anil Shivaraj Nov 30 '12 at 18:24
  • @PeterKnolle, thanks for your reply but i am afraid i didnt get the parameterized part of hasmessages. What do u mean by passing the entire APexPages.severity.error? do i pass to the VF page from the controller? a little more light would be helpful, thanks? – Anil Shivaraj Nov 30 '12 at 18:25
  • @Anil - Change return ApexPages.hasMessages; to return ApexPages.hasMessages(ApexPages.Severity.ERROR); – Peter Knolle Nov 30 '12 at 18:52
0

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

1
  • hi rao,the issue i am facing is i am able to flag the boolean as success in the controller but the boolean value is not getting updated when checking with javascript using oncomplete method. In simple, i have a button having a action and a oncomplete JS.If i update the boolean to true/false and check the same boolean value in the JS on the oncomplete part of the button it still shows the old boolean value but now the new one. – Anil Shivaraj Nov 30 '12 at 18:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.