Get Site URL from apex

  • 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

    Are Sites available in some table you can query? Like `select NamespacePrefix, Name from ApexPage limit 10`?

    yes, you can query them: `[SELECT Name, Status, Subdomain FROM Site]` this only gives me the subdomain in, 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: ``

    I just posted an idea on the IdeaExchange to make this easier to do. Please vote!

    Great idea, voted Benj.

  • pjcarly

    pjcarly Correct answer

    8 years ago

    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.

    that's simply wrong - at least today. `URL.getSalesforceBaseUrl().toExternalForm()` returns the complete org-url.

License under CC-BY-SA with attribution

Content dated before 7/24/2021 11:53 AM