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: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;
        return NULL;
    }   

}

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 the attachment */
         insert a;
        PageReference parentPage = new PageReference('/' + accid); 
        parentPage.setRedirect(true); 
        return parentPage;
    }   

}

Try this
RatanRatan
Small change you are using onclick
 
​<apex:commandbutton value="Save" action="{!Save}"onclick="window.top.location='/{!Tenant_Coordination__c.id}'; return true"/>

instead use oncomplete
 
​<apex:commandbutton value="Save" action="{!Save}" oncomplete="window.top.location='/{!Tenant_Coordination__c.id}'; return true"/>

So  when your controller logic execution completed after that it will refresh the page. 
Tatiana Cooke 9Tatiana Cooke 9
Team, 

I tried both ways and neither seemed to work for me. Any other ideas?

Appreciate the help. 
RatanRatan
It should work I think issue with return true. try to remove that
 
​<apex:commandbutton value="Save" action="{!Save}" oncomplete="reloadPage();"/>

<script>
function reloadPage()
{
      window.top.location='/{!Tenant_Coordination__c.id}'; 
}
</script>

 
Tatiana Cooke 9Tatiana Cooke 9
Team, 

Found the answer hidden in the darkest corners of the internet. Here is what finally worked. 

Ended up incorporating the below code into my visualforce page
<apex:actionFunction name="reloadparent" action="{!reload}" />

and the below code into the class
 
public void reload(){


Below is the final version. 


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;
        return NULL;
    }   
 public void reload(){  
}
}


Thanks for all your help!
 
Tatiana Cooke 9Tatiana Cooke 9
Team, 

So the above was working and now its finicky again? 

Can anyone help? I feel like I'm so close!!
RatanRatan
I suggest same ans as above

Use oncomplete instead onclick
 
<apex:commandbutton value="Save" action="{!Save}" oncomplete="window.top.location='/{!Tenant_Coordination__c.id}'; return true;"/>

 
Tatiana Cooke 9Tatiana Cooke 9
Hey Ratan, just tired it again and it is not working. 
apex:page standardController="Tenant_Coordination__c" extensions="attachmentsample">
    <apex:form >
    <apex:inputfile value="{!myfile.body}" filename="{!myfile.Name}" />
    <apex:commandbutton value="Save" action="{!Save}" oncomplete="window.top.location='/{!Tenant_Coordination__c.id}'; return true;"/>
    </apex:form>
</apex:page>

I get the following when I try that: 

User-added image

= (
Tatiana Cooke 9Tatiana Cooke 9
Is this the issue?
 
I found the solution so posting in hope that it may help some other lost soul. Basically I added a Page reference to the view of the Opportunity (see updated code below), whereas before I was simply letting the page refresh by not providing any rerender attribute.

public PageReference Del () {

if(delOLI!= null) {
Delete delOLI;
getOppFieldAndHistory(opp);
queryOLIs(opp.id);
}
PageReference oppPage = new ApexPages.StandardController(opp).view();
oppPage.setRedirect(true);
return oppPage;

How do I incorporate the above solution into my code?

Appreciate any help. 
Deepak GulianDeepak Gulian
I mentioned it in my above code with the pageReference
Tatiana Cooke 9Tatiana Cooke 9
Thanks Deepak, I am still having an issue where its not until I manually refresh the parent page again, do I see the attached file in the notes and attachments section. 

The code you have helped me with so far is not refreshing the related this. How do we incorporate that?