You need to sign in to do that
Don't have an account?
Soundar Rajan 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.
Can anyone resolve my queries.
Regards,
Soundar.
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.
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
You need to use setTreatTargetObjectAsRecipient(treatAsRecipient) and set treatAsRecipient as false. It will populate values as well.
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/
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.
ERROR:
Reagrds,
Soundar.