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

Getting Salesforce instance type in apex class

I am sending emails through my Apex class(NOT VisualForce APEX class). The text body of EMail contains the hyperlink to one of my Visualforce page. But depending on the Salesforce account type the instance name in the URL changes.(Example:

In above case cs2 can be na1 or na2 depending on instance type.
Is there any method in APEX by which I can get the name of the SF instance?
This is very simple in SControl but I am not getting anything in Apex fopr the same.

Try taking off the host name on the link. So just use /apex/myTimeSheets/

This works is supported in the UI on buttons and links.
I don't see why it wouldn't work from an email, but the user has to be logged into the correct instance of salesforce.
David VPDavid VP

A relative url won't work in mail. (Your browser knows which webserver the html page with the relative link in it came from - your mailclient however has no idea to which webserver/domain you're referring to if you just give it a /folder/page )

The Apex Email classes can make use of mail templates ... (and some objects have fields like {!Case.Link} etc ... which you can include in mail templates but I gues that doesn't help you much further ...)


Message Edited by David VP on 10-08-2008 09:55 PM

Does anybody've solved that issue?


I solved this issue by placing the page domain into a hidden field and isolating the Salesforce instance from the domain string in the controller.

For example in the visualforce page:


<apex:page controller="SomePageController" id="thePage">

  <script type="text/javascript">
    window.onload = function() 

  <apex:form id="theForm">
    <apex:inputHidden value="{!Domain}" id="domain"/>

 And then in the controller, maybe something like this for example:



// Class properties
public string Domain { get; set; }

private string SfInstance {
  get {
    string result;
    integer firstPos = Domain.indexOf('.');
    integer secondPos = Domain.indexOf('.', firstPos+1);
    if(firstPos != -1 && secondPos != -1)
      result = Domain.substring(firstPos+1, secondPos);
    return result;
} }

 In this case, where the domain is for a managed package with a namespace - i.e.





try this if you are using apex controller and visualforce pages, it wouldn't work from triggers though:
public static string serverURL
  return ApexPages.currentPage().getHeaders().get('Host');



Even easier from sites;


String domain = Site.getDomain();