I'm having issues coming up with the instance name (i.e. na1) needed for generating a URL to a VF page with "My Domains" enabled.

The changes for My Domains are described well here: https://na15.salesforce.com/help/doc/en/domain_name_app_url_changes.htm

I am trying to construct: https://$subdomain--$yournamespace.na1.​visual.force.com​/apex/​ within an apex batch job.

In the past I have used: String sfUrl=URL.getSalesforceBaseUrl().getHost() to pull out the instance name (na1, etc).

However with My Domains enabled String sfUrl=URL.getSalesforceBaseUrl().getHost() outputs customdomaintest-dev-ed.my.salesforce.com. Notice that the instance name is not present. So, I'm unsure of how to get the instance name which I need to construct the Visualforce page url.

Does anyone have any tricks up their sleeve? The only option I know of right now is having a org wide custom setting that can be manipulated to impact the url.

  • Why do you need the instance name?
    – sfdcfox
    Commented Jul 24, 2013 at 21:28
  • I need the instance name in order to generate the link to a VF page. Notice the na1 in https://$subdomain--$yournamespace.na1.​visual.force.com​/apex/​ . Simply dropping it or any replacement I have tried results in no page being rendered. Commented Jul 25, 2013 at 0:00
  • 1
    I would have thought that My Domain would allow the page to render without knowing an instance name. I'm going to toy with this a bit.
    – sfdcfox
    Commented Jul 25, 2013 at 13:24

2 Answers 2


Edit: @sfdcfox pointed out that you can take advantage of the existing redirection structure on salesforce.com in this situation.

Instead of constructing the visual.force.com url, simply construct the salesforce.com url, which is very simple given the URL.getSalesforceBaseUrl().getHost() method.

Example code:

String sfUrl=URL.getSalesforceBaseUrl().getHost();
String myURL = 'https://'+sfUrl+'/apex/NAMESPACE__pagename?Id=';

This is a much cleaner solution and appears to work in all cases.

Initial workaround:

This is my work around. The main issue has been overcome by just randomly assigning a valid SF instance name. They seem to be redirected to the appropriate instance depending upon their login cookie/session. From there I am simply parsing the url to figure out the correct VF page format.

String sfUrl=URL.getSalesforceBaseUrl().getHost().replace('salesforce','visual.force');
String myURL = 'https://namespace.'+sfUrl+'/apex/pagename?Id=';
    String customDomain=sfUrl.split('.my.')[0];
    //We are assuming na1 as we are redirected to whatever instance we need.
    //Hopefully they don't do away with na1 and break this!
    myURL = 'https://'+customDomain+'--namespace.na1.visual.force.com/apex/pagename?Id=';

This seems to work in all the cases I have found.

  • Unless, of course, the user is logged into multiple instances and they happen to get one of these links. Of course, I expect that to be exceedingly rare, as it basically only applies to developers, but it might happen. Also, it would fail on Sandbox organizations, which do not interact with production organizations.
    – sfdcfox
    Commented Jul 25, 2013 at 13:34
  • 1
    I'm not sure how this approach would be any different from the normal approach of generating links when logged in to multiple orgs. Seems like it all just depends on what context/browser/cookie the link is opened with. Agree on the sandboxes, luckily this case applies to generating email contents (which won't fire in a sandbox). There is probably still an outstanding question of generating the links in a sandbox. Commented Jul 25, 2013 at 13:42
  • 1
    I just tested this, and <ns>-dev-ed.my.salesforce.com/apex/page prompted me to login and displayed the page correctly. The redirect to na3 (my home instance) happened automatically. I suspect something else is going on with your link instead.
    – sfdcfox
    Commented Jul 25, 2013 at 14:15
  • Furthermore, as long as I was already logged in, it automatically sent me to my instance correctly without any hacks of any sort.
    – sfdcfox
    Commented Jul 25, 2013 at 14:15
  • 1
    That is actually the solution I bet. I wound up constructing the visual.force.com domain a while back and don't see any real reason to keep it and not allow the redirect/resolution from salesforce.com -> visual.force.com. My new url is simply https://customdomaintest-dev-ed.my.salesforce.com/apex/NAMESPACE__pagename?id= Commented Jul 25, 2013 at 18:55

The modern answer to that is to build the VF URL literally and not wait for a redirect, otherwise you might encounter System.VisualforceException: org.apache.commons.httpclient.RedirectException: Maximum redirects (100) exceeded

String vfUrl = 'https://' + Url.getOrgDomainUrl().getHost().substringBefore('.') + '--' + namespace + '.vf.force.com/apex/' + pageName;

You must log in to answer this question.

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