6

I want to return a URL with a location hash in a page reference, but it appears that the system strips it out. Anybody know of a way to make this work without resorting to javascript? I can think of some ways to do it like that but I'd rather stick with standard functionality if possible.

This code:

 ApexPages.PageReference pr = new ApexPages.PageReference('/apex/APage#' + obj.Get('Id'));
 pr.SetRedirect(true);
 return pr;

Results in this URL in the browser:

<<instance>>/apex/APage

4
  • 1
  • I just posted an answer showing these methods, but it's not working for me right now! Will have to post when I get something functioning correctly. – Matt Lacey Jul 30 '14 at 8:06
  • 1
    Dang! Do you have the latest & greatest metadata version on all of the files involved with your page (including the page)? Looking forward to seeing this work using the PageReference class. – Mark Pond Jul 30 '14 at 8:10
  • Yeah, all brand new stuff. Looks like Lance might have found the secret incantations! – Matt Lacey Jul 30 '14 at 10:40
3

This seems to be a Salesforce bug to me. I did some investigation on this:

ApexPages.PageReference pr = new ApexPages.PageReference('/apex/newTestPage');
pr.setAnchor('123');
System.debug(pr.getAnchor());
System.debug(pr.getUrl());
pr.SetRedirect(true);
return pr;

pr.getAnchor() returns 123 and pr.getUrl returns apex/newTestPage#123 with no issue. But on the returning page, it is still /apex/newTestPage

However, I changed the pr definition line to be:

ApexPages.PageReference pr = new ApexPages.PageReference('/apex/newTestPage?id=456');

The returning page becomes /apex/newTestPage?id=456#123, which contains the anchor.

So one easy walk around is just include something like ?id=456 will resolve your issue. It is strange though however works.

2
  • Nice one Lance, I had a quick crack with the methods but observed it not working, didn't think to try a parameter! – Matt Lacey Jul 30 '14 at 10:39
  • 1
    In case anyone is interested, this issue also came up when redirecting to an Angular JS page that includes a hash tag for routing purposes (ex. /apex/angularPage#/contact/:contactId). Adding a parameter before the hash tag fixes the issue here too (ex. /apex/angularPage?a=1#/contact/:contactId). (Apologies for the repost, there was a typo in the workaround example). – Kyle Apr 17 '15 at 14:41

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.