How can I retrieve email templates from a sandbox using the Salesforce CLI?
The EmailTemplate metadata type does not support wildcard retrieval[0] - in package.xml or using the CLI. You need to specify each individual EmailTemplate you want to retrieve. E.g. sfdx force:source:retrieve -m EmailTemplate
won't work, but sfdx force:source:retrieve -m EmailTemplate:folder/MyEmailTemplate
should .
[0] https://developer.salesforce.com/docs/atlas.en-us.api_meta.meta/api_meta/meta_emailtemplate.htm#!
I ran into this situation few days ago, so I created a bash script, which will first query all email folders and then creates package.xml and retrieves all EmailTemplates using that package.xml.
This is assuming you are on a unix box and jq is installed.
1) Prepare a file named DownloadAllEmailTemplates.sh with below content:
rm -f temp.txt
rm -f package_temp.xml
echo "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>\n" >> package_temp.xml
echo "<Package xmlns=\"http://soap.sforce.com/2006/04/metadata\">" >> package_temp.xml
sfdx force:mdapi:listmetadata --metadatatype=EmailFolder --targetusername=$1 --json | jq '.result[].fullName' > temp.txt
input="temp.txt"
echo " <types>" >> package_temp.xml
while IFS= read -r line
do
folderName="${line//\"/}"
echo "$folderName"
rm -f temp_1.txt
sfdx force:mdapi:listmetadata --metadatatype=EmailTemplate --folder=$folderName --targetusername=$1 --json | jq '.result[].fullName' >> temp_1.txt
while IFS= read -r line1
do
templateName="${line1//\"/}"
echo " <members>$templateName</members>" >> package_temp.xml
done < "temp_1.txt"
rm -f temp_1.txt
done < "$input"
echo " <name>EmailTemplate</name>" >> package_temp.xml
echo " </types>" >> package_temp.xml
rm -f temp.txt
sfdx force:source:retrieve --targetusername=$1 --manifest=package_temp.xml
If you have a org alias as production, you can run the below command to retrieve all email templates from that org.
sh DownloadAllEmailTemplates.sh production