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
akrikosakrikos 

Automatic SOQL query for Email Template?

Is there an apex method for creating a SOQL query based on an email template? Since Salesforce figures the data for all of the objects in a template on the fly, I was hoping that they'd exposed a method that would allow me to generate the same query they would use.

 

Otherwise, solving this problem is non-trivial due to the number of mappings that would be required.

 

Any help is appreciated!

 

 

Pradeep_NavatarPradeep_Navatar

Try out  this SOQL query to get a specific Email template :

 

EmailTemplate templateId = [Select id, from EmailTemplate where name = 'Waitlisted opportunity now open'];

 

you can put more fields of email template if you check in Eclipse IDE.

 

Hope this helps.

akrikosakrikos

Thanks for replying, but the question you answered isn't the question that I asked. My question is more:

 

Given that I already have a template and a starting point, is there an easy way to generate the SOQL query(s) or have Salesforce generate the SOQL query(s) for me to that would give the data that Salesforce would use to merge into that template if I were to send that email.

 

 

sfdcfoxsfdcfox

On the EmailMessage, use the setTargetObjectId (a lead, contact, or user ID) and/or setWhatId (an account, opportunity, etc), as well as setTemplateId to specify a template. When you send the email, the template will be merged with the contact, lead, or user record identified in setTargetObjectId, as well as with the entity specified in setWhatId.