Pages

Friday, July 22, 2011

Clean Up Classes w/ Hard-Coded SF URLS

Whether you are building something for an internal VF page or an external site, you'll be instantiating instances of the pageReference class.  <-- Did I say that right?  May not have mentioned this, but I have no background in traditional coding, just what I've picked up through my SF work.

For a while, I found my self doing something like this:
pageReference pageRef = new pageReference('https://na4.salesforce.com/apex/mypagename');

Obviously, those links would break when pushed from a sandbox to production and I would have to modify the code.

To save time, I came up with this to prevent those changes:
String hostname = ApexPages.currentPage().getHeaders().get('Host');
pageReference pageRef = new pageReference ('https://'+hostname +'/yourpagenamehere’);
system.debug('pageRef was:  ';+ pageref);
return pageRef;