function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
highgoshhighgosh 

How to determine instance via APEX on a My Domain?

Using Apex in a trigger, how can one find the current SFDC instance (the pod - like "cs3") when in a sandbox logged in under a My Domain?  Using Url.getSalesforceBaseUrl().getHost(); doesn't seem to get us there.

Best Answer chosen by Admin (Salesforce Developers) 
highgoshhighgosh

I ended up finding this routing table, which allows me to do a query for the OrgId via Apex, and then look at the 4th character in the OrgID which explains which SFDC instance (server) this is on.

 

            "login" => array("Login: Production/Developer",""),
            "test"  => array("Login: Sandbox (test)",""),
            "prerellogin.pre" => array("Login: Pre-Release", ""),
            "na0-api" => array("NA0 (ssl)","0"),
            "na1-api" => array("NA1","3"),
            "na2-api" => array("NA2","4"),
            "na3-api" => array("NA3","5"),
            "na4-api" => array("NA4","6"),
            "na5-api" => array("NA5","7"),
            "na6-api" => array("NA6","8"),
            "na7-api" => array("NA7","A"),
            "na8-api" => array("NA8","C"),
            "na9-api" => array("NA9","E"),
            "na10-api" => array("NA10","F"),
            "na11-api" => array("NA11","G"),
            "na12-api" => array("NA12","U"),
            "na14-api" => array("NA14","d"),
            "ap0-api" => array("AP0 (ap)","1"),
            "ap1-api" => array("AP1","9"),
            "eu0-api" => array("EU0 (emea)","2"),
            "eu1-api" => array("EU1","D"),
            "eu2-api" => array("EU2","b"),
            "tapp0-api" => array("Sandbox: CS0 (tapp0)","T"),
            "cs1-api" => array("Sandbox: CS1","S"),
            "cs2-api" => array("Sandbox: CS2","R"),
            "cs3-api" => array("Sandbox: CS3","Q"),
            "cs4-api" => array("Sandbox: CS4","P"),
            "cs5-api" => array("Sandbox: CS5","O"),
            "cs6-api" => array("Sandbox: CS6","N"),
            "cs7-api" => array("Sandbox: CS7","M"),
            "cs8-api" => array("Sandbox: CS8","L"),
            "cs9-api" => array("Sandbox: CS9","K"),
            "cs10-api" => array("Sandbox: CS10","J"),
            "cs11-api" => array("Sandbox: CS11","Z"),
            "cs12-api" => array("Sandbox: CS12","V"),
            "cs13-api" => array("Sandbox: CS13","W"),
            "cs14-api" => array("Sandbox: CS14","c"),
            "cs15-api" => array("Sandbox: CS15","e"),
            "cs16-api" => array("Sandbox: CS16","f"),
            "cs17-api" => array("Sandbox: CS17","g"),
            "prerelna1.pre" => array("Pre-Release: NA1","t")

All Answers

Vinit_KumarVinit_Kumar

I am afraid you won't be able to et the instance using Apex if My domain is already deployed.

highgoshhighgosh

Hmmm, well, here's the challenge.  Let's say I need to create a URL that will hit a Sites page.  Let's say that the Site in question is running in a sandbox under a My Domain.  Per documentation, if you want to create an secure URL (SSL), for some reason, the requirement is for the link to be changed to the following format.

https://mycompany.mysandbox.cs1.force.com

...thereby requiring that we know which SFDC server this Site is sitting on.  

Documentation:  http://na14.salesforce.com/help/doc/en/sites_creating_subdomain.htm

I'm not sure it's possible for us to figure this out programmatically, since the System.URL.getSalesforceBaseURL().getHost(); in this situation will respond back without the "cs##" included in it.

Help?

Vinit_KumarVinit_Kumar

You are getting confused here.In

 

https://mycompany.mysandbox.cs1.force.com

 

mycompany is Site sub doamin which you have to register while creting a Force.com Site,it is not your My Domain which is the domain of your salesforce org not the Site.

 

I hope now it is clear to you.To get the URL of site try below code :-

 

Site.getCurrentSiteUrl();

 

You can also go through the below link for more :-

 

http://boards.developerforce.com/t5/Apex-Code-Development/Get-Current-Page-URL-in-Apex-Class-Full-amp-Exact-URL/td-p/134623

 

 

 

 

highgoshhighgosh

I ended up finding this routing table, which allows me to do a query for the OrgId via Apex, and then look at the 4th character in the OrgID which explains which SFDC instance (server) this is on.

 

            "login" => array("Login: Production/Developer",""),
            "test"  => array("Login: Sandbox (test)",""),
            "prerellogin.pre" => array("Login: Pre-Release", ""),
            "na0-api" => array("NA0 (ssl)","0"),
            "na1-api" => array("NA1","3"),
            "na2-api" => array("NA2","4"),
            "na3-api" => array("NA3","5"),
            "na4-api" => array("NA4","6"),
            "na5-api" => array("NA5","7"),
            "na6-api" => array("NA6","8"),
            "na7-api" => array("NA7","A"),
            "na8-api" => array("NA8","C"),
            "na9-api" => array("NA9","E"),
            "na10-api" => array("NA10","F"),
            "na11-api" => array("NA11","G"),
            "na12-api" => array("NA12","U"),
            "na14-api" => array("NA14","d"),
            "ap0-api" => array("AP0 (ap)","1"),
            "ap1-api" => array("AP1","9"),
            "eu0-api" => array("EU0 (emea)","2"),
            "eu1-api" => array("EU1","D"),
            "eu2-api" => array("EU2","b"),
            "tapp0-api" => array("Sandbox: CS0 (tapp0)","T"),
            "cs1-api" => array("Sandbox: CS1","S"),
            "cs2-api" => array("Sandbox: CS2","R"),
            "cs3-api" => array("Sandbox: CS3","Q"),
            "cs4-api" => array("Sandbox: CS4","P"),
            "cs5-api" => array("Sandbox: CS5","O"),
            "cs6-api" => array("Sandbox: CS6","N"),
            "cs7-api" => array("Sandbox: CS7","M"),
            "cs8-api" => array("Sandbox: CS8","L"),
            "cs9-api" => array("Sandbox: CS9","K"),
            "cs10-api" => array("Sandbox: CS10","J"),
            "cs11-api" => array("Sandbox: CS11","Z"),
            "cs12-api" => array("Sandbox: CS12","V"),
            "cs13-api" => array("Sandbox: CS13","W"),
            "cs14-api" => array("Sandbox: CS14","c"),
            "cs15-api" => array("Sandbox: CS15","e"),
            "cs16-api" => array("Sandbox: CS16","f"),
            "cs17-api" => array("Sandbox: CS17","g"),
            "prerelna1.pre" => array("Pre-Release: NA1","t")
This was selected as the best answer
DuTomDuTom

This mapping does not always work.

We were moved to a new POD and the OrgId did not change (4th char still represents to orig org not the new org)

 

 

Also my findings show then the Url.getSalesforceBaseUrl().getHost(); returns the domain it was instatiated

Ex:

API (Force IDE execute Anon) will return the normal api domain with pod

when running APEX from native UI ( developer console) it returns the mydomain name

and when in a VF controller returns the VF domain.

 

Please reply if anyone finds a workaround.

AshishkAshishk
Query on Organinzation object :- [Select o.InstanceName From Organization o], you can get instance name in any org, with or without domian.
Rakesh K CGMergeRakesh K CGMerge
@Ashishk - Which permission and minimum profile is required to query Organization object?