合并字段在使用模板通过 apex 发送电子邮件时不会显示。

huangapple go评论89阅读模式
英文:

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)。

请参考下面的文档链接:

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_Messaging_SingleEmailMessage_setWhatId.htm

我认为你无法在不自定义编码消息的情况下实现这一点。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

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_Messaging_SingleEmailMessage_setWhatId.htm

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.

huangapple
  • 本文由 发表于 2023年7月5日 00:19:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76614411.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定