0

I am trying to include Case Number of an email template for an email-to-case. Basically, when a client sent an email to a closed case, he received an automatic email reply. I would like the automatic email reply to show the case number. On the template I have added the merge field value {!Case.CaseNumber}

enter image description here

but the Case Number does not appear on the email reply sent to the client

enter image description here

I believe that, to add the Case Number in the automatic email reply, I need to reference the CaseId somewhere in my code. But I haven't figured out how. I tried to do :

mail.setTargetObjectId(c.Id);

I Hoped to get the case number but this does not work Here is my full code: Apex Class:

public class EmailManager {
@future
public static void sendEmailToCaseDeactivated(Set<Id> caseIds){

    List<Messaging.SingleEmailMessage> allmsg = new List<Messaging.SingleEmailMessage>();
    List<Case> lstCase = [SELECT Id, Status,ContactEmail FROM Case WHERE 
                          Status = 'Closed' AND
                          ContactEmail != Null AND
                          Id IN: caseIds];
    EmailTemplate templateId = [SELECT Id FROM EmailTemplate WHERE DeveloperName =:'Email_to_Case_Closed'];
   
    for(Case c : lstCase) {
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setTargetObjectId(UserInfo.getUserId());
        mail.setTemplateId(templateId.Id);
        String[] sendingTo = new String[]{c.ContactEmail}; 
        mail.setToAddresses(sendingTo); 
        mail.setSaveAsActivity(false);
        allmsg.add(mail);
    }               
    Messaging.sendEmail(allmsg, false);
}

}

Apex Trigger:

trigger IncomingEmailClosedCase on EmailMessage (before insert,after insert) {

if(trigger.isBefore) { 
    Set<Id> sCaseIds = new Set<Id>();
    for(EmailMessage em: Trigger.New) {
        if (em.Incoming)
             sCaseIds.add(em.parentId);   
    }
    if(!sCaseIds.isEmpty())
        EmailManager.sendEmailToCaseDeactivated(sCaseIds);
}
if(trigger.isAfter){
    Map<Id,Id> caseIdsToEmailMessageIds = new Map<Id,Id>();
    for(EmailMessage em: Trigger.New) {
        if (em.Incoming)
            caseIdsToEmailMessageIds.put(em.parentId,em.Id);   
    }
    set<Id> sMessageIdsToDelete = new Set<Id>();
    if(caseIdsToEmailMessageIds.isEmpty()) return;
    for(Case lstCase : [SELECT Id, Status,ContactEmail FROM Case WHERE 
                      Status = 'Closed' AND
                      ContactEmail != Null AND
                      Id IN: caseIdsToEmailMessageIds.keyset()]){
        if(caseIdsToEmailMessageIds.containsKey(lstCase.Id)){
            Id emId = caseIdsToEmailMessageIds.get(lstCase.Id);
            sMessageIdsToDelete.add(emId);
        } 
    } 
    if(!sMessageIdsToDelete.isEmpty())   delete [ select Id from EmailMessage where id in :sMessageIdsToDelete];
}

}
0

The merge fields in an email template that reference the relatedTo object (in VF email template terms) must set the whatId

setWhatId(whatId)

If you specify a contact for the targetObjectId field, you can specify an optional whatId as well. This helps to further ensure that merge fields in the template contain the correct data.

and, per the doc, you should setTargetObjectId(c.ContactId) instead of to the running user.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.