+ Start a Discussion
steeeeveeeeeesteeeeveeeeee 

Refreshing Salesforce page from Embedded Visualforce page Problem

My question is based on this and other threads which have solved this problem:

 

http://forums.sforce.com/t5/Visualforce-Development/Embedding-Visual-FOrce-page-in-standard-layout-access-to-std/td-p/116543

 

However in these other threads, they managed to have solved the problem window.top.location.href = "{!standardobject.id}".  This reloads the same page but however loses the window placement which is kept by a refresh.  I've tried using window.top.location.refresh() in the same context but to no avail.  

 

Is there another way of refreshing the parent salesforce page from a visualforce page?

 

Thanks in advance

Best Answer chosen by Admin (Salesforce Developers) 
DaveHDaveH

So I decided to write up a quick test page to demostrate whether it should work or not and it works for me. I'm not sure if this is exactly what you're looking for but feel free to copy this and load it into your sandbox to test it. 

 

NOTE: I tried this in Chrome and IE9 and both worked perfectly.

 

This page is loaded by going to the URL: https://YOUR_ORG/apex/ScrollLockTest?scroll_pos=5000 (if your page name is ScrollLockTest)

 

the param scroll_pos will tell the page where to scroll to when loaded

 

The controller:

public class ScrollLockTestController{

    public List<TableObject> tableData {get; set;}
    public Integer scrollPos {get; set;}
    public Boolean hasPosParam {get; set;}
    
    public ScrollLockTestController() {
        // Load test data
        String baseStr = 'Dummy Data ';
        this.tableData = new List<TableObject>();
        
        for(Integer i = 0; i < 500; i++) {
            TableObject obj = new TableObject();
            obj.data = baseStr + String.valueOf(i);
        	tableData.add(obj);
        }
        
        this.hasPosParam = false;
        String scrollPosParam = ApexPages.currentPage().getParameters().get('scroll_pos');
        
        if(scrollPosParam != null) {

            this.scrollPos = Integer.valueOf(scrollPosParam);
            this.hasPosParam = true; 
        } else {
        	this.scrollPos = -1;    
        }
    }
    
    public class TableObject {
        public String data {get; set;}
    }
}
           
           
           
           
           
           
           
           

 

 

The VF Page: 

<apex:page controller="ScrollLockTestController">
    <apex:pageBlock>
    <apex:pageBlockTable value="{!tableData}" var="obj">
    	<apex:column value="{!obj.data}"/>
    </apex:pageBlockTable>
    </apex:pageBlock>
    <script>
        pagePos = {!scrollPos};
        function savePos() {
        	pagePos = (document.pageYOffset?document.pageYOffset:document.body.scrollTop);
        }
        
        function restorePos() {
			window.scrollTo(0, pagePos);
        }
    </script>
    <apex:pageBlock rendered="{!hasPosParam}">
        <script>restorePos();</script>
    </apex:pageBlock>
</apex:page>

 

 

A couple things to note:

1) This page currently just checks for the existence of a page position param. I added the javascript method to save the param when reloading but you can take it from there.

2) I used a page block to render the script tag if the param was present. This is just a sloppy quick way to do it so obviously another way would be used for a production app.

 

Let me know if you have any questions.

All Answers

DaveHDaveH

I'm having trouble understading what you're trying to do. I think I get that you have an embedded VF page within a standard page layout and you want to reload the standard layout from within the controller of your VF page.

 

Am I right so far?

 

If so, I don't get what you mean by you "lose window placement". Could you clarify that a little for me?

steeeeveeeeeesteeeeveeeeee

Yep that's exactly what I'm trying to do.  My problem exists with the solutions I've found so far on the board which consists of putting "/+Standard object Id".  When you refresh a page while looking halfway through it the window returns to that area.  I'm trying to mimic that function

DaveHDaveH

Hmm ok. What about on reload of the page passing the page scroll index as a parameter to the page and then using that to scroll back to that specific area. That was hard to explain so I laid out the steps below.

 

1) User visits the std page with your VF page built-in

2) When the VF page reloads the window using window.top.href pass an extra param like page_pos (ex. https://na11.salesforce.com/003000000000034Tgfd?page_pos=100

3) When your VF page loads check for the page_pos param. If found use javascript to scroll the page down to the supplied position

 

Does that make sense?

steeeeveeeeeesteeeeveeeeee

Thanks for the reply DaveH.  

 

I've tried it and it doesn't work for me (in Chrome)  also wouldn't manually setting a value not scale with different records and different layouts?  Is there a reason a reload() doesn't work on the page in this aspect or another way to go about this?

DaveHDaveH

So I decided to write up a quick test page to demostrate whether it should work or not and it works for me. I'm not sure if this is exactly what you're looking for but feel free to copy this and load it into your sandbox to test it. 

 

NOTE: I tried this in Chrome and IE9 and both worked perfectly.

 

This page is loaded by going to the URL: https://YOUR_ORG/apex/ScrollLockTest?scroll_pos=5000 (if your page name is ScrollLockTest)

 

the param scroll_pos will tell the page where to scroll to when loaded

 

The controller:

public class ScrollLockTestController{

    public List<TableObject> tableData {get; set;}
    public Integer scrollPos {get; set;}
    public Boolean hasPosParam {get; set;}
    
    public ScrollLockTestController() {
        // Load test data
        String baseStr = 'Dummy Data ';
        this.tableData = new List<TableObject>();
        
        for(Integer i = 0; i < 500; i++) {
            TableObject obj = new TableObject();
            obj.data = baseStr + String.valueOf(i);
        	tableData.add(obj);
        }
        
        this.hasPosParam = false;
        String scrollPosParam = ApexPages.currentPage().getParameters().get('scroll_pos');
        
        if(scrollPosParam != null) {

            this.scrollPos = Integer.valueOf(scrollPosParam);
            this.hasPosParam = true; 
        } else {
        	this.scrollPos = -1;    
        }
    }
    
    public class TableObject {
        public String data {get; set;}
    }
}
           
           
           
           
           
           
           
           

 

 

The VF Page: 

<apex:page controller="ScrollLockTestController">
    <apex:pageBlock>
    <apex:pageBlockTable value="{!tableData}" var="obj">
    	<apex:column value="{!obj.data}"/>
    </apex:pageBlockTable>
    </apex:pageBlock>
    <script>
        pagePos = {!scrollPos};
        function savePos() {
        	pagePos = (document.pageYOffset?document.pageYOffset:document.body.scrollTop);
        }
        
        function restorePos() {
			window.scrollTo(0, pagePos);
        }
    </script>
    <apex:pageBlock rendered="{!hasPosParam}">
        <script>restorePos();</script>
    </apex:pageBlock>
</apex:page>

 

 

A couple things to note:

1) This page currently just checks for the existence of a page position param. I added the javascript method to save the param when reloading but you can take it from there.

2) I used a page block to render the script tag if the param was present. This is just a sloppy quick way to do it so obviously another way would be used for a production app.

 

Let me know if you have any questions.

This was selected as the best answer