@future(callout=true)
public static void sendEmailForLowLevelInventoryReached(Id recordId)
{
POS_Item__c posItemDetails = [SELECT Id,Item_Name__c,CreatedById,Low_Inventory_Level__c FROM POS_Item__c WHERE Id=:recordId LIMIT 1];
//get the email template details
EmailTemplate emailTemplate = [SELECT Id, Body, Subject FROM EmailTemplate
WHERE Name = 'Low inventory level'];
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
//set the template id
mail.setTemplateId(emailTemplate.Id);
//Optional. The default value is true, meaning the email is saved as an activity.
//This argument only applies if the recipient list is based on targetObjectId or targetObjectIds.
// If HTML email tracking is enabled for the organization, you will be able to track open rates.
mail.setSaveAsActivity(false);
//User id of your record
mail.setTargetObjectId(posItemDetails.CreatedById);
mail.setCcAddresses(new list<string>{'[email protected]','[email protected]'});
//Enter your record Id whose merge field you want to add in template
mail.setWhatId(posItemDetails.id);
//send email
Messaging.SendEmailResult[] resultMail = Messaging.sendEmail(new Messaging.SingleEmailMessage[]{mail});
}
这是我收到的错误消息
15:30:12:165 FATAL_ERROR System.EmailException:发送电子邮件失败。第 0 行的第一个异常;第一个错误:INVALID_ID_FIELD,WhatId 无法用于向 UserIds 发送电子邮件。:[whatId,a0LDF00000AXvAK]
请帮我解决这个问题
这是我的经典电子邮件模板
电子邮件模板
主题 POS 商品:{!POS_Item__c.Item_Name__c} ({!POS_Item__c.Item_No__c}) 已达到低库存水平纯文本预览嗨,这是通知您 POS 商品:{!POS_Item__c.Item_Name__c} ({!POS_Item__c.Item_No__c} ) 现在有 {!POS_Item__c.Available_Stock__c} 包库存。请采取必要的措施。Emerge 系统管理员
我尝试删除 whid,值没有被绑定。我需要向记录创建者和抄送地址列表发送电子邮件。内容应该通过我的模板
设置
email.setTargetObjectId(target);
时,不能将User
设置为whatId
的记录。仅当 WhatId
是联系人时才能设置 setTargetObjectId
。
所以应该是这样
mail.setTargetObjectId(ContactId);