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
ssousanssousan 

Error sending Email template to user

I have the following Trigger:

 

trigger send_notification on Inquery__c (after update) {

Inquery__c inquery = trigger.new[0];

if (Trigger.isUpdate) {

      if(inquery.Quilification__c == 'Qualified') {

      }
      if(inquery.Quilification__c == 'Disqualified') {
          Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
          EmailTemplate et=[Select id from EmailTemplate where name=:'Ineligible_course_candidate'];
          mail.setTemplateId(et.id);
          mail.setTargetObjectId(inquery.email__c);
          mail.setSenderDisplayName('Salesforce Support');
         Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

      }

   }
}

 

Im getting the following error:

 

Error:Apex trigger send_notification caused an unexpected exception, contact your administrator: send_notification: execution of AfterUpdate caused by: System.QueryException: List has no rows for assignment to SObject: Trigger.send_notification: line 15, column 1

 

for the following line:

 

EmailTemplate et=[Select id from EmailTemplate where name=:'Ineligible_course_candidate'];

 

Anyone know why?

 

Thanks

 

Best Answer chosen by Admin (Salesforce Developers) 
Kiran  KurellaKiran Kurella
setTargetObjectId should be set to the contact or user id to whom you are trying to send an email in this trigger.

Say, if you want to send the email to the record owner then you can replace the following line

mail.setTargetObjectId(record.ID);

with

mail.setTargetObjectId(inquery .OwnerId);

All Answers

Kiran  KurellaKiran Kurella

 

I am assuming you have the email template name Ineligible_course_candidate. If so try replacing the name field with DeveloperName as follows:

 

Replace the following line 

 

          EmailTemplate et=[Select id from EmailTemplate where name=:'Ineligible_course_candidate'];

 

with

 

          EmailTemplate et=[Select id from EmailTemplate where DeveloperName= 'Ineligible_course_candidate'];

ssousanssousan

Yes that did work,

 

But now im getting the following error:

 

Error:Apex trigger send_notification caused an unexpected exception, contact your administrator: send_notification: execution of AfterUpdate caused by: System.EmailException: SendEmail failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Missing targetObjectId with template: []: Trigger.send_notification: line 21, column 1

 

 

It seems i have to include mail.setTargetObjectId,

 

so i changed the code to:

 

trigger send_notification on Inquery__c (after update) {  

public List<Inquery__c > u {get;set;}  

Inquery__c inquery = trigger.new[0];    

if (Trigger.isUpdate) {           

if(inquery.Quilification__c == 'Qualified') {             

}      

 

if(inquery.Quilification__c == 'Disqualified') {          

u = new List<Inquery__c >();          

u  = [select ID from Inquery__c where email__c=:inquery.email__c ];          

Inquery__c record = u[0];          

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();          

EmailTemplate et=[Select id from EmailTemplate where DeveloperName=:'Ineligible_course_candidate'];          

mail.setTemplateId(et.id);          

mail.setTargetObjectId(record.ID);          

mail.setSenderDisplayName('Salesforce Support');         

Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

      }         

}

}

 

But now im getting the following error:

 

Error:Apex trigger send_notification caused an unexpected exception, contact your administrator: send_notification: execution of AfterUpdate caused by: System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_TYPE_FOR_OPERATION, Only Users, Contact or Lead allowed for targetObjectId : a08J0000003Fbbf.: []: Trigger.send_notification: line 23, column 1

 

for the following line:

Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

 

But i dont have the user in my contact or lead,

 

 

What should i do instead?

 

Thanks,

 

 

 

 

 

 

 

 

Kiran  KurellaKiran Kurella
setTargetObjectId should be set to the contact or user id to whom you are trying to send an email in this trigger.

Say, if you want to send the email to the record owner then you can replace the following line

mail.setTargetObjectId(record.ID);

with

mail.setTargetObjectId(inquery .OwnerId);

This was selected as the best answer