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
guenthmnguenthmn 

How to query current salesforce instance in apex code na1, na2, ...

I like to include a link to my salesforce record in an outbound  Messaging.SingleEmailMessage email, but I don't want to include a hard code instance as outlined in the documentation. How do I get the salesforce instance na1, na2, na3,... that I am working on. Pagereference getURL() only returns a partial URL.

 

From the documentation:

 

 http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content%2Fapex_classes_email_outbound.htm|SkinName=webhelp

 

mail.setHtmlBody('Your case:<b> ' + case.Id +' </b>has been created<p>'+
' View case <a href=https://na1.salesforce.com/'+case.Id+'>click here</a>');

 

Thanks,

 

Matt

Best Answer chosen by Admin (Salesforce Developers) 
Surfwriter.comSurfwriter.com

Here's how you can query your current Salesforce.com instance from Apex code:

 

String s = System.URL.getSalesforceBaseUrl().getHost();

system.debug(s);

 

Example - email link with absolute URL from Apex Code:

 

mail.setHtmlBody('Your case:<b> ' + case.Id +' </b>has been created<p>'+
     ' View case <a href=https://' + System.URL.getSalesforceBaseUrl().getHost() + '/'+case.Id+'>click here</a>');

 

 

All Answers

CaffeineCaffeine

Matthias,

This is a little hacky, but it's the only way I know to do it.

 

1. Create a custom object called Properties, with 2 custom fields (name, value)

2. Insert a record in each environment with the following values 'Instance', 'na1' (or na2, or cs3, etc).

3. Query the the Properties table for Instance and substitute this into your code.

 

Hope this helps. 

cpetersoncpeterson

But that doesn't work for managed packages sadly.

airrick3airrick3

You could also use a 'Custom Label' to store the instance.  Accessing the value from APEX is pretty simple

 

 

Label.OrgURL + '/' + t.Id

 

 

 

nelloCnelloC

Another option would be to pass the domain via a hidden field, see this thread: Getting Salesforce instance type in apex class

Surfwriter.comSurfwriter.com

Here's how you can query your current Salesforce.com instance from Apex code:

 

String s = System.URL.getSalesforceBaseUrl().getHost();

system.debug(s);

 

Example - email link with absolute URL from Apex Code:

 

mail.setHtmlBody('Your case:<b> ' + case.Id +' </b>has been created<p>'+
     ' View case <a href=https://' + System.URL.getSalesforceBaseUrl().getHost() + '/'+case.Id+'>click here</a>');

 

 

This was selected as the best answer