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
OzymandiasOzymandias 

Getting sObject detail page URL through Apex

I'm trying to create an email whose body is hardcoded in an Apex class and I'm running into some problems trying to get the URL for the details page of a Task object. I intend to include a link to this object in the email, similar to what you can do when you create an Email Template using the web UI. I've experimented with the PageReference class but all I've been able to get was a relative URL...server URL wasn't included.

Has anyone here done something similar? Appreciate it if anyone can point me in the right direction.
Joseph FerraroJoseph Ferraro
perhaps I'm misunderstanding, but can't you just pass the task id to the class to form the URL?
Https://na1.salesforce.com/'+taskid+'
OzymandiasOzymandias
Thanks Joe..However, I'm trying to avoid hardcoding the server URL. In my company, we're using 3 instances, each with a different server URL. If I do it this way, I'd have to tweak the apex code every time I deployed it to another instance. Not that it's a lot of work, just one line of code, but hardcoding it would be a last resort.

I was just wondering if there's some method that could get the full URL since it's pretty straightforward when creating email templates (e.g. {!Account.Link}).
Joseph FerraroJoseph Ferraro
Ah yes...I've had the same issue and asked the question at Tour De Force in NYC.  As of now, I don't believe there's a way to retrieve the server instance, but using na1 will suffice because when the user logs in, Salesforce will redirect them to the appropriate server instance.  Obviously this is not a best practice, but it will work for now.
TehNrdTehNrd
FYI....

http://ideas.salesforce.com/article/show/10091004/Apex_method_to_identify_salesforcecom_server
OzymandiasOzymandias
hmm..didn't know about the redirection thing. I've tested it and works, but not as well as I want. I still have to change it to na1 for production deployment, while use cs1 for sandbox deployment. Anyway, after reading the posts here (and backreading on the other thread that TehNrd mentioned), I guess that means I'll have to make do with hardcoding for now.

Thanks guys.