Saturday, September 13, 2008

How to get your SalesForce instance URL address

Often I see programmers has a challenge to problematically find what their SalesForce instance address is.

Whatever, your instance might be:
- cs2
- emea
- na5
- na4

One way of tackling this issue that you might find it useful:

You can always use the following javascript code to get the URL:

// getDomain() is called to obtain the domain portion of web app
function getDomain()
var url = window.location.toString();
var domain = url.match( /:\/\/(www\.)—([^\/]+)/ );
domain = domain[2]?domain[2]:'';

return domain;


  1. Good solution Sam!

    I've taken a slightly different approach to determining the current pod URL that is perhaps a bit more pedestrian. I use the following formula in custom links/custom web tabs, S-controls, etc.:

    MID( $Api.Enterprise_Server_URL_130, 9, 18)

    1. Can you show me an example of using this with a custom link?

  2. Great post.
    Helps me a lot.
    But i want instance in apex class.
    Can we do that?

  3. It's best to use relative path in Apex, as your code may be deployed in to various production environments.

  4. Hi Sam,

    Can you please send me a sample example of this. I am a newbie to Salesforce. How to implement this in VF pages?

  5. hey.. i hv written a method to capture the domain url.. i wantu call that method in a trigger..I have called it like this : classname.methodname(parameter); .trigger is throwing an error which says ... 'parameter' doesn exist.. can smbdy help ...