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?