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

How to get server URL in Apex?

I want to send a e-mail with a link to an object...How would one get server URL in Apex?

Best Answer chosen by Admin (Salesforce Developers) 

All Answers


This is the format:
  https://YOUR OBJECT ID


I need the dynamic instance...based upon the actual could be cs2, na1, na2, na5...

Where are you developing s-control or VF page?
In each there are some functions that you may be able to use.


You may be able to use the following code:

  PageReference myPage = ApexPages.currrentPage();
  String Url = myPage.getUrl();



TehNrd, thanks. I will add my vote to the idea...

based on the link, you posted, the best idea so far seems to be using't work for sandbox, but it seems better compared to creating a custom object (that's what I had thought  of as well).


I tried that before.

It just returns a relative URL, not the full URL. I took a standard controller for Account(my context isn't a VF page), and tried using getuRl...didn't work...

You can always use the following javascript code to get the URL:

// getDomain() is called to obtain the domain portion of web app
function getDomain()
    var url = window.location.toString();
    var domain = url.match( /:\/\/(www\.)—([^\/]+)/ ); 
    domain = domain[2]?domain[2]:''; 
    return domain;

Message Edited by Sam.arj on 07-29-2008 10:10 AM

Message Edited by Sam.arj on 07-29-2008 10:18 AM
Thanks! Sam,

Sounds like a good idea...won't work for me...

My code is in a trigger that is fired due to a workflow rule being executed. I am trying to send e-mail reminders to people on their tasks (with a URL link to task)...



Hi GoForceGo,

Did you ever figure this one out? I'm trying to do something very similar to what you are doing, i.e., programmatically create a task and email notification which contains the full URL to the task, using Apex. Just wondering if you ever came up with a satisfactory solution on this before I go and try to re-invent the wheel. TIA.

The best solution so far is:

This won't work for Sandbox, but will do the job for production.

Hmm... I tried that. I kept getting a "URL No Longer Exists" error. That's ok. I worked around it by saving the Salesforce hostname as a field on a custom object type that I use to store other application settings. That approach is good enough for my purposes. Thanks.
May be the object got deleted.

A custom "configuration" object is obviously a good idea!

This was selected as the best answer

Cool and thanks!  Hidden stuff


A standard page's header has "Host" in it's map!


This works in Apex, but how would I get the server URL in a standard e-mail template? Frequently you want to to send a link to the user to an record in an e-mail template.