Retrieve all email templates with sfdx

email-templatesalesforcedx

I am trying to generate a command or set of command that will retrieve all my email templates.

I am using sfdx and do not want to use ant or package.xml

This post only talks about getting a specific email template, I want a method to get all of them.

Is that that possible?

If not – what is the preferred way on doing that?

Best Answer

Unlike other metadata types EmailTemplate metadata type does not support a wild character.

Also, it will need you to specify the folder name as well to retrieve the metadata.

It will be some manual work unless you script it using Bash or Node.js,

Here is how I would approach manually,

Step 1

Create a SOQL to get all the folder names of Type Email

sfdx force:data:soql:query --query "Select Id, Name, DeveloperName, Type, NamespacePrefix from Folder where DeveloperName!=null and Type='Email'"

Step 2

For every folder in step1, note down the developername and use the below command.

sfdx force:mdapi:listmetadata -m EmailTemplate --folder=<developernameoffolder>

Step 3

From the output JSON look for the field fullName to construct a package.xml like below

<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
 <types>
    <members>unfiled$public/MarketingProductInquiryResponse</members>
    <members>unfiled$public/SalesNewCustomerEmail</members>
    ....
    ....
    <name>EmailTemplate</name>
  </types>
 <version>53.0</version>
 </Package>

Retrieve using package.xml using the mdapi

sfdx force:mdapi:retrieve -r ./mdapipkg -u <username> -k ./package.xml

Alternatively sfdx force:source:retrieve -m "EmailTemplate:<folderdevelopername>:<templatedevelopername>" for n times.