英文:
Merge fields do not appear when sending an email with apex using a template
问题
我正在向一些用户发送模板电子邮件,模板中包含一个合并字段{{!Opportunity.custom__c}}。在发送时,该字段的值不会显示。
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setTemplateId(et.Id); // 电子邮件模板ID
email.setTargetObjectId(user.Id); // 要发送的用户ID
//email.setWhatId(opp.Id); !错误!
email.setSaveAsActivity(false);
emails.add(email);
所以我想知道是否有办法将有关机会的信息传递给模板?
我不应该使用模板吗?
我已经搜索过,他们说要添加email.setWhatId(opportunity.Id),但当我添加它时,它会给我以下错误:INVALID_ID_FIELD,无法将WhatId用于发送电子邮件到UserIds:[whatId,XXXXXXXXXX]。
英文:
I am sending a template email to some users, the template contains a merge field {{!Opportunity.custom__c}}. When sending it, the value of the field does not appear.
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setTemplateId(et.Id); // Email Template ID
email.setTargetObjectId(user.Id); // User ID to send
//email.setWhatId(opp.Id); !Error!
email.setSaveAsActivity(false);
emails.add(email);
So I want to know if there is any way to pass information about the opportunity to the template?
Shouldn't I use a template?
I have searched and they say to add email.setWhatId(opportunity.Id), when adding it it gives me the following error: INVALID_ID_FIELD, WhatId is not available for sending emails to UserIds.: [whatId, XXXXXXXXXXX].
答案1
得分: 0
这个错误是因为方法 notification.setTargetObjectId(usr.Id) 使用了用户 Id,根据文档,只有在 setTargetObjectId 是联系人时才需要使用 notification.setWhatId(updatedOpp.Id)。
请参考下面的文档链接:
我认为你无法在不自定义编码消息的情况下实现这一点。public Void setHtmlBody(String htmlBody) 我建议尝试使用这个方法而不是使用模板。
英文:
This error is because the method notification.setTargetObjectId(usr.Id) uses User Id and as per documentation you need to use notification.setWhatId(updatedOpp.Id) only when setTargetObjectId is contact.
Refer documentation below
I don't think you can achieve this without custom coding the message. public Void setHtmlBody(String htmlBody) I would try and use this instead of the templates.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论