CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY : CloseCase: execution of AfterInsert

caused by: System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_TYPE_FOR_OPERATION, Only User, Contact, Lead, or Person objects are allowed for targetObjectId: 00G2v000004mZpT.: [targetObjectId, 00G2v000004mZpTEAU]

Class.CaseCreationNotification.CaseCreationNotificationmtd: line 36, column 1 Trigger.CloseCase: line 23, column 1

public class CaseCreationNotifications


   public static void CaseCreationNotificationmtd(list<case> cases)

        set<id> setids=new set<id>();
        List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
        for(Case record: cases) {
     Map<Id, case> conMap = new Map<Id, case>([SELECT Id,SuppliedEmail,RecordType.DeveloperName FROM case WHERE Id =:setids]);  
     EmailTemplate et=[Select id,Name,DeveloperName from EmailTemplate where DeveloperName ='SSC_Case_Creation_Notification'];
        for(case c:cases){
               case relatedCaseContact = conMap.get(c.id);

            //  Messaging.SingleEmailMessage CaseNotificationmail = new Messaging.SingleEmailMessage();
            //  CaseNotificationmail.setToAddresses(new List<String> {relatedCaseContact.SuppliedEmail});
            //  CaseNotificationmail.setTargetObjectId(relatedCaseContact.id);
            //  CaseNotificationmail.setWhatId(relatedCaseContact.id);
            //  CaseNotificationmail.setReplyTo('[email protected]');
            //  CaseNotificationmail.setSenderDisplayName('Case Creation Notification'); 

            if(c.Origin =='Email' && c.SuppliedEmail != Null){

               Messaging.SingleEmailMessage CaseNotificationmail = new Messaging.SingleEmailMessage();
               CaseNotificationmail.setToAddresses(new List<String> {relatedCaseContact.SuppliedEmail});

          //   CaseNotificationmail.setSubject(' Case Creation Notification '+ c.Subject + c.{!Case.Thread_Id});
          //   CaseNotificationmail.setPlainTextBody('"Dear Team, <br></br> Good day, <br></br> Below request is noted and sent to partner for approval. We will get back to you once we receive reply from partner. In the meantime, if you have any query or need status of application , please use the same email chain in order to keep track of all correspondence"<br> </br>');



  • The error doesn't match the code. 00G is a Group (or in this case, probably a Case Queue). Can you please make sure you're supplying the correct error and/or code?
    – sfdcfox
    May 5, 2020 at 11:55

1 Answer 1


setTargetObjectId The ID of the contact, lead, or user to which the email will be sent. The ID you specify sets the context and ensures that merge fields in the template contain the correct data.

But here CaseNotificationmail.setTargetObjectId(c.Id); you are setting case id to this method. Change it to the user, lead or contact id

Refer the link - https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_email_outbound_single.htm

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .