+ Start a Discussion
rohitmaratherohitmarathe 

Getting Salesforce instance type in apex class

Hi,
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: https://cs2.salesforce.com/apex/myTimeSheets).

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.

mikefmikef
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 ...)


David


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

Does anybody've solved that issue?

nelloCnelloC

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() 
      {
        document.getElementById('{!$Component.thePage.theForm.domain}').value=document.domain;
      } 
  </script>

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

 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. namespace.na7.visual.force.com

 

 

Perfectionist52Perfectionist52

 

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

 

Eric_SantiagoEric_Santiago

Even easier from sites;

 

String domain = Site.getDomain();