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
Kiran NKiran N 

How to create a hyperlink in the body of an email using apexcode

I have a custom formula field called Way__c in custom object(Abc__c) . On UI this field is displayed as a hyperlink.When u click on this link it takes you to a particular folder in Sharepoint. Now i want to send an email to the user using apex code(without using email template) including this field as hyperlink in the body of my email. so that user can click on the hyperlink which navigates to the folder directly in sharepoint. Here is my code...

I am actually getting the path(folder path to sharepoint) here in apex class in variable called Path. Now i have to make this path a hyperlink so that users can click and directly go to sharepoint.

Can someone please help me with this.

if(emailslist.size()>0){
            
                 for(string s: foldermap.keyset())
                 {
                     
                    Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();

                    message.toAddresses = emailslist;
                    message.subject = 'Alert';
                    message.plainTextBody = 'Dear User\n';
                  
                    message.plaintextbody+= '\n\n'+foldermap.get(s)+'\n\n';
                    message.plainTextBody+= '\nPlease click the following URL which directs you to sharepoint folder and take appropriate action \n';
                    message.plainTextBody+= '\nThanks\n';

Thanks,
Kiran

 
NagendraNagendra (Salesforce Developers) 
Hi Kiran,

You can do something like this:
+ '<br><br><a href="https://www.surveymonkey.co.uk">Take the survey now.</a>' +
P.S. Instead of plain text body you will have to use Htmlbody like this:
message.setHtmlBody(htmlBody);

For reference:  Thanks,
Nagendra
Kiran NKiran N
Hi Nagendra,
Thanks for the response. This is how I'm trying. 
                     String url= site_url+'(\'Shared Documents/'+fpath +'\')';           
                     url = url.replace(' ', '%20');
                     system.debug('url:          '+url);
                      message.setHtmlBody('<a href="url">Click here.</a>'); 
The url here is a dynamic url.

The path that gets stored in url variable should be a hyperlink. Hyperlink is getting displayed in the email but when i click on that i'm gettting this error..............This site can’t be reached
url’s server IP address could not be found.
Try running Windows Network Diagnostics.
DNS_PROBE_FINISHED_NXDOMAIN

Can u please tell me where am I missing.

 
NehaDabas16NehaDabas16
Hi Kiran,

First of all please make sure that your url is correct by looking at it in debug log and try pasting in browser. It should redirect you to the desired page.

Secondly, try below code while setting the HTMLBody, as I can see you are assigning a static string instead of the actual url.
String url= site_url+'(\'Shared Documents/'+fpath +'\')';           
url = url.replace(' ', '%20');
system.debug('url:          '+url);
String htmlBody = '<a href="'+url+'">Click here.</a>';
message.setHtmlBody(htmlBody);

If this helps, please it as resolved.

Regards,
Neha