0

I am sending email to single email address, when users are deactivated through apex. Below is my send email method

public void sendMail(List<user> Users, string emailTemplateName, string address) {
    final string FROM_ADDRESS = 'Test';
    EmailTemplate emailTempRec = getEmailTemplate(emailTemplateName);
    List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
    String[] toAddresses = new String[] {'[email protected]'}; 
    Messaging.SingleEmailMessage email;
    for(User userRec : Users){
        email = new Messaging.SingleEmailMessage(); 
        if(emailTemplateName == DISABLE_NOTIFICATION_WARNING) {
            email.setTargetObjectId(userRec.Id); 
        }else{
            email.setTargetObjectId(userRec.Id);
            email.setTreatTargetObjectAsRecipient(false);
            email.setToAddresses(toAddresses);  
        }
        email.setSenderDisplayName(FROM_ADDRESS); 
        email.setUseSignature(false); 
        email.setBccSender(false); 
        email.setSaveAsActivity(false); 
        email.setTemplateId(emailTempRec.Id);
        emails.add(email);
    }
    Messaging.sendEmail(emails);  
}

Template

Hello,

 <p>The following individual's account was disabled due to inactivity on {!Today}<br> 

{!User.Name}<br>

{!User.FederationIdentifier}</p>

The email has to be sent to toaddress and email should have the information of user that is being deactivated rather the it takes information from sending user. i tried using {!target.user.name} but with no result(it could be because email is sent through apex not through workflow).Any other way i can achieve this?

2
  • Have you tried updating your template to use the receiving user fields? e.g. {!Receiving_User.Name}
    – Nick C
    Jun 7, 2021 at 3:45
  • Yes. it displays nothing when i use {!Receiving_User.Name} or {!target.user.name}.
    – Shira
    Jun 7, 2021 at 4:05

1 Answer 1

0

Was able to resolve the issue with html.replace.

    String htmlBody;
    String subject = emailTempRec.Subject;
    Messaging.SingleEmailMessage email;
    for(User userRec : Users){
        email = new Messaging.SingleEmailMessage(); 
        htmlBody = emailTempRec.HtmlValue;
        if(emailTemplateName == DISABLE_NOTIFICATION_WARNING) {
            email.setTargetObjectId(userRec.Id); 
        }else{
            email.setTargetObjectId(userRec.Id);
            email.setTreatTargetObjectAsRecipient(false);
            email.setToAddresses(toAddresses);  
            htmlBody = htmlBody.replace('{!Today}', todaysdate);
            htmlBody = htmlBody.replace('{!User.Name}', userRec.name);
            htmlBody = htmlBody.replace('{!User.FederationIdentifier}', userRec.FederationIdentifier);
            email.setHtmlBody(htmlBody);
            email.setSubject(SUBJECT_TO_ADMINS);
        }
            email.setSenderDisplayName(FROM_ADDRESS); 
            email.setUseSignature(false); 
            email.setBccSender(false); 
            email.setSaveAsActivity(false); 
            email.setTemplateId(emailTempRec.Id);
            emails.add(email);
        
    }

You must log in to answer this question.

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