You need to sign in to do that
Don't have an account?

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
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
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.
But that doesn't work for managed packages sadly.
You could also use a 'Custom Label' to store the instance. Accessing the value from APEX is pretty simple
Label.OrgURL + '/' + t.Id
Another option would be to pass the domain via a hidden field, see this thread: Getting Salesforce instance type in apex class
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>');