Getting Salesforce Organization Server Url

Tuesday, February 23, 2010 by Aslam - The Alexendra
Hi All,
Recently i faced a situation where i need to get Current Org Server URL (Host Url) in Trigger. I know we can get it easily in VF or Apex Class, but what about in Trigger or some future method? One of my colleague Mr. Yogesh Rankawat suggested me a very good way to do this easily (Thanks to him). So here i am going to tell you this method.

1) Create a simple class with following code:-
public class HostUrl{
public string url {get;set;}
public HostUrl(){
url = ApexPages.currentPage().getHeaders().get('Host');

2) Create a even simple VF page with following code:-
<apex:page sidebar="false" showHeader="false" controller="HostUrl" contentType="text">{!url}</apex:page>

3) Now just simply use below code to get the Server URL

PageReference pg = Page.HostUrl;
String SERVER_URL = pg.getContent().toString();

Isn't it too simple!!! If you know even better way, then please share.

Aslam Bari
Posted in | 2 Comments »


Manish said...

getContent() method is not supported in the Trigger. Please Suggest another code.


Aslam - The Alexendra said...

@Manish: Unfortunately getContent() is not supported in trigger so far by salesforce.
You can try Custom Setting for fetching url in trigger.

Post a Comment