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
Shebin-KVP Business SolnsShebin-KVP Business Solns 

Get the instance url in apex code

Hi All,


  Anybody know how to get the instance url in apex code. My requirement is such that , a mail should be sent to  a user of the instance which containing  hyperlink to a vf page something like  "/apex/merge_profiles?id=....". Since  I need to use absolute url to open salesforce page from gmail or any mail , I have to  use the server name something like that na12 , ap1 ....

So the resultant url should become like   "".


Since I cannot hard code the  server name na12 or ap1, I should get it programatically. 


Please share anybody know this...


Thanks in Advance,

Shebin Mathew



Niket SFNiket SF

You can try


Following methods


  • ApexPages.currentPage().getUrl()   = Gives page name
  • ApexPages.currentPage().getHeaders().get('Host') => gives Host Id
  • Site.getCurrentSiteUrl()  => Site URL



Skype Name : niket.chandane

Shebin-KVP Business SolnsShebin-KVP Business Solns

Hello Nik,


 First of all Thanks alot for your support.


 My code is running in trigger and it is not related to a page so I cant use ApexPages.....


 I got a solution which is Apex  URL class there is one method getSalesforceBaseUrl().toExternalForm().


Currently I am doing testing for the same hope it will become success.


Anyway Thanks alot for your Kind Support.


Shebin Mathew



Niket SFNiket SF

ohh Sorry Shebin. I was not aware that you are doing this coding in trigger. 

But yes nice solution "getSalesforceBaseUrl().toExternalForm()





Shebin-KVP Business SolnsShebin-KVP Business Solns

Thanks alot Nik


 Have Fun with Apex and Visualforce...:smileyhappy:


Thanks alot.