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
robertcw777robertcw777 

Close VisualForce Page and Refresh Detail Record Page

I want to open a visualforce page from a custom button on a custom object detail page. The user selects a value in a tree dispayed in a VF page, and then clicks on a save button. I would like to update a record on the custom object, close the VF page, and then refresh the detail page so the record updates (updating the field itself would be better).

 

I've tried all kinds of solutions for similar situations on the web, but can't get any to work. Does someone know how to do this?

 

Thanks! 

 

Here is VF:

 

<apex:page id="CompetencyRolesPage" StandardController="Competency__c" Extensions="TestPageController">

    <apex:form >

       <apex:outputPanel rendered="{!validRoleID}">

           <c:TreeView selectable="true" roleOrUserId="{!RoleID}" value="{!selectedValues}" />

           <apex:commandButton value="Save" action="{!saveRole}" />

        </apex:outputPanel>

    </apex:form>

</apex:page>

 

Here arerelevant pieces from controller:

 

public String selectedValues {get; set;}

public String userRoleID {get;set;}

   

public Boolean getValidRoleID() {

      If(null==UserInfo.getUserRoleID())

         return false;

      else

        return true;

    }

           

    public String getRoleID() {

       userRoleId=UserInfo.getUserRoleID();

       return userRoleID;

    }

   

    public PageReference saveRole() {

       //will update customer field in detail record here.

        return null;

    }

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

The close script needs to be its own page, completely separate from the page that is the popup.

All Answers

sfdcfoxsfdcfox

Here's how I solved this in my project where we have a similar mechanism:

 

<!-- closerefreshparent.page -->
<apex:page>
<script>
window.opener.top.location.reload();
window.close();
</script>
</apex:page>

From there, you then only need to modify your TestPageController.cls:

 

public PageReference saveRole() {
  // Save data here
  // on error
  return null;
  // on success
  return page.closerefreshparent;
}

By the way, you can simplify getValidRoleId:

 

public Boolean getValidRoleId() {
  return null!=UserInfo.getUserRoleId();
}

This reduces the number of lines you have to cover with test methods, reducing both overall code size and test code size.

robertcw777robertcw777

Thanks for your help. I tried as you suggested, but nothing happens when I click on the Save button. It looks like the VF page that gets refreshed and then stays open. I want the VF page to close, and the custom object detail page with the button to open the VF page refreshed. Here is the code with your suggestion.

 

In general, I'm trying to open a VF selection page to update a specific record wihin a custom object.

 

<apex:page id="CompetencyRolesPage" StandardController="Competency__c" Extensions="TestPageController">
    
    <script language="JavaScript" type="text/javascript">
        window.opener.top.location.reload();
        window.close();
   </script>
   
    <apex:form >
       <apex:outputPanel rendered="{!validRoleID}">
           <c:TreeView selectable="true" roleOrUserId="{!RoleID}" value="{!selectedValues}" />
           <apex:commandButton value="Save" action="{!saveRole}"/>
        </apex:outputPanel>
    </apex:form>
</apex:page>

 

Here is the controller:

 

 public with sharing class TestPageController {

    public String selectedValues {get; set;}
    public String userRoleID {get;set;}
  

    public PageReference saveRole() {
       
        return page.CompetencyRolesPage;
    }

 
    public Boolean getValidRoleID() {
      If(null==UserInfo.getUserRoleID())
         return false;
      else
        return true;
    }
          
   
    public String getRoleID() {
       userRoleId=UserInfo.getUserRoleID();
       return userRoleID;
    }
   
}

 

 

sfdcfoxsfdcfox

The close script needs to be its own page, completely separate from the page that is the popup.

This was selected as the best answer
robertcw777robertcw777
Thanks you for the great solution!
robertcw777robertcw777

Thank you for the great solution!