function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
@ M  Coder@ M Coder 

targetObjectId issues in Template - Need Urgent FIX

I am getting below error if i pass the template id as anyone of  below 
   mail.setTargetObjectId(Userinfo.getUserId());
   mail.setTargetObjectId(cs.owner.Id); 
   mail.setTargetObjectId(cs.ContactId);
except if i pass any dummy contact id  i am getting emails .
 mail.setTargetObjectId('0037f00001ZAvc9');

Error : 
Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Test_Case: execution of AfterInsert caused by: System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_ID_FIELD, WhatId is not available for sending emails to UserIds.:

code:
Trigger Test_Case on Chh__c (after insert) {



//newList contains case records which are inserted from above missin code

// piece of code starts here in 
 if(newList.size()>0)
    {   
        
        Map<String,Id> templateList  = new Map<String,Id>();
        for(EmailTemplate e : [Select Id,Name from EmailTemplate where Name LIKE 'Test Case%'])
        {
            templateList.put(e.Name,e.Id);
        }
    
    List<Messaging.SingleEmailMessage> masterListMails =  new List<Messaging.SingleEmailMessage>();
    for(Case cs :[select id,Owner.Id,ContactId,Account.Place__c,Account.Ac_Owner__r.email,subject from case where id in: newList AND subject ='Test Case' ]) 
       {
        if((cs.Account.Place__c == 'Banglore') && ((cs.Account.AC_Owner__r.email !=null && cs.Account.AC_Owner__r.email !='')))
        {
            system.debug('if Called====');
            list<String> sendTo = new list<String>();
           if(cs.Account.AC_Owner__r.email !=null )
            sendTo.add(cs.Account.AC_Owner__r.email);
        
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            mail.setToAddresses(sendTo);
        /*  mail.setTargetObjectId(Userinfo.getUserId());
            mail.setTargetObjectId(cs.owner.Id); 
            mail.setTargetObjectId(cs.ContactId);*/
            mail.setTargetObjectId('0037e00001ZAxc9');
            mail.setWhatId(cs.Id); 
            mail.setTreatTargetObjectAsRecipient(false);
            mail.setSaveAsActivity(false);
            mail.setTemplateId(templateList.get('Test Case')); 
            masterListMails.add(mail);
        }
       }    
       // Sends mass emails
        if(masterListMails.size()>0)
       Messaging.sendEmail(masterListMails);
     } 
}
SwethaSwetha (Salesforce Developers) 
Recommend reviewing similar posts from past: https://salesforce.stackexchange.com/questions/81499/whatid-is-not-available-for-sending-emails-to-userids
https://salesforce.stackexchange.com/questions/22397/invalid-id-field-whatid-is-not-available-for-sending-emails-to-userids


Thanks