Email should go through the apex class

26 Views Asked by At
@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});
    
    }

This is the error message i am getting

15:30:12:165 FATAL_ERROR System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_ID_FIELD, WhatId is not available for sending emails to UserIds.: [whatId, a0LDF00000AXvAK]

Please help me to handle this issue

This is my classic email template

Email Template

Subject POS item : {!POS_Item__c.Item_Name__c} ({!POS_Item__c.Item_No__c}) has reached Low Inventory Level Plain Text PreviewHi ,This is to inform you that POS Item : {!POS_Item__c.Item_Name__c} ({!POS_Item__c.Item_No__c}) now has {!POS_Item__c.Available_Stock__c} packs in stock.Please take necessary action.Emerge System Administrator

I tried removing the whaid, values are not getting binded . I need to send an email for the record creator and for the cc address list . Content should go through my template

0

There are 0 best solutions below