电子邮件应经过顶点类

问题描述 投票:0回答:1
@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 triggers notifications apex
1个回答
0
投票

设置

email.setTargetObjectId(target);
时,不能将
User
设置为
whatId
的记录。仅当
WhatId
是联系人时才能设置
setTargetObjectId

所以应该是这样

mail.setTargetObjectId(ContactId);
© www.soinside.com 2019 - 2024. All rights reserved.