Create Global Email Template Programmatically in Dynamics 365 CRM

By | June 19, 2019

Introduction

In this blog, we will discuss how we can create Global Email Template programmatically.
Email templates contain prefilled data that you specify, so you don’t have to re-enter the same information for each article.

To create an Email template for a specific entity (for Contact) we use logical name of the Entity “templatetypecode=contact”.

Similarly, to create a Global Email template we do not have any specific entity. Therefore, In order to create a record of the Global Email template, Microsoft has provided a solution, i.e. set the field value of the “templatetypecode = systemuser” as shown in the examples below:

Using XRM.WebAPI

//This function is used to create a Global Email Template in Dynamics 365 CRM

function createEmailTemplate() {

try {

var objEmailTemp = new Object();

objEmailTemp.title = “Created Email Template using JS”; //Title

objEmailTemp.templatetypecode = “systemuser”; //Global

/*We can use the Entity logical name to create a template for a specific entity*/

objEmailTemp.languagecode = 1033; //Language

objEmailTemp.subjectpresentationxml = “<template><text><![CDATA[Test Subject]]></text></template>”; //Subject

objEmailTemp.presentationxml = “<template><text><![CDATA[Body of the Template]]></text></template>”; //Body

objEmailTemp.ispersonal = true;

// Create account record

Xrm.WebApi.createRecord(“template”, objEmailTemp).then(

function success(result) {

// Perform operations on record creation

showMessage(“template created with ID: ” + result.id);

},

function (error) {

// Handle error conditions

showMessage(error.message);

}

);

} catch (error) {

showMessage(error.message);

}

}

//This function is used to show the Message

function showMessage(message) {

try {

var alertStrings = { confirmButtonLabel: “OK”, text: message };

var alertOptions = { height: 120, width: 260 };

Xrm.Navigation.openAlertDialog(alertStrings, alertOptions).then(function success(result) {

console.log(success);

}, function (error) {

console.log(error);

});

}

catch (ex) {

}

}

Using C# code

/// <summary>

/// This method is used to create an Email Template in Dynamics 365 CRM of type “Global”

/// </summary>

public void CreateEmailTemplate()

{

try

{

//Entity object

Entity entEmailTemplate = new Entity(“template”);

entEmailTemplate[“title”] = “Created Template using C#”;//title

entEmailTemplate[“templatetypecode”] = “systemuser”;//Global

entEmailTemplate[“languagecode”] = 1033;//Language = English

entEmailTemplate[“presentationxml”] = “<template><text><![CDATA[Body of the Template]]></text></template>”;//Body of the email

entEmailTemplate[“subjectpresentationxml”] = “<template><text><![CDATA[Test Subject]]></text></template>”;//Subject of the email

entEmailTemplate[“ispersonal”] = true;//True = Individual and False = Organization

//Create the Record

Service.Create(entEmailTemplate);

}

catch (Exception ex)

{

throw new Exception(ex.Message);

}

}

Conclusion

Using the above custom code we can create the Global Email Template record in Dynamics 365 CRM.

Note: We cannot create Email templates using OOB Workflows or Actions.

Leave a Reply

Your email address will not be published. Required fields are marked *