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
NarutoNaruto 

Add Hyperlink To Body Of Email In Apex Code without URL

I want to provide hyperlink in the apex email body without displaying the url and html tag
message.subject = 'some subject';
emailBody += '<html><a href="'+URL.getSalesforceBaseUrl().toExternalForm()+'/'+someid+'">'+displayname+'</a></html>';

from the above code, I am able to get an email with hyperlink but the hyperlink is not only applied to the 'displayname'. The email body even has the html tag and the complete URL in the hyperlink.
How to provide the hyperlink only to displayname and hide the URL
Priyananth RPriyananth R
Hi,

Please follow this link, it will help you,
https://developer.salesforce.com/forums/?id=906F000000094F2IAI

Thanks,
NarutoNaruto
@Priyananth R the link https://developer.salesforce.com/forums/?id=906F000000094F2IAI didnt help me it still displaying the html tag and the URL.
Priyananth RPriyananth R
Hi Naruto,

Just try below sample code,
List<Messaging.SingleEmailMessage> mails =  new List<Messaging.SingleEmailMessage>();
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
List<String> sendTo = new List<String>();
sendTo.add('abcd@gmail.com');
mail.setToAddresses(sendTo);
mail.setSubject('some subject');
String someid = 'dynamic Id';
String displayname = 'Display Name' ;
String emailBody = '<html><a href="'+URL.getSalesforceBaseUrl().toExternalForm()+'/'+someid+'">'+displayname+'</a></html>';
mail.setHtmlBody(emailBody);
mails.add(mail);
Messaging.sendEmail(mails);
Thanks,