How to Retrieve Public Email Templates by FolderName?

Question
I have been trying to retrieve all the public email templates by querying for templates within the ‘Public Email Templates’ folder. Here’s the query I attempted:
SELECT Id, Name, Subject, Folder.Name FROM EmailTemplate WHERE Folder.Name = 'Public Email Templates'
However, this query does not seem to work. When querying an email template, I can see that the Folder.Name
indeed displays as ‘Public Email Templates’, but the Folder.DeveloperName
field comes back as blank. I am unable to filter the public email templates based on the FolderName
or Folder.Name
fields using SOQL. How can I resolve this issue?
Answer
The issue you’re encountering comes from a misunderstanding of how Salesforce treats the “Public Email Templates” folder. The label ‘Public Email Templates’ that you see in the UI is a user-friendly name for a folder, but it does not exist in the Salesforce database as a queryable field.
Instead, Salesforce uses the organization ID to represent the ‘Public’ folder. The Folder.Name
field that displays ‘Public Email Templates’ in the UI is not a part of the underlying database structure for querying purposes. This is why querying Folder.Name
for ‘Public Email Templates’ does not yield the expected results.
To resolve this issue, you need to query based on the FolderId
, which directly references the folder in Salesforce. For public email templates, the FolderId
will be equal to the organization’s ID. You can retrieve the organization’s ID dynamically using UserInfo.getOrganizationId()
. Here’s how you can modify your query:
String orgId = UserInfo.getOrganizationId();
List<EmailTemplate> emailTemplates = [SELECT Id, Name, Subject FROM EmailTemplate WHERE FolderId = :orgId];
In this query, the FolderId
is set to the organization ID, which corresponds to the ‘Public’ folder in Salesforce. This will retrieve all email templates within the public folder.
If you’re specifically looking for Lightning email templates (which are typically associated with Salesforce Lightning Experience), you can further filter the results by adding the UiType
condition:
String orgId = UserInfo.getOrganizationId();
List<EmailTemplate> emailTemplates = [SELECT Id, Name, Subject FROM EmailTemplate WHERE FolderId = :orgId AND UiType = 'SFX'];
In this query, the UiType = 'SFX'
condition ensures that only Lightning email templates are retrieved from the public folder.
Explanation:
The FolderName in the UI (e.g., ‘Public Email Templates’) is just a label and doesn’t exist as an actual database field. Salesforce uses the organization ID to represent the ‘Public’ folder.
The FolderId field in the EmailTemplate
object stores the ID of the folder, which can be used to query for email templates in a specific folder. For public email templates, this ID corresponds to the organization ID.
The UiType field differentiates between standard and Lightning email templates, and you can use it to filter results to get only Lightning email templates.
Thus, to retrieve public email templates, you need to query by the FolderId
(using the organization ID) and, if necessary, filter by UiType
to target Lightning email templates specifically.
Job-Oriented Salesforce Course with Real-Time Projects and Money Back Guarantee
Our Salesforce course is meticulously designed to equip you with a thorough understanding of the Salesforce platform, helping you develop the essential skills needed for success in the CRM industry. The program covers vital modules such as Salesforce Admin, Developer, and AI, combining theoretical knowledge with hands-on experience. Through engaging in real-world projects and assignments, you will build the expertise necessary to solve business challenges using Salesforce solutions. Our seasoned instructors ensure you gain the technical skills and practical insights to excel within the Salesforce ecosystem.
Along with building technical proficiency, our Salesforce Training in Nagpur offers personalized mentorship, exam preparation, and interview coaching to enhance your career prospects. You’ll have access to extensive study resources, practical exposure, and unwavering support throughout your learning journey. By the end of the course, you’ll be fully prepared for certification exams and equipped with the problem-solving abilities that employers value. Start your Salesforce career today and open the door to endless opportunities. Sign up for a free demo now!