0

classic email template -

    <p>Hi {!Receiving_User.Name},</p>
    
    <p>You have been tagged in a chat for claim - {!WOD_2__Claim__c.Name}.</p>
    
    <p><a href="{!WOD_2__Claim__c.Link}">Click here</a>to go to claim detail page.</p>
    
    <p>Regards,<br>{!User.Name}<p>

Apex code -


public static void setEmailTemplate(String emailTempBody, String emailSubject, String recordId, List<User> userList) {

    List<Messaging.SingleEmailMessage> emailMessageList = new List<Messaging.SingleEmailMessage>();

    String sobjectName = '';

    String userObjectFields = '';

    List<String> lstrecipientFields = new List<String>();

    Configuration_Setting__mdt fetchAvailableStatus = TWoDMetadataUtility.getConfigurationSettingMetaData('Custom_Mail_Config');

    

    if (fetchAvailableStatus != null && fetchAvailableStatus.WOD_2__isActive__c && !String.IsBlank(fetchAvailableStatus.Configuration_Value__c)) {

        Map<String, Object> configMap = (Map<String, Object>) JSON.deserializeUntyped(fetchAvailableStatus.Configuration_Value__c);

        sobjectName = (String) configMap.get('SobjectName');

        userObjectFields = (String) configMap.get('FieldsApiName');

        lstrecipientFields = ((String) configMap.get('recipientFields')).split(',');

    }

    List<String> fieldsList = userObjectFields.split(',');

    String query = 'SELECT ' + String.join(fieldsList, ',') + ' FROM ' + sobjectName + ' WHERE Id = \'' + recordId + '\'';

    SObject claimObject = Database.query(query);

    if (claimObject != null) {

        for (User u : userList) {

            for (String fieldName : fieldsList) {

                if (emailTempBody.contains(sobjectName + '.' + fieldName)) {

                    emailTempBody = emailTempBody.replaceAll(sobjectName + '.' + fieldName, String.valueOf(claimObject.get(fieldName)));

                }

                if (emailSubject.contains(sobjectName + '.' + fieldName)) {

                    emailSubject = emailSubject.replaceAll(sobjectName + '.' + fieldName, String.valueOf(claimObject.get(fieldName)));

                }

            }

            for (String recipientField : lstrecipientFields) {

                if (emailTempBody.contains('Receiving_User.' + recipientField)) {

                    String fieldValue = String.valueOf(u.get(recipientField));

                    emailTempBody = emailTempBody.replaceAll('Receiving_User.' + recipientField, fieldValue);

                }

            }

            emailTempBody = emailTempBody.replaceAll('User.Name', UserInfo.getFirstName() + ' ' + UserInfo.getLastName());

            emailTempBody = emailTempBody.replace('{!', '').replace('}', '');

            emailSubject = emailSubject.replace('{!', '').replace('}', '');

            System.debug('emailTempBody----->'+emailTempBody);

            System.debug('emailSubject----->'+emailSubject);

            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

            mail.setToAddresses(new String[]{u.Id});

            mail.setSubject(emailSubject);

            mail.setPlainTextBody(emailTempBody);

            emailMessageList.add(mail);

        }

        Messaging.SendEmailResult[] results = Messaging.sendEmail(emailMessageList);

        for (Messaging.SendEmailResult result : results) {

            if (result.success) {

                System.debug('The email was sent successfully.');

            } else {

                System.debug('The email failed to send: ' + result.errors[0].message);

            }

        }

    }

}

Can someone please help out?

1 Answer 1

1

The issue is due to the use of setPlainTextBody in your Apex code, which sends the email as plain text, causing the hyperlink to appear as plain text instead of a clickable link. To ensure the hyperlink is clickable, use setHtmlBody instead.

Update you code as:

...
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(new String[]{u.Id});
mail.setSubject(emailSubject);
mail.setHtmlBody(emailTempBody); // Use setHtmlBody instead of setPlainTextBody

emailMessageList.add(mail);
...
1
  • Hi tushar.i already tried using sethtmlBody() but still its not working getting the plain text as click here Commented Jul 26 at 13:52

You must log in to answer this question.

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