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

Target object ID error while sending email through Trigger for a Custom Object

Hi All,

trigger SendNotification on Savings_Risk_Checklist__c (After insert,after update) {

   Savings_Risk_Checklist__c SCS =[0]; 
  if (Trigger.IsInsert || Trigger.IsUpdate) { 
    if(SCS.Savings_Checklist_Form_Status__c =='Proposal Submitted'|| SCS.Savings_Checklist_Form_Status__c=='Final From Submitted') {
       Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
          EmailTemplate et=[Select id from EmailTemplate where Name=:'Savings Risk Check List Proposal Submittion'];
          mail.setToAddresses(new List<String>{''});
          Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
i have Written above code on Custom Object to send an email. but i am Getting Below Error

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger SendNotification caused an unexpected exception, contact your administrator: SendNotification: execution of AfterUpdate 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: 01I37000000kq3E.: [targetObjectId, 01I37000000kq3EEAQ]: Trigger.SendNotification: line 13, column 1

Sukanya BanekarSukanya Banekar
Hi Satya,

You can not set custom object record id in setTargetObjectId method of Messaging.SingleEmailMessage. This id can be user , lead or contact so in your case you can set:
Also, You can write workflow or Process builder to send email.

Let me know if this helps you to solve your problem.

Sukanya Banekar
Amol Salve 14Amol Salve 14

Hello Satya,

you need to set ownerId insted of Id to set TargetObject ID




Thank yo
Amol Salve
Salesforce Developer 
Hi Amol,

trigger SendNotification on Savings_Risk_Checklist__c (After insert,after update) {

  Savings_Risk_Checklist__c SCS =[0]; 
 if (Trigger.isAfter && (Trigger.IsInsert || Trigger.IsUpdate)) { 
   if(SCS.Savings_Checklist_Form_Status__c =='Proposal Submitted'|| SCS.Savings_Checklist_Form_Status__c=='Final From Submitted') {
      Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
         EmailTemplate et=[Select id from EmailTemplate where Name=:'Savings Risk Check List Proposal Submittion'];
         mail.setToAddresses(new List<String>{'});
         Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});

The above trigger is working fine. but it's sending the email to Record owner also but i need only for the Receipients. also one more is i have a detail page link in my template. but in email i'm not receiving any link through email.

Could you please check.

Mustafa JhabuawalaMustafa Jhabuawala

To avoid sending email to record owner, you can use setTreatTargetObjectAsRecipient(false)

- Optional. If set to true, the targetObjectId (a contact, lead, or user) is the recipient of the email. If set to false, the targetObjectId is supplied as the WhoId field for template rendering but isn’t a recipient of the email. The default is true.

Reference Link (

Mustafa Jhabuawala
Technical Lead at Zen4orce (