{"id":19165,"date":"2019-06-19T06:42:35","date_gmt":"2019-06-19T06:42:35","guid":{"rendered":"https:\/\/www.inogic.com\/blog\/?p=19165"},"modified":"2019-06-19T06:42:35","modified_gmt":"2019-06-19T06:42:35","slug":"create-global-email-template-programmatically-in-dynamics-365-crm","status":"publish","type":"post","link":"https:\/\/www.inogic.com\/blog\/2019\/06\/create-global-email-template-programmatically-in-dynamics-365-crm\/","title":{"rendered":"Create Global Email Template Programmatically in Dynamics 365 CRM"},"content":{"rendered":"<h2>Introduction<\/h2>\n<p>In this blog, we will discuss how we can create Global Email Template programmatically.<br \/>\nEmail templates contain prefilled data that you specify, so you don\u2019t have to re-enter the same information for each article.<\/p>\n<p>To create an Email template for a specific entity (for Contact) we use logical name of the Entity <em>\u201ctemplatetypecode=contact\u201d.<\/em><\/p>\n<p style=\"text-align: justify;\">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 <em>\u201ctemplatetypecode = systemuser\u201d<\/em> as shown in the examples below:<\/p>\n<h2>Using XRM.WebAPI<\/h2>\n<p>\/\/This function is used to create a Global Email Template in Dynamics 365 CRM<\/p>\n<p>function createEmailTemplate() {<\/p>\n<p>try {<\/p>\n<p>var objEmailTemp = new Object();<\/p>\n<p>objEmailTemp.title = &#8220;Created Email Template using JS&#8221;; \/\/Title<\/p>\n<p>objEmailTemp.templatetypecode = &#8220;systemuser&#8221;; \/\/Global<\/p>\n<p>\/*We can use the Entity logical name to create a template for a specific entity*\/<\/p>\n<p>objEmailTemp.languagecode = 1033; \/\/Language<\/p>\n<p>objEmailTemp.subjectpresentationxml = &#8220;&lt;template&gt;&lt;text&gt;&lt;![CDATA[Test Subject]]&gt;&lt;\/text&gt;&lt;\/template&gt;&#8221;; \/\/Subject<\/p>\n<p>objEmailTemp.presentationxml = &#8220;&lt;template&gt;&lt;text&gt;&lt;![CDATA[Body of the Template]]&gt;&lt;\/text&gt;&lt;\/template&gt;&#8221;; \/\/Body<\/p>\n<p>objEmailTemp.ispersonal = true;<\/p>\n<p>\/\/ Create account record<\/p>\n<p>Xrm.WebApi.createRecord(&#8220;template&#8221;, objEmailTemp).then(<\/p>\n<p>function success(result) {<\/p>\n<p>\/\/ Perform operations on record creation<\/p>\n<p>showMessage(&#8220;template created with ID: &#8221; + result.id);<\/p>\n<p>},<\/p>\n<p>function (error) {<\/p>\n<p>\/\/ Handle error conditions<\/p>\n<p>showMessage(error.message);<\/p>\n<p>}<\/p>\n<p>);<\/p>\n<p>} catch (error) {<\/p>\n<p>showMessage(error.message);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>\/\/This function is used to show the Message<\/p>\n<p>function showMessage(message) {<\/p>\n<p>try {<\/p>\n<p>var alertStrings = { confirmButtonLabel: &#8220;OK&#8221;, text: message };<\/p>\n<p>var alertOptions = { height: 120, width: 260 };<\/p>\n<p>Xrm.Navigation.openAlertDialog(alertStrings, alertOptions).then(function success(result) {<\/p>\n<p>console.log(success);<\/p>\n<p>}, function (error) {<\/p>\n<p>console.log(error);<\/p>\n<p>});<\/p>\n<p>}<\/p>\n<p>catch (ex) {<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<h2>Using C# code<\/h2>\n<p>\/\/\/ &lt;summary&gt;<\/p>\n<p>\/\/\/ This method is used to create an Email Template in Dynamics 365 CRM of type &#8220;Global&#8221;<\/p>\n<p>\/\/\/ &lt;\/summary&gt;<\/p>\n<p>public void CreateEmailTemplate()<\/p>\n<p>{<\/p>\n<p>try<\/p>\n<p>{<\/p>\n<p>\/\/Entity object<\/p>\n<p>Entity entEmailTemplate = new Entity(&#8220;template&#8221;);<\/p>\n<p>entEmailTemplate[&#8220;title&#8221;] = &#8220;Created Template using C#&#8221;;\/\/title<\/p>\n<p>entEmailTemplate[&#8220;templatetypecode&#8221;] = &#8220;systemuser&#8221;;\/\/Global<\/p>\n<p>entEmailTemplate[&#8220;languagecode&#8221;] = 1033;\/\/Language = English<\/p>\n<p>entEmailTemplate[&#8220;presentationxml&#8221;] = &#8220;&lt;template&gt;&lt;text&gt;&lt;![CDATA[Body of the Template]]&gt;&lt;\/text&gt;&lt;\/template&gt;&#8221;;\/\/Body of the email<\/p>\n<p>entEmailTemplate[&#8220;subjectpresentationxml&#8221;] = &#8220;&lt;template&gt;&lt;text&gt;&lt;![CDATA[Test Subject]]&gt;&lt;\/text&gt;&lt;\/template&gt;&#8221;;\/\/Subject of the email<\/p>\n<p>entEmailTemplate[&#8220;ispersonal&#8221;] = true;\/\/True = Individual and False = Organization<\/p>\n<p>\/\/Create the Record<\/p>\n<p>Service.Create(entEmailTemplate);<\/p>\n<p>}<\/p>\n<p>catch (Exception ex)<\/p>\n<p>{<\/p>\n<p>throw new Exception(ex.Message);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<h2>Conclusion<\/h2>\n<p>Using the above custom code we can create the Global Email Template record in Dynamics 365 CRM.<\/p>\n<blockquote><p><strong>Note<\/strong>: We cannot create Email templates using OOB Workflows or Actions.<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>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\u2019t 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 \u201ctemplatetypecode=contact\u201d. Similarly,\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.inogic.com\/blog\/2019\/06\/create-global-email-template-programmatically-in-dynamics-365-crm\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":13,"featured_media":19167,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[13,16,18,19],"tags":[545],"class_list":["post-19165","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-customizations","category-dynamics-365","category-dynamics-365-v9-2","category-dynamics-crm","tag-dynamics-365-crm"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/19165","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/comments?post=19165"}],"version-history":[{"count":0,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/19165\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media\/19167"}],"wp:attachment":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media?parent=19165"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/categories?post=19165"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/tags?post=19165"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}