Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visualforce Page embedded in a detail page that needs to redirect to other page

I have a Visualforce page that is embedded on the detail page of Opportunities.

Within the page is a command button that invokes a method in the backing controller extension.

Once the backing method is complete, how can I redirect the user to another page?

I can return a PageReference from the method but it will only redirect the iframe that the embedded Visualforce page is displayed in.

Ideally I'd like to refresh the top level window but I'm concerned there may be cross domain issues if the embedded visualforce page isn't in the same domain as the parent window.


As a basic test in I tried adding the following to the embedded Visualforce page:

<script>
    window.setTimeout(testRedirect,2000);
    function testRedirect() {
        top.location.reload();
    }
</script>

This resulted in Chrome logging the error:

Unsafe JavaScript attempt to access frame with URL https://na2.salesforce.com/006400000000000 from frame with URL https://ab2.na2.visual.force.com/servlet/servlet.Integration?lid=066400000000000&ic=1. Domains, protocols and ports must match.

So the domains differ for the Visualforce page.

like image 258
Daniel Ballinger Avatar asked Jul 19 '12 00:07

Daniel Ballinger


People also ask

How do I redirect from one Visualforce page to another?

1. If you want to directly link to the other page, there is the Visualforce $Page global variable. 2. If you want to send data back to the server, or otherwise perform some server based action before performing the redirect, you would want that method in your controller to return a PageReference.

How do I add an inline VF page to a page layout?

Follow the steps to embed visualforce page.Drag a new section into page layout. Now Select Visualforce page. Drag Visualforce from the list to new section. Click on Save Button.

What is PageReference in visualforce?

PageReference returns a reference to a Visualforce page, including its query string parameters. Using the page reference, use the getParameters method to return a map of the specified query string parameter names and values. Then a call to the get method specifying id returns the value of the id parameter itself.


1 Answers

It's a bit more code, but this works for me in all browsers, and I'm not getting any kind of cross-domain error.

Controller Extension:

public class Opp_Ext {
    private ApexPages.StandardController stdController;
    public String redirectUrl {public get; private set;}
    public Boolean shouldRedirect {public get; private set;}

    public Opp_Ext(ApexPages.StandardController stdController) {
        this.stdController = stdController;
        shouldRedirect = false;
    }

    public PageReference doStuffAndRedirect() {
        shouldRedirect = true;
        redirectUrl = stdController.view().getUrl();
        return null;
    }
}

VF Page:

<apex:page standardController="Opportunity" extensions="Opp_Ext" >
    <apex:form >
        <apex:commandButton value="Do Stuff" action="{!doStuffAndRedirect}" rerender="redirectPanel" />
        <apex:outputPanel id="redirectPanel" >
            <apex:outputText rendered="{!shouldRedirect}">
                <script type="text/javascript">
                    window.top.location.href = '{!redirectUrl}';
                </script>
            </apex:outputText>
        </apex:outputPanel>
    </apex:form>
</apex:page>
like image 66
JCD Avatar answered Sep 23 '22 19:09

JCD