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
Andrey BolkonskyAndrey Bolkonsky 

Target Object ID for Apex Messaging class not accepting UserId or OpportunityId for Template email send

Having problem sending email in my Opportunity Trigger Handler Class. The template uses merge fields from the Opportunity but TargetObjectId says it is only accepting Contacts, Leads and Users (this is a really stupid limitation by the way).

I don't want to put the Opportunity ownerId either becasue the email doesn't go to the opportunit owner, it goes to a list of support staff. Here is my code so far. Sorry it's a bit of a mess. Really trying to send as a template and not as a plain text email.

I have also tried to use the renderStoredEmailTemplate method but also with no success.

Please help!
 
if(Opp.StageName == 'Documentation' && stageFrom == 'Submission'){
                
                //Fix this TargetObjectID issue***
                Messaging.reserveSingleEmailCapacity(2);
                Messaging.SingleEmailMessage DocEmail = new Messaging.SingleEmailMessage();
                    
                EmailTemplate et =[SELECT Id, Name FROM EmailTemplate WHERE Name = 'Doc Stage'];
                DocEmail.setTemplateId(et.Id);
                DocEmail.setToAddresses(new String[] {'email1@company.com', 'email2@company.com'});
                DocEmail.setReplyTo('DocEmails@Company.com');
                DocEmail.setSenderDisplayName('Doc Automated Emails');
                DocEmail.setTargetObjectId(Opp.OwnerId);
                DocEmail.setWhatId(Opp.Id);
                DocEmail.setSaveAsActivity(FALSE);
                Messaging.sendEmail(new Messaging.SingleEmailMessage[] {DocEmail});
                
            }

 
mukesh guptamukesh gupta
Hi Andry,

Please follow below code:- because TargetObject for only for Contacts, Leads and Users so need to modify code according below code. means you can't use email template for opportunity object.
 
List<Opportunity> Owners =[Select Owner.Email FROM Opportunity where Id=:opp.id OR Parent_Opportunity__r.Id=:opp.Id];   
  Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
  mail.setToAddresses(Owners);
  mail.setReplyTo('myemail@mail.com');
  mail.setSenderDisplayName('My Name');
  mail.setPlainTextBody('A related Opportunity has been won');
  mail.setHtmlBody('A related Opportunity has been won');
  mail.setBccSender(false);
  mail.setUseSignature(false);
  
   //mail.setHtmlBody('<b> This is HTML body </b>' );
   Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
 }

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh 
Andrey BolkonskyAndrey Bolkonsky
So there is no way to use the Messaging class to send a classic email template from the Opportunity??

How is this possible when you can use process builder to send a classic email template with merge fields pulled from the Opportunity Object?

I would use Process builder but I would rather do this programmatically.

Thanks for your help.