由顶点发送时,合并字段值未显示在电子邮件模板中

Merge field value is not showing in email template when it send by apex

我有一个电子邮件模板,我想通过 apex class.I 收到电子邮件通知,但合并字段的值不是 showing.I 尝试了很多解决方案但没有得到 result.Please , 谁能帮帮我?

这是我的电子邮件模板的截图 Email template

和顶点代码:-

global class sampleBatch implements Database.Batchable<sObject>{
global Database.QueryLocator start(Database.BatchableContext bc) {
    String query = 'Select id,Name from book__c;
    return Database.getQueryLocator(query);
}

global void execute(Database.BatchableContext bc, List<book__c> books){ 
    Map<Id, List<Case>> associatedCases = CaseHelperClass.getCase(new Map<Id,book__c>(books).keySet());
    Map<Id,book__c> bookMap= new Map<Id,book__c>(books);
    EmailTemplate emailTemplate = [SELECT Id,Subject,Description,HtmlValue,DeveloperName,Body FROM EmailTemplate WHERE DeveloperName =: 'sameple_template'];
    if(associatedCases <> null){
        for(Id bookId : associatedCases.keySet()){
            String headerAndAssociatedCases = 'CaseNumber,Type,Status,Subject,Description,CreatedDate\n';
            for(Case c : associatedCases.get(bookId)){
                headerAndAssociatedCases += c.CaseNumber+','+c.Type+','+c.Status+','+c.Subject+','+c.Description+','+c.CreatedDate+'\n';
            }
            Messaging.EmailFileAttachment csvAttachment = new Messaging.EmailFileAttachment();
            blob csvBlob = Blob.valueOf(headerAndAssociatedCases);
            csvAttachment.setFileName('Case.csv');
            csvAttachment.setBody(csvBlob);
            Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
            list<string> toAddresses = new list<string>{name@test.com};
            email.setSubject(emailTemplate.Subject);
            email.setTemplateId(emailTemplate.Id);
            email.setPlainTextBody(emailTemplate.Body);
            email.setToAddresses(toAddresses);
            email.setFileAttachments(new Messaging.EmailFileAttachment[]{csvAttachment});
            Messaging.SendEmailResult [] result = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
       }
    }
}

global void finish(Database.BatchableContext bc){
    system.debug('Apex Job Done');
}}

任何帮助都将不胜感激。 谢谢!

请参考SingleEmail方法的要求:

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_email_outbound_single.htm

具体参考以下:

setTargetObjectId(targetObjectId)

如果使用模板则为必需,否则为可选。将向其发送电子邮件的联系人、潜在客户或用户的 ID。您指定的 ID 设置上下文并确保模板中的合并字段包含正确的数据。

setWhatId(whatId)

如果您为 targetObjectId 字段指定一个联系人,您也可以指定一个可选的 whatId。这有助于进一步确保模板中的合并字段包含正确的数据。

我在你的代码中没有看到这两种方法。