我有一个电子邮件模板,我想通过apex类发送。我收到了电子邮件通知但是合并字段的值没有显示。我尝试了很多解决方案但没有得到结果。请问,有人可以帮助我吗?
这是我的电子邮件模板的快照 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');
}}
任何帮助都会非常值得注意。 谢谢!
答案 0 :(得分:1)
请参阅SingleEmail方法的要求:
具体参考以下内容:
setTargetObjectId(targetObjectId)
如果使用模板,则为必需,否则为可选。电子邮件将发送到的联系人,潜在客户或用户的ID。您指定的ID设置上下文并确保模板中的合并字段包含正确的数据。
setWhatId(whatId)
如果为targetObjectId字段指定联系人,则还可以指定可选的whatId。这有助于进一步确保模板中的合并字段包含正确的数据。
我在代码中没有看到这些方法中的任何一种。