Friday 11 July 2014

Refresh Parent Window after Closing Child Window Salesforce

                 Hi, Recently had a requirement to perform multiple actions in a single page. First taught of using Modal POPUPS as they are simple and easy to work. But due to salesforce date field not showing in popup issue had to modify it from popup's to multiple windows/pages.

                Later came to know I have opened window successfully but how to refresh parent window after closing this child window. Bang.. How ??? 

              Finally with a bit of  Java script  code i  found solution in this requirement.

Parent Page vf code :-

 <apex:pageblockTable var="all_stud" value="{!allStudents}" >  
     <apex:column headerValue="Student Name">  
         <apex:outputLink value="#" onclick="openLookupPopup('{!all_stud.name}', '{!all_stud.id}')" >{!all_stud.name}</apex:outputLink>  
    <!--here onclick i am invoking a javascript function,  
     in this function i am passing two parameters name,and id. You can customize as you need-->  
     </apex:column>  
  </apex:pageblockTable>   

Here is the java script of parent page :-

 <script>  
       var newWin=null;  
       function openLookupPopup(name, id){  //function for opening page
 // specify the URL of page you want to open  
         var url="/apex/quickregister?namefield=" + name + "&id=" + id;  
         newWin=window.open(url, 'Popup','height=700,width=1200,left=100,top=100,resizable=no,scrollbars=yes,toolbar=no,status=no');  
         if (window.focus){  
           newWin.focus();  
         }  
         return false;  
       }     
       </script>  
        <script>  
       function reload() {  //this is function used to refresh parent page
       reloadparent();  
       }     
     </script>  
Now let's move to child page :-

perform the action you want to and add this script to close child window
 <script>  
  function closeaftersave(){  
   var winMain=window.opener;  
    if (null==winMain)  
    {  
      winMain=window.parent.opener;  
    }  
   winMain.reload();//this is the function of parent page which gets invoked from child page  
   window.top.close();// this line just closes the window opened  
   }  
 </script>  
 <apex:form>  
     <apex:page:block>  
      <apex:commandButton value=" Button " action="{!myaction}" oncomplete="closeaftersave()"  />  
     </apex:pageblock>  
 </apex:form>  

What is the problem Now.. Child page is closing successfully but why is parent page not getting refreshed ??

Paste this line of code in parent page outside of pageblock table. It will through an error if it's inside the table
  <apex:actionFunction name="reloadparent" action="{!reload}" />  
 <!--specify rerender if want to refresh only a particular area of page-->  
<!--Note: the name of actionFunction should be same of function within reload function-->

Controller Code:- of parent //i haven't specified any logic here you can work on your own, it just refreshes my page
 public void reload(){  
 }  


Hope This helps.. Happy Coding..!

No comments:

Post a Comment