I need to return this value:
Is there anyway to get the registered Force.com domain for a given org, with raw Apex code or Metadata API?
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It only takes a minute to sign up.
Sign up to join this communityI need to return this value:
Is there anyway to get the registered Force.com domain for a given org, with raw Apex code or Metadata API?
You just need SOQL
/SOSL
on the Domain
object. Just to double check, I looked at a domain, saw a url of instance.salesforce.com/0I4?parameters=values
, and ran:
system.debug(Id.valueOf('0I4000000000000').getSObjectType()); // yields Domain
The Domain Name
column you have highlighted is represented by the Domain
field, leading to a somewhat silly looking query:
SELECT Domain FROM Domain
I have also found that with any Site
records configured, you get one record in that list view that is not a domain at all, but a link to your Site
tab. The url you land on if you follow the link will look like instance.salesforce.com/0DM?parameters=values
. If you want the value of the "Domain Name" from that column, you have to get a bit more hack-y. I modified the code posted there by @mattandneil just a bit to get the exact value:
Site site = [SELECT GuestUserId FROM Site WHERE UrlPathPrefix != null LIMIT 1];
String path = '/sites/servlet.SiteDebugMode';
PageReference pr = new PageReference(path);
pr.getParameters().put('sitedebugmode', 'x');
pr.getParameters().put('guid', site.GuestUserId);
String url = pr.getContent().toString().substringAfter('URL=').substringBefore(path);
system.debug(url.substringBetween('//', '/'));
I do not believe this behavior is supported, but it has worked for at least two years, and does so currently.
Domain
after a bit of digging.
you can store this in custom setting. Although if you still need this in that case you can take help from URL class
Url.getSalesforceBaseUrl().getHost();
or you can query Domain sObject
Select id,Domain from domain
CreatedBy
someone else. Hmm...