3

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?

2 Answers 2

7

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.

4
  • 1
    I've written a similar script; it's not too bad, but it is awfully obnoxious that there isn't a nice way to build this with a single command. Same for other types of folders that don't support wild cards, like Reports.
    – sfdcfox
    Commented Nov 30, 2021 at 16:00
  • Ya! Mind opening a feature request on the official git repo and we can socialize a bit to get into developer tooling teams backlog. Commented Nov 30, 2021 at 16:02
  • Thank you Monith for the answer. @sfdcfox - can you please share this script? Commented Dec 2, 2021 at 9:50
  • @ItaiShmida I don't have that script, it was for a previous role, but I can write it again this weekend or so. It was just a simple node script that called sfdx to list the metadata, reads the resulting output, then builds the appropriate XML for a second pass. It's really not that bad, maybe like 25 lines of code or so.
    – sfdcfox
    Commented Dec 2, 2021 at 13:04
1

Based on above Mohit answer, below may can help, you just need to call this function from powershell and pass the username of the org

function retrieveEmail ([string] $u) {

$soql_query = "Select DeveloperName from Folder where DeveloperName!=null and (Type='Email' OR Type = 'EmailTemplate')"

$output = sf data query --query $soql_query -o $u --json | ConvertFrom-Json


$pxml = "<?xml version='1.0' encoding='UTF-8'?><Package xmlns='http://soap.sforce.com/2006/04/metadata'><types>"

foreach ($result in $output.result.records) {
    $value = $result.DeveloperName
    $metadata = sfdx force:mdapi:listmetadata -m EmailTemplate --folder $value -u $u --json | ConvertFrom-Json

    foreach ($d in $metadata.result) {
        $metaValue = $d.fullName
        Write-Host($metaValue)

        $pxml += "<members>$metaValue</members>"
    }

}
$pxml += "<name>EmailTemplate</name></types><version>53.0</version></Package>"

Set-Content -Path "PATH To Copy The XML" -Value $pxml

}

Note: This script will respond slow based on number of Email Templates in the Org

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .