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 retrieve server URL in APEX class or in trigger?

i want to retrieve the Server URL in trigger or in APEX class?
Actual my requirement is:
i had an object RelatedAttachement. in that Hyperlink field is of URL type. in that i am saving the URL like /servlet/servlet.FileDownload?file=00PR0000000MqVH. For this i want to retrieve the server URL and concatenate that Server URL to this field and updating this field.  in one server it looks like and in another server it looks like so for this one i want to retrieve the server URL and i want to concatenate that URL to that field.
So please tell me how to retrieve the Server URL in trigger or in APEX class? 
Hi Padma,

 Have you tried with the geturl() function?
Doug ACFDoug ACF
Not sure if this fits your use case, but in Visualforce controllers, you can use ApexPages.currentPage().getHeaders().get('Host') to get the server name (e.g.,

Message Edited by Doug ACF on 11-02-2008 10:16 AM
Thanks so much. The resulting links require a login, even though we were just logged in. This is what I'm trying to solve. Any Ideas would be much appriciated.


Here's what I've done.
In the VF page datatable:
<apex:outputLink value="http://{!Host}/{!}" target="_blank">

In the Controller Class :

public string getHost(){
return ApexPages.currentPage().getHeaders().get('Host');

The resulting links require a login, even though we were just logged in. This is what I'm trying to solve.

(I've checked that the resulting links are well formed: example: )
just by way of a reminder: relative links are working, but offcourse not when I render the content as Excel.

Also, I'm sure there's a way of obtaining these parameters using code in the page, and not in the Apex class. any ideas?

Thanks so much

Message Edited by beener on 01-03-2009 04:39 AM

Change the output link to point to 'https' instead of 'http'.




This should not have been marked solved. The original question was "So please tell me how to retrieve the Server URL in trigger or in APEX class? "


Neither Triggers nor standard Apex classes (that are not controllers) have access to ApexPages.currentPage().


It still baffles me that there is just not something as simple as {!Organization.ServerHost} yet. 


Try using the following. It pulls the URL using the standard controller from any object.


String url = new ApexPages.StandardController(acct).view().getHeaders().get('Host')



@ hmmm, are you sure? I've used this code in a number of places and it works.
Message Edited by wesnolte on 06-08-2009 12:58 AM

Yeah. I'm positive that ApexPages.currentPage().getHeaders().get('Host') fails in a Trigger because ApexPages.currentPage() has no context in a class or trigger not associated with a VF page. It will give you a nice System.NullPointerException.


ScrappyDog's suggestion looked more promising, but all it returned was NULL. Here's my code. Let me know if I'm doing anything  wrong. 


Here's what I've tried;


  • String host = new ApexPages.StandardController(sol).view().getHeaders().get('Host'); returns null
  • String host = pageRef.getHeaders().get('Host'); returns null
  • String host = pageRef.getURL(); returns only the partial url




trigger AttachedSolutions on CaseSolution__c (after delete, after insert, after undelete,
after update) {

List<CaseSolution__c> impactingSolutions = new List<CaseSolution__c>();
List<Id> caseIds = new List<Id>();
List<Case> casesToUpdate = new List<Case>();
Map<Id, String> caseSolutions = new Map<Id, String>();

If (Trigger.isDelete || Trigger.isUnDelete) {
ImpactingSolutions = Trigger.old;
} else {
ImpactingSolutions =;

//build list of cases we need to update
for (CaseSolution__c sol : impactingSolutions) {

if (caseIds.size() > 0){
List<Case> updatedCases = [select Id
from Case
where Id in :caseIds

List<CaseSolution__c> solAttached = [select Id
from CaseSolution__c
where Case__c in :caseIds

for (CaseSolution__c sol : solAttached) {
PageReference pageRef = new PageReference('/' +;
String host = new ApexPages.StandardController(sol).view().getHeaders().get('Host'); //returns null
String Url = 'https://' + host + pageRef.getUrl();
String cseSolution = sol.Name + ' - ' + Url + '\n\n';
if (!caseSolutions.containsKey(sol.Case__c)){
} else {
cseSolution += caseSolutions.get(sol.Case__c);

for (Case cse : updatedCases){
cse.KB_Solution_Title_Links__c = caseSolutions.get(cse.Id);

update casesToUpdate;





Message Edited by Eric_Santiago on 06-08-2009 08:36 AM

Ah, I see. Sorry bud.


Conceptually it makes sense that you don't have access to that information at a trigger level, as a trigger is usually associated with operating within(or on) the database.


The only thing I can think of immediately would be to write the value of the afformentioned method to the database at some earlier point and fetch the value out again when needed. Of course this may not be applicable for your situation.


Good luck.

thanks for this post guys - exactly what I was looking for.

What I've done in the past is create a formula field on the object I'm working with in apex and then I can access it at anytime, plus it is dynamic to sandbox or production servers.  The only time this won't work is if you are using My Domain since it is getting the server name, not your custom domain.


So in the Task scenario where I am creating an email alert about a new task, I have added a field called Server_URL to the Activity table with the following formula:



left($Api.Enterprise_Server_URL_25 , FIND("/", $Api.Enterprise_Server_URL_25, 9))


and then, in either my VF email template or the email object I create in apex, I can reference the to get the URL. 



Hope that helps.




Modified the formula to remove "-api"..


Try this..


Substitute(left($Api.Enterprise_Server_URL_25 , FIND("/", $Api.Enterprise_Server_URL_25, 9)),"-api","")




Prafulla N PatilPrafulla N Patil

Create a custom label named - SFDC_Server_URL with value of your server URL ''  and access it in the apex code as follows -





Creating formula field on every object is the worst option

DevNVDevNV has also released a new URL method that can pull your system URL in Apex.  The only issue I've seen is that depending on your context it could bring in the api or VF URL when you want the user to see their regular link.  But something to investigate that wasn't an option a few years ago.


Custom Setting is a good idea as well but it needs adjusting if you are running code in Sandbox and also when the sandbox is regenerated you have to add the entry again in your custom setting - why don't they copy our custom setting data over like they do Products and other setup data?  Wouldn't that be nice...


You can try this one.  I used in Apex Trigger and it works.


URL.getSalesforceBaseUrl().toExternalForm() + '/' + myAccount.Id;

Mario G.Mario G.
Old question but just to confirm...
Weiping's solution is the right answer!