1

I am sending out an email from my code after a certain action, and I am trying to add the record link to it. But in the email that is being sent out, the link of the record page is not appearing or working and I am not sure why.

I am adding the code for the email template below. Can someone please look into this and let me know what am I doing wrong. Thanks in advance.

strMailText = '';
            strMailText += '<div>Hi, <br/> <br/> The IO file synced successfully from SF to OB for the SF Order ID: </b>' + sfOrderId + '<br/>' +
                                            '<br/> The URL to the Order Record is as below <br/>'+ 
                                            '<a href= '+ URL.getSalesforceBaseUrl()+'/'+sfOrderId+'>Order Record Page</a><br/><br/> Thank you';
            strMailText += '</body></html>';

            Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
            message.toAddresses = new String[] { '[email protected]' };
            message.optOutPolicy = 'FILTER';
            message.subject = 'IO File Synced Successfully from SF to OB for Order Number ' + sfOrderId;
            //message.plainTextBody = strMailText;
            message.setHtmlBody(strMailText);
            Messaging.SingleEmailMessage[] messages =   new List<Messaging.SingleEmailMessage> {message};
            Messaging.SendEmailResult[] results = Messaging.sendEmail(messages);

enter image description here

Html code in browser inspection window

enter image description here

Updated Code based on inputs of sfdcfox

strMailText = '';
            strMailText += '<div>Hi, <br/> <br/> The IO file synced successfully from SF to OB for the SF Order ID: </b>' + sfOrderId + '<br/>' +
                                            '<br/> The URL to the Order Record is as below <br/>'+ 
                                            '<a href="'+ URL.getSalesforceBaseUrl().toExternalForm()+'/'+sfOrderId+'">Order Record Page</a><br/><br/> Thank you';
            strMailText += '</body></html>';

            Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
            message.toAddresses = new String[] { '[email protected]' };
            message.optOutPolicy = 'FILTER';
            message.subject = 'IO File Synced Successfully from SF to OB for Order Number ' + sfOrderId;
            //message.plainTextBody = strMailText;
            message.setHtmlBody(strMailText);
            Messaging.SingleEmailMessage[] messages =   new List<Messaging.SingleEmailMessage> {message};
            Messaging.SendEmailResult[] results = Messaging.sendEmail(messages);
2
  • can you add a screenshot of what are you receiving? Sep 1, 2021 at 15:07
  • @OleksandrBerehovskyi I have added a screenshot as you suggested. Sep 1, 2021 at 15:13

1 Answer 1

3

Your code is outputting something like:

<a href= https://my.salesforce.com/12345>Order Order Page</a>

Which is not proper HTML, so it gets rendered incorrectly.

It should look like:

<a href="https://my.salesforce.com/12345">Order Order Page</a>

Your href attribute needs to be quoted, and not have a space after the equals sign.

You also need to convert the URL from a URL to a String with toExternalForm().

The following Apex should work:

'<a href="'+ URL.getSalesforceBaseUrl().toExternalForm()+'/'+sfOrderId+'">Order Record Page</a><br/><br/> Thank you';
5
  • Thank you so much for the answer, I did change my code to the way you mentioned but I am still not able to see it as the link. In the browser inspection window, I am able to see it inside the anchor tag but the href attribute is missing. Can this be happening because I am sending this email from the sandbox instance? I have added the screenshot of the same too. Sep 1, 2021 at 15:28
  • @AkshayVasu I just realized you didn't convert to a String, so it gets rendered incorrectly. See my edit. I tested the new version in my org, it works fine.
    – sfdcfox
    Sep 1, 2021 at 15:39
  • I updated the code as you had mentioned and have added it above in the question too, but I am still not able to see the href attribute in the anchor tag. The email is arriving in the spam folder as it's coming from the sandbox, I am moving it to the primary inbox after marking it as safe. I am not sure if this issue is happening because of that. Sep 1, 2021 at 16:02
  • @AkshayVasu That's a matter of the email being flagged as spam, for sure. You can't really fix it on your end, other than to go through the entire process of setting up DKIM and SPF records for the sandbox, or using an Email Relay.
    – sfdcfox
    Sep 1, 2021 at 16:25
  • Got it! Thank you for the help. Sep 1, 2021 at 16:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .