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:






Code:
// 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;
}

6 comments:

  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)

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

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

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

    ReplyDelete
  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?

    ReplyDelete
  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 ...

    ReplyDelete