1

I am getting the below error when the below trigger is triggered. Can anyone tell me how to resolve this error -

Error -

Error: Invalid Data. Review all error messages below to correct your data. Apex trigger EmailService caused an unexpected exception, contact your administrator: EmailService: execution of AfterInsert caused by: System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_ID_FIELD, All targetObjectIds for an email must be of the same type.: []: Trigger.EmailService: line 20, column 1

Code -

trigger EmailService on Task__c (after insert) {
    EmailTemplate et=[Select id from EmailTemplate where name='Multiple Tasks'];
    List<Messaging.MassEmailMessage> mails = new List<Messaging.MassEmailMessage>();

    for(Task__c t : Trigger.new)
    {
        if(t.Project_Approval__c =='Approved' &&t.Status__c == 'Open' ){
            List<Id> lstIds= new List<Id>();
            lstIds.add(t.Task_Executioner__c);
            lstIds.add(t.Task_Executioner_1__c);
            lstIds.add(t.Task_Executioner_2__c);

            Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage();
            mail.setTargetObjectIds(lstIds);
            mail.setSenderDisplayName('Charan Tej');
            mail.setTemplateId(et.id);
            mails.add(mail);
       }
    }
    Messaging.sendEmail(mails);
}
1
  • It sounds like your Task_Executioner fields are referencing multiple different sObjects, and the setTargetObjectIds method will only take Ids from a single sObject.
    – dBeltowski
    Commented May 26, 2015 at 18:59

1 Answer 1

3

From the docs on method setTargetObjectIds of MassEmailMessage:

The objects must be of the same type (all contacts, all leads, or all users).

You are likely trying to add mixed IDs to the collection of IDs that you then pass to this method. Please verify that the fields Task_Executioner__c, Task_Executioner_1__c, and Task_Executioner_2__c are IDs, and that they are all either contacts, users, or leads.

3
  • All the (t.Task_Executioner__c), (t.Task_Executioner_1__c), (t.Task_Executioner_2__c); are 3 different fields of same object Task__c and lookup to contact object.. Commented May 26, 2015 at 19:03
  • They all have to be IDs of the same type of object. If one is a Contact, and the other two are Users, this error will be thrown Commented May 26, 2015 at 19:03
  • All the 3 fields are currently lookup to contact... Commented May 26, 2015 at 19:06

You must log in to answer this question.

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