Passing custom string into a Salesforce email template.

  • I've stumbled upon a problem at the final stage of a new feature I was building (as always)

    I need to pass in a custom string into a salesforce template, however there doesnt seem to be a way of doing this through the standard methods.

    I'm passing in a contact ID, to email the correct user and name to personailse the email. But I need a string to be passed to the template which is completely unrelated to the contact record, and need to be dynamic. So I cant just put it in the template.

    Is there a way? I know i cant pass in a custom object as it needs to be a contact, user or lead id to email a user.

    Can you help?

    Here is the code if you need to see it:

    public static void sendSingleMail(id objId, ID templateId, string fromaddress) {
    
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setReplyTo(fromaddress);
        mail.setTemplateId(templateId);
        mail.setTargetObjectId(objId);
        mail.saveAsActivity = false;
        mail.setReplyTo(fromaddress);
        mail.setSenderDisplayName(fromaddress);
    
        ErrLogger.logger('Email being sent to :');
        ErrLogger.logger('objId found:  ' + objId);
        ErrLogger.logger('templateId found:  ' + templateId);
        ErrLogger.logger('fromaddress found:  ' + fromaddress);
    
        Messaging.sendEmail(new Messaging.SingleEmailmessage[] {mail});
    }
    
  • Tim Smith

    Tim Smith Correct answer

    9 years ago

    I've done something similar in the past, but it requires that you manually do the template merge field lifting. Here is an example of how you could do it below.

    Since you are manually doing the merging in the template, you can put any kind of merge field into your template that you'd like. In this example, I'm pretending there is a custom merge field {!myCustomString} in the email template for your custom string you want to insert.

    I've also assumed below that you have a merge field for first name {!Contact.FirstName} in the subject and in the body of the template. If you have other fields, you would need to include the fields in the contact query and then to the string replace for each of those fields.

    public static void sendSingleMail(Id contactId, Id templateId, String fromAddress, String myCustomString){
    
        // grab the email template
        EmailTemplate emailTemplate = [select Id, Subject, HtmlValue, Body from EmailTemplate where Id =: teamplateId];
    
        // grab the contact fields we need. This assumes we are emailing a contact.
        Contact c = [Select Id, FirstName FROM Contact WHERE Id=: contactId];
    
        // process the merge fields
        String subject = emailTemplate.Subject;
        subject = subject.replace('{!Contact.FirstName}', c.FirstName);
    
        String htmlBody = emailTemplate.HtmlValue;
        htmlBody = htmlBody.replace('{!Contact.FirstName}', c.FirstName);
        htmlBody = htmlBody.replace('{!myCustomString}', myCustomString);
    
        String plainBody = emailTemplate.Body;
        plainBody = plainBody.replace('{!Contact.FirstName}', c.FirstName);
        plainBody = plainBody.replace('{!myCustomString}', myCustomString);
    
            //build the email message
        Messaging.Singleemailmessage email = new Messaging.Singleemailmessage();
    
        email.setReplyTo(fromaddress);
        email.setSenderDisplayName(fromaddress);
        email.setTargetObjectId(objId);
        email.setSaveAsActivity(true);
    
        email.setSubject(subject);
        email.setHtmlBody(htmlBody);
        email.setPlainTextBody(plainBody);
    
        Messaging.sendEmail(new Messaging.SingleEmailmessage[] {email});
    }
    

    Nice, can I just ask if this is the only way to send a *list* of objects in a mail from APEX? i.e. by making the list as an HTML string and inserting it thusly?

License under CC-BY-SA with attribution


Content dated before 7/24/2021 11:53 AM