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
donobdonob 

Adding a link to HTML body when sending an email alert via apex

I'm trying to send an email that includes a link to a record in Saleforce using apex email alert and the setHtmlBody method.  I'm trying to send the link like so..

 

<a href="'+URL.getSalesforceBaseUrl()+'/'+workOrder.id+'">'+workOrder.Name+'</a>

 The email gets sent out just fine, but the link isn't there.  When I look at the source in the email it shows the a tag without the href attribute like so...

<a>Work Order Name</a>

 Can anybody tell me why this is happening and what I can do to get the link to work?

Best Answer chosen by Admin (Salesforce Developers) 
donobdonob

Thank you digamberlucky for your reply.  I did as you suggested and wrapped the <html></html> tags around the body, unfortunately it didn't solve the problem.  I did however figure out what was wrong.

 

The method  URL.getSalesforceBaseUrl()  showed up as 

<a href="Url:[delegate=https://my.salesforce.com">Sample Work Order</a>

To get it to work I had to do URL.getSalesforceBaseUrl().toExternalForm() which made the link show up properly as href="https://my.salesforce.com"

All Answers

digamber.prasaddigamber.prasad

Hi,

 

The body which you are creating should also be wrapped around <html></html> tag.

 

Something like below:-

 

<HTML><a href="'+URL.getSalesforceBaseUrl()+'/'+workOrder.id+'">'+workOrder.Name+'</a></HTML>

Let me know if you still have any question.

 

Happy to help you!

 

donobdonob

Thank you digamberlucky for your reply.  I did as you suggested and wrapped the <html></html> tags around the body, unfortunately it didn't solve the problem.  I did however figure out what was wrong.

 

The method  URL.getSalesforceBaseUrl()  showed up as 

<a href="Url:[delegate=https://my.salesforce.com">Sample Work Order</a>

To get it to work I had to do URL.getSalesforceBaseUrl().toExternalForm() which made the link show up properly as href="https://my.salesforce.com"

This was selected as the best answer
Prince_sfdcPrince_sfdc
If it is being added to a text field first to make the URL dynamic, it can be directly added to the string and then used like this: 
String body='';
body += '</br> </br>' + 'For more details, please review the opportunity HERE: '+ 
<a href="'+URL.getSalesforceBaseUrl()+'/'+opp.id+'">'+opp.Name+'</a>
body +=  '</body></html>';
mail.setHtmlBody(body);

Where opp is the opportunity's instance.