function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
robert webber 8robert webber 8 

Display errors from openlink VF page on save and stay open

I have a VF page that opens from an output link:

                <apex:column headervalue="Title" width="50%">
                    <apex:outputLink value="/apex/WorkRequestVF?ID={!w.WorkItemID}">
                        {!w.WorkItemName}
                    </apex:outputLink>
                </apex:column>

Within the WorkRequestVF child page I want to save and refresh the parent page.

                <apex:commandButton value="Save" action="{!MySave}" oncomplete="closeWin();" style="width:150px"/>

MySave in the controller does error checks and then does an update on the record, then returns null.

Here is the VF javascript function:

function closeWin(){   
        if(window.parent){
            window.parent.opener.location.href = window.parent.opener.location.href;
            window.parent.close();
        }  
        else{
            window.opener.location.href = window.opener.location.href;
            window.close();  
        }  
    }

This all works fine except for the case where there are ApexPages:message error messages added. The window closes before any of the error messages can be seen.

My requirement is that I want to be able to open the linked record from the parent page. When there are no errors I want to update the record, close the child page and refresh the parent page. If there are error messages, I want to stay on the page and show the error messages. How can I do this? Any help greatly appreciated.
 
Dushyant SonwarDushyant Sonwar
Robert,

You will need to declare boolean variable that will let the vf page know if error has occured or not.

isError= true; // make this variable true just before the ApexPages:message line

<apex:commandButton value="Save" action="{!MySave}" oncomplete="closeWin();" style="width:150px" rerender="scriptPanel"/>
<apex:outputPanel id="scriptPanel">
<script>
function closeWin(){   
if({!isError} == false){
        if(window.parent){
            window.parent.opener.location.href = window.parent.opener.location.href;
            window.parent.close();
        }  
        else{
            window.opener.location.href = window.opener.location.href;
            window.close();  
        }  
    }
}
</script>
</apex:outputPanel>

This will stay on the page and will not close when error occurs.
 
robert webber 8robert webber 8
Thank you for your help. I tried this.

    <apex:outputPanel id="idScriptPanel">  
        <script>
        function closeWin(){
            if({!ErrorMessages} == false) {
                if(window.parent){
                    window.parent.opener.location.href = window.parent.opener.location.href;
                    window.parent.close();
                }   
                else{
                    window.opener.location.href = window.opener.location.href;
                    window.close();   
                }
            }
        }
        </script>
    </apex:outputPanel>
 
ErrorMessage is defined in the controller as Boolean and set to true if the error checks fail. I also changed the commandbutton as you suggested:

                <apex:commandButton value="Save" action="{!MySave}" oncomplete="closeWin();" style="width:150px" rerender="idScriptPanel"/>
                <apex:commandButton value="Delete" action="{!MyDelete}" oncomplete="closeWin();" style="width:150px"/>                
                <apex:commandButton value="Cancel" action="{!MyCancel}" oncomplete="closeWin();" style="width:150px"/>

Now the page does not close whether there is an error or not, and does not close with Delete or Cancel.
 
Dushyant SonwarDushyant Sonwar
make the ErrorMessages boolean to false in case of Mydelete and my cancel method.

<apex:commandButton value="Delete" action="{!MyDelete}" oncomplete="closeWin();" style="width:150px" rerender="idScriptPanel"/>                
                <apex:commandButton value="Cancel" action="{!MyCancel}" oncomplete="closeWin();" style="width:150px" rerender="idScriptPanel"/>

Could you please post you whole vf page / apex code ? This will be easier to identify bug.