Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

Is there a way to get the URL of a salesforce site from apex

I need this to send the url in an email message, so the apex is run outside of the site context. I basically want to expose a link to my salesforce site in an email message. But so far I haven't found a way to do that

I would prefer not hard coding the url in apex

share|improve this question
    
Are Sites available in some table you can query? Like select NamespacePrefix, Name from ApexPage limit 10? –  eyescream Jan 24 '13 at 17:29
1  
yes, you can query them: [SELECT Name, Status, Subdomain FROM Site] this only gives me the subdomain in subdomain.force.com, this should work well when installed on a production org, but when using the app on a dev org / sandbox you get a different URL for example: http://subdomain.na14.force.com/sitename –  PJC Jan 24 '13 at 17:35
1  
I just posted an idea on the IdeaExchange to make this easier to do. Please vote! –  Benj Oct 3 '13 at 0:57
    
Great idea, voted Benj. –  greenstork Oct 3 '13 at 3:22

6 Answers 6

This used to work. I didn't tested it recently, try it out.

Site s = [SELECT s.Subdomain FROM Site s LIMIT 1 ];
String urlCode = String.valueof(URL.getSalesforceBaseUrl().toExternalForm());
String [] cuts = urlCode.split('[.]');
urlCode = '';
for(Integer i = 1; i < cuts.size(); i++){
    if(!cuts[i].contains('visual')){
        urlCode += cuts[i] + '.';
    }
}     
urlCode = urlCode.substring(0, urlCode.length()-1);  
urlCode = 'https://'+s.Subdomain+'.'+urlCode+'/yourpage;
share|improve this answer
    
I'll give it a try, and keep you posted, thanks for the code! –  PJC Jan 24 '13 at 22:12
    
unfortunately this doesn't seem to give me the correct site address on a developer org, but will probably work on a production org though –  PJC Jan 25 '13 at 13:26
    
Is that because DE orgs have the "-developer" portion enforced on the URL? –  joshbirk Feb 20 '13 at 15:45

If you are looking to associate to a single record, you will need to use the PageReference class. Specifically:

ApexPages.currentPage().getHeaders().get('Host');

and

ApexPages.currentPage().getHeaders().getUrl();

To piece that together you would use the following:

String url = 'https://' + ApexPages.currentPage().getHeaders().get('Host') + ApexPages.currentPage().getHeaders().getUrl();

If you are looking to link to a specific object, you just need the host and the ID of that object. Say you want to link to an account for instance and the account you have is referenced by the acc variable:

String url = 'https://' + ApexPages.currentPage().getHeaders().get('Host') + '/' + acc.Id;
share|improve this answer
    
I don't have a Page i'm executing from, so there is no Apexpages available, it is run directly from apex. –  PJC Jan 24 '13 at 16:53
    
Ah ok. I don't know if there is a way to get the host outside of the Page context. In this scenario, to avoid hard coding a value, I would just create a custom setting and reference that. You can then update that custom setting whenever needed. It is a similar concept to a property file used in other programming languages. –  Jesse Altman Jan 24 '13 at 16:56
    
I know custom settings, but the thing is, i'm trying to write an app, and having to set a custom setting per deployment is quite a hasstle –  PJC Jan 24 '13 at 17:01
up vote 1 down vote accepted

This doesn't seem to be possible from apex, I ended up trying the different ways described above, and decided to go with a Custom Setting. I added the Custom Setting Site URL which needs to be filled in every time when the app gets configured.

share|improve this answer

Please refer the below link:

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

seems you might need to make use of

apexpages.currentpage().getURL() and apexpages.currentpages().getHeaders().get('HOST')
share|improve this answer
1  
that's not answering my question, I need the site url like so: 'https://mysaleforceorg.force.com/', Site.getCustomWebAddress() keeps returning NULL because it is run from outside the site context –  PJC Jan 24 '13 at 16:50

As others have noticed, this isn't currently possible in Apex. I've posted an idea on the IdeaExchange to request that this feature be added. Please consider voting for it!

https://success.salesforce.com/ideaView?id=08730000000kvysAAA

share|improve this answer
String sfdcBaseURL = URL.getSalesforceBaseUrl().toExternalForm();

This should work for your requirement.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.