+ Start a Discussion
Soundar Raj PonpandiSoundar Raj Ponpandi 

Target object id in SendEamil By Using Email Template

Hi,

I am sending an email with Custom Email template. But target object id is not accepted custom object. It's always accepting user, lead and few standard object.

How can i map my custom email template with related object fields.
 
GD_Order__c ordEmail = [select id,name,GD_Account__c,GD_Account_Name_Formula__c, GD_Sales_Rep__c, GD_Sales_Rep_Name_Formula__c,GD_Sales_Manger_Name_Formula__c,GD_Sales_Manager__c,GD_No_of_Ordered_Items__c,GD_Pharmacy_Location__c from GD_Order__c where id =:ordId];
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                EmailTemplate emailTemplate = [Select Id,name,Subject,Description,HtmlValue,DeveloperName,Body from EmailTemplate where name = 'Sales Order Sharing Records Email Template - Dubai'];
                system.debug('emailTemplate' + emailTemplate.name);
                mail.setTemplateID(emailTemplate.Id); 
                //mail.setTargetObjectId(ordEmail.Id);
                mail.setSaveAsActivity(false);

Can anyone resolve my queries.

Regards,
Soundar.
AnudeepAnudeep (Salesforce Developers) 
Hi Soundar, 

This is by design. Only the ID of the contact, lead, or user to which the email will be sent can be specified for setTargetObjectId method

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

Unfortunately you can't use a custom object as the target object for a SingleEmailMessage object. You can only use Use, Contact, Lead or Person objects as the target. If you need to save a record of the email, you can try saving it as a Task record instead and set the WhatId of the Task to the Id of your custom object record

If the above information is helpful, please mark this as solved by selecting this as answer as best so that it can help others in the community. Thank You!

Anudeep
 
{tushar-sharma}{tushar-sharma}
Hi Soundar,

You need to use setTreatTargetObjectAsRecipient(treatAsRecipient) and set treatAsRecipient as false. It will populate values as well.
 
List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage ();
email.setTemplateId(templateId);
email.setSaveAsActivity(false);
email.setToAddresses('raytest1@domain.com');
email.setTargetObjectId(leadId); //having email raytest2@domain.com
email.setWhatId(customObject.Id);
email.setTreatTargetObjectAsRecipient(false) 
emails.add(email);
List<Messaging.SendEmailResult> results = Messaging.sendEmail(emails);

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

If this answer helps you, please mark it as accepted.

Regards,
Tushar Sharma
https://newstechnologystuff.com/
Soundar Raj PonpandiSoundar Raj Ponpandi
Dear Sharma,

Thanks for your quick response.

I have modified my code based on your input. Even i am facing following Error. Can you please advise me how can i resolve this error.
 
User[] recipients = [SELECT email FROM user WHERE id IN :idList];
                user u = [Select id,name from User where id = '0050C0000022v2b'];
                string uId = u.id;
                GD_Order__c ordEmail = [select id,name,GD_Account__c,GD_Account_Name_Formula__c, GD_Sales_Rep__c, GD_Sales_Rep_Name_Formula__c,GD_Sales_Manger_Name_Formula__c,GD_Sales_Manager__c,GD_No_of_Ordered_Items__c,GD_Pharmacy_Location__c from GD_Order__c where id =:ordId];
                EmailTemplate msgTemplate = [Select Id,name,Subject,Description,HtmlValue,DeveloperName,Body from EmailTemplate where name = 'Sales Order Sharing Records Email Template - Dubai'];
 
                List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
                Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage ();
                email.setTemplateId(msgTemplate.Id);
                email.setSaveAsActivity(false);
                email.setToAddresses(mailToAddresses);
                email.setTargetObjectId(uId); //having email raytest2@domain.com
                email.setWhatId(ordEmail.Id);
                email.setTreatTargetObjectAsRecipient(false);
                emails.add(email);
                List<Messaging.SendEmailResult> results = Messaging.sendEmail(emails);



ERROR:
 
USER_DEBUG [180]|DEBUG|System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_ID_FIELD, WhatId is not available for sending emails to UserIds.: [whatId, a0G0C000004Gt2K]

Reagrds,
Soundar.