9

I have a Visualforce page that gets embedded into the opportunity page layout. Part of the embedded page is a command button with an action defined in the controller to redirect the user to another page.

Snippet from controller method referenced by the command button:

public PageReference editDynamicProperties() {
    PageReference editpr = Page.AnotherPage;

    Map<string, string> params = editpr.getParameters();
    params.put('oppId', opportunityFromController.Id);
    // Attempt to remove the &inline query string parameter, but it isn't present yet.
    if(params.containsKey('inline')) {
        params.remove('inline');
    }
    editpr.setRedirect(true);
    System.debug(LoggingLevel.Info,'editDynamicProperties() redirecting to ' + editpr);
    return editpr;
}

When the button is pressed the redirection occurs, but the query string picks up the parameter &inline=1. As a result the page is rendered without any of the standard Salesforce content, such as the AppBodyHeader or sidebar. Manually removing the &inline=1 produces the desired results.

I tried explicitly removing the inline parameter from the PageReference, but it isn't present at that time. I've also tried explicitly adding &inline=0, the headers still get removed. It doesn't seem to matter what the value of inline is, only that it is present.

How can I redirect from an embedded Visualforce page to another page without becoming inlined?

3 Answers 3

3

I managed to hack my way around it by using JavaScript in the command button oncomplete to do the redirection from the top level window.

Simplified Page:

My reality is a bit more complicated as the Command button is a dynamic Visualforce component.

E.g. public PageReference stubAction() { return null; }

public PageReference editDynamicProperties() {
    PageReference editpr = Page.AnotherPage;

    Map<string, string> params = editpr.getParameters();
    params.put('oppId', opportunityFromController.Id);

    editpr.setRedirect(true);
    return editpr;
}

public Component.Apex.OutputPanel getDynamicPropertyControls() {
    //...

    Component.Apex.PageBlockButtons pbButtons = new Component.Apex.PageBlockButtons();
    pb.childComponents.add(pbButtons);

    Component.Apex.CommandButton editCommandButton = new Component.Apex.CommandButton();
    editCommandButton.value='Edit';
    //editCommandButton.expressions.action = '{!editDynamicProperties}';
    editCommandButton.expressions.action = '{!stubAction}';

    editCommandButton.rerender= new Set<string>{'dynamicForm'};
    editCommandButton.oncomplete= 'window.top.location.href = \''+editDynamicProperties().getUrl()+'\'';
    pbButtons.childComponents.add(editCommandButton);

    //...
}

UPDATE: Apparently I've come across this problem before. See Visualforce Page embedded in a detail page that needs to redirect to other page That question had a very similar answer by JCD, but the redirection was handled by the rerendered component.

3

As a simpler alternative to the first answer, you could also start using a simple <apex:outputLink> without an action attribute, but simply using the value attribute. Like so

<apex:outputLink value="{!$Page.somePage}?oppId={!opportunityFromController.Id}" target="_top">click here</apex:outputLink>

That will give you a URL without the inline part in it.

1
  • 1
    This works like a charm. It should be the accepted answer. Apr 12, 2016 at 15:54
0

instead of commandButton use outputlink with target attribute.

<apex:commandButton action="{ url to redirect}" target="_blank or _top">Click Here</apex:commandButton>

Replace with:

<apex:outputLink value="{ url to redirect}" target="_blank or _top">Click Here</apex:outputLink>
1
  • 2
    Welcome to SFSE! This answer is very similar to one given over 5 years ago. While similar answers can be fine, I like to see something to differentiate between them. Providing additional details, or framing your answer from a different perspective (and reaching a similar conclusion) are 2 ways you could accomplish that.
    – Derek F
    Sep 14, 2019 at 10:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .