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
kminevkminev 

Get server url in Apex code

Hi,

 

I know this has been posted before, but I have't found a solution to my question.

 

I need to able to get the server url via apex code. For example: https://tapp0.salesforce.com/

 

Is there a call that does that already and if not are there any work-arounds?

 

Thank you very much.

Best Answer chosen by Admin (Salesforce Developers) 
JmBessonartJmBessonart

Hi, here is the code:

 

public static String getDomain () { String hostname = ApexPages.currentPage().getHeaders().get('Host'); String domain = 'https://' + hostname + '/apex'; return domain; }

 

 

 

I hope help you.

J.

All Answers

JmBessonartJmBessonart

Hi, here is the code:

 

public static String getDomain () { String hostname = ApexPages.currentPage().getHeaders().get('Host'); String domain = 'https://' + hostname + '/apex'; return domain; }

 

 

 

I hope help you.

J.

This was selected as the best answer
kminevkminev
Actually that does not work in apex trigger. Any ideas why?
Richie DRichie D

Hi,

 

You won't be able to get access to any 'apexpages' stuff within the trigger. It is not guaranteed that the trigger is being fired from a web request - could be from data loader, workflow etc... 

 

Can I ask why you need the server url??

 

If you do need it you can always store it in a custom object (or custom setting) and pull it out when required within the trigger. This would have to be done within an apex page controller.

 

Regards,

R.

kminevkminev

The reason why I need the url is to embed url link in my email body that I am sending from apex class/trigger.

For example myUrs + caseId

 

 

Thanks

 

Richie DRichie D

Fair enough....

 

You could create a static parameter which holds the url value that is populated via a controller somewhere else. You'd just need to make sure the parameter is populated before you need to use it. Alternatively, stick it in a custom object.

 

Can't think of anyother sensible ways to do it. Perhaps someone else has some ideas??? 

 

R.