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
Tatiana Cooke 9Tatiana Cooke 9 

Code for refreshing a visual force page to the parent object upon hitting save.

Code for refreshing a visual force page to the parent object upon hitting save. I created a visualforce page that attaches documents in the notes and attachement section, however it doesn't ALWAYS work. 

Sometimes when I hit save it does refresh the parent record and show me the attached document in the notes and attachement section and sometimes I have to manually refresh the page to see the recod. 

Can someone help me with the below code?

Visualforce Page
<apex:page standardController="Tenant_Coordination__c" extensions="attachmentsample">
    <apex:form >
    <apex:inputfile value="{!myfile.body}" filename="{!myfile.Name}" />
       <apex:commandbutton value="Save" action="{!Save}" onclick="window.top.location='/{!Tenant_Coordination__c.id}'; return true"/>
         <apex:actionFunction name="reloadparent" action="{!reload}" />  
    </apex:form>
</apex:page>

class
 
public class attachmentsample {

    public attachmentsample(ApexPages.StandardController controller) {

    }
    Public Attachment myfile;
    Public Attachment getmyfile()
    {
        myfile = new Attachment();
        return myfile;
    }
   
    Public Pagereference Save()
    {
        String accid = System.currentPagereference().getParameters().get('id');

        Attachment a = new Attachment(parentId = accid, name=myfile.name, body = myfile.body);
         
         /* insert the attachment */
         insert a;
        PageReference parentPage = new PageReference('/' + accid); 
        parentPage.setRedirect(true); 
        return parentPage;
    }   
    
    
 public void reload(){  
}
}

Please help!
Deepak GulianDeepak Gulian
public class attachmentsample {

    public attachmentsample(ApexPages.StandardController controller) {

    }
    Public Attachment myfile;
    Public Attachment getmyfile()
    {
        myfile = new Attachment();
        return myfile;
    }
   
    Public Pagereference Save()
    {
        String accid = System.currentPagereference().getParameters().get('id');

        Attachment a = new Attachment(parentId = accid, name=myfile.name, body = myfile.body);
         
         insert a;
        PageReference parentPage = new PageReference('/' + accid); 
        parentPage.setRedirect(true); 
        return parentPage;
    }   

}
<apex:page standardController="Tenant_Coordination__c" extensions="attachmentsample">
    <apex:form >
    <apex:inputfile value="{!myfile.body}" filename="{!myfile.Name}" />
       <apex:commandbutton value="Save" action="{!Save}" />
    </apex:form>
</apex:page>
Try this!

 
Tatiana Cooke 9Tatiana Cooke 9
Deepak, 

I am trying to relaod the Notes and Attachments section so it will need to refresh the parent page and any related lists. 

The above does not work. I think onclick or on complete must be in there. 

Appreciate any help!
 
Tatiana Cooke 9Tatiana Cooke 9
What about something that includes this?

https://developer.salesforce.com/forums/?id=906F000000096osIAA
Deepak GulianDeepak Gulian
I did something like this here
https://developer.salesforce.com/forums/ForumsMain?id=906F0000000D83SIAS

Replace child page
<apex:page standardController="Tenant_Coordination__c" extensions="attachmentsample">
<script>
function callParent(){
var winMain=window.opener; if (null==winMain){
winMain=window.parent.opener; }
winMain.closePopup();

</script>
<apex:form >
<apex:inputfile value="{!myfile.body}" filename="{!myfile.Name}" />
<apex:commandbutton value="Save" action="{!Save}" oncomplete="javascript:callParent();return false;" />
</apex:form>
</apex:page>
Deepak GulianDeepak Gulian
Your parent page is VF page or standard detail page ?
Tatiana Cooke 9Tatiana Cooke 9
The parent page is a custom object. NOT a visualforce. I know somehow I need to factor in two different domains. = /

I have a custom object for which I have made visualforce buttons that add attachments to the notes and attachments section. 

User-added image
 
Tatiana Cooke 9Tatiana Cooke 9
I tried using the below bulited answer, but no success. 

Is there a way to use it in my code?

 
If you do not want to add any Javascript in parent window and you have some parameter that is passed to popup window, you can reload parent window using the below script,
function closeWindow(){      
            window.opener.location.href="/{!$CurrentPage.parameters.id}";
            window.top.close();
      }
In my case I am passing Id of the record as parameter to the popup window. Make sure that "Developer Mode" is not enabled. If it is enabled, it will not work.