3

I'm unable to get a VF page to load with an anchor in the URL if I redirect server-side. After clicking a button, I'm doing a DML operation and redirecting to a VF page. In the redirect, I'd like to add an anchor to the URL which is handled by JS to open the page in edit mode once loaded.

Redirecting page

<apex:page controller="TestRedirectController" showHeader="true" sidebar="true" action="{!autoRedirect}">
</apex:page>

Controller

public with sharing class TestRedirectController {
    public TestRedirectController() {}

    public PageReference autoRedirect() {
        PageReference pg = Page.TestRedirect2;
        pg.setAnchor('test');
        return pg;
    }
}

The resulting URL is https://c.na17.visual.force.com/apex/testredirect2. In contrast, when I redirect to the home page, I get https://na17.salesforce.com/home/home.jsp#test.

How can I redirect to a VF page with an anchor in the url?

EDIT

As suspected, it appears the anchor is lost during the redirection from salesforce.com to visual.force.com. Passing the full VF url to the PageReference works as expected:

public with sharing class TestRedirectController {
    public TestRedirectController() {}

    public PageReference autoRedirect() {
        PageReference pg = new PageReference('https://c.na17.visual.force.com/apex/testredirect2');
        pg.setAnchor('test');
        return pg;
    }
}

Since this is in a managed package, I would love to see workarounds to this problem that don't require having to pre-build the fully-qualified VF page URL.

  • what API version is your vf page? – pchittum Aug 4 '14 at 20:17
  • I'm using v30... – geeljire Aug 4 '14 at 21:15
  • hmmm...yeah, I am able to reproduce this exactly as you specify, even on API 31. is there a reason you don't want to just pass as a URL parameter? – pchittum Aug 4 '14 at 21:52
  • Yeah, I ended up using a url parameter at the end. I'll file a case with support soon. – geeljire Aug 4 '14 at 23:11
3

This appears to be a bug. Please file a case with support. When I do exactly as you state, I get the exact same behavior.

In attempting to just pass a parameter, I left the setAnchor call in my code, and the anchor was preserved in the URL as follows:

https://c.na11.visual.force.com/apex/testredirect2?testparam=test#test

But even here, PageReference.getAnchor() is not successfully reading the anchor when I call it on CurrentPage.

The best workaround I can offer would be to pass something as a URL parameter. But I realize that certain JS frameworks look for specific anchors in the URL, and this might not be ideal.

I tried something really odd just because I was curious, which was to add setAnchor to the constructor of the controller of the redirect destination page, using a parameter to set the anchor. The result was strange in that it was readable by getAnchor in a getter method later, but didn't actually show up in the URL on the client in the address bar. That bit of ugly mess looked like this:

public TestRedirectController2() {//constructor
    String an = ApexPages.currentPage().getParameters().get('testparam'); 
    ApexPages.currentPage().setAnchor(an);
}

public String getAnchor(){
    String anchor = ApexPages.currentPage().getAnchor();
    System.debug('Anchor is: ' + anchor);
    //above outputs 'test' to log
    //method outputs 'test' to page when bound to
    return anchor; 
}

In any case, none of this seems to be acting as an API to work with URL anchors ought to by any reasonable measure. All signs point to bug.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.