I am writing trigger to send email to task owner after task got created.In the mail i was sending recordId as a plain text.But i was trying is there any way to make the id as a link to the related record in salesforce.

the mail should be like

  Hello {task.OwnerId},

    A new {task.Id} was allocated for you.


this was a custom html mail here i am not using any html template.So i was wondering is there any way to do it in custom way like in standard html template we use {task.Link}

  • possible duplicate of Detail page link under email template – cropredy Jul 2 '15 at 18:56
  • While it may be a duplicate of something, it doesn't appear to be a duplicate of the linked question. – sfdcfox Jul 2 '15 at 19:38
  • @crop1645 i was asking how to get this in apex but the link given is about email templates.. i might be wrong , thanks – rajasekhar Jul 3 '15 at 5:45
  • sorry - i thought you were asking about templates - metadaddy below has the apex answer – cropredy Jul 4 '15 at 3:57

If you want a link in a text email template, you can use https://login.salesforce.com/{task.Id} (or https://test.salesforce.com/{task.Id} in a sandbox) - if the user is already logged in, this will go straight to the Task; otherwise, it will take them to the Task via the login page. It also has the benefit of avoiding a direct reference to the instance (e.g. na15.salesforce.com), which can cause problems in the long term.

If you're generating the email text from Apex, you can use URL.getSalesforceBaseUrl().toExternalForm() + '/' + task.Id to achieve the same end.

  • Won't this break for sandboxes though? – Adrian Larson Jul 2 '15 at 21:06
  • Added the sandbox login link. I can't think of a generic way to do this that will work across both. – metadaddy Jul 2 '15 at 21:08
  • 1
    Since it's apex you could just get it programmatically with URL.getSalesforceBaseUrl().toExternalForm(). – Adrian Larson Jul 2 '15 at 21:11
  • Question seemed to be about templates, but I added a note for Apex. Thanks! – metadaddy Jul 2 '15 at 21:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.