Get Template Folder Names

If you are working with templates and template folders, you might have run into challenges around displaying folder names for templates in the personal and public folders.

The API does not return folder name for Personal or Public folders in the API. The personal folder is represented by the user id and the public folder is represented by the org id.

Challenges

  • Have you ever needed to get templates out of the salesforce.com API?
  • Having trouble getting the Folder Names displayed from the API?
  • Need to print the Personal & Public Folders?

Features

  • Query templates
  • Print out templates for your organization
  • Decode which templates are in your Personal Folder
  • Decode which templates are in a Public Folder
  • Format the result in an HTML table

Screen Shot Example

Image:FolderExample.jpg

Sample Code

Use the Ajax 9.0 toolkit for the template folders, do a query for templates and retrieve the templates and folder names.

In this example I am only retrieving active templates, ordering by folderID and limiting my result to 30 records

<html>
<head>
<script src="/soap/ajax/9.0/connection.js" type="text/javascript"></script>
<script>
function setupPage() {
  sforce.connection.query("Select Id, Name, Subject, TemplateType, FolderId, Folder.Name From EmailTemplate Where IsActive = true order by FolderId limit 30", layoutResults);
}

The query returns the queryResults and calls the layoutResults function below.

The output variable is populated with table information for formatting.

Start the for loop for parsing through the resultset.

function layoutResults(queryResult) {

if (queryResult.size > 0) {
   var output = "";
       output = "<table><tr><th>ID</th><th>Template Name</th><th>Template Subject</th><th>Template Type</th><th>Folder Name</th></tr>";
   var records = queryResult.getArray('records');
for (var i=0;i<records.length;i++) {
   var Template= records[i];
   var fname ="";

Looping through the resultset, checking if it is a personal, public or regular folder.

  • If the Folder ID is a personal folder add a label "Personal Folder"
  • If the Folder ID is a public folder add a label "Public Folder"
  • Otherwise use the Folder.name as the label
if (Template.FolderId.substr(0,3) == '005') 
   { fname ='Personal Folder'; }
else if (Template.FolderId.substr(0,3) == '00D') 
   { fname ='Public Folder'; }
else
   { fname = Template.Folder.Name; }

  output += "<tr><td>" + Template.Id + " </td><td> " + Template.Name + " </td><td> " + Template.Subject + " </td><td> " + Template.TemplateType + " </td><td> " + fname + "</td></tr>";
} 

  output += "</table>";
       document.getElementById("output").innerHTML = output;
} else {
        document.getElementById("output").innerHTML = "No Folders Found.";
 }
}
</script>
</head>
<body>
<body onload="setupPage()">
   <div id="output"></div>
</body>
</html>