web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :

Send Emails using Templates for Custom Entities in C#

Spring Wave 2016 Profile Picture Spring Wave 2016 325

In this article, I will explain how we can use global email template for custom entities using C# code.

Note: You can download full code of this article from here.

In CRM, using an email template with OOB entities is straight forward. But, what if you have such requirement to use an email template with a custom entity? Well, you can do this. Question is how? Here is detailed description to achieve this.

Steps:

  • Create a global email template.

Click “New” and select “Template Type” as “Global”

EmailTemplate_1

  • In the template, put the cursor where you want the value to appear, type within 2 curly braces an exclamation symbol followed by the entity logical name. After the entity name, add a colon, and then the field logical name, ending it with a semi colon. If you need a default value in case of no value found from dynamic field, after the semi colon add the default value.

{!<EntityLogicalName>: <FieldLogicalName>; <Default Text or Value>}

Note: Do not use SchemaName for entity and field name, instead use logical name which is usually in small letters in CRM.

Fill the template body as follows

EmailTemplate_2.png

  • Let’s suppose for each field type we need to show text from respective field. Here is how we can add each dynamic field with default text.

Simple Text field

Document Name: {!dot_document:dot_name;Project Proposal}

OptionSet field

Request Type: {!dot_document:crm_requesttype/@name;Establishment Card}

Lookup field

Customer: {!dot_document:dot_Customer/@name;Aslam Restaurant}

Datetime field

Issuance Date: {!dot_document:dot_issuancedate/@date;12-12-2018}

  • As soon as you save the template after filling the body with your default fields, the background color of each dynamic field should be changed to yellow color. If some dynamic field does not get changed to yellow background then there is spell mistake in your body text inside curly braces. Here is the final look of email template body with custom entity fields

EmailTemplate_3

  • Here is C# code to use in your plugin or any of your C# code-based application.
public void SendEmailByTemplate()
        {
            Guid fromUserId = new Guid("A8EDC0AC-3CDF-E311-9407-005056BD2B49"); //-- This is admin user id
            Guid toContactId = new Guid("1039BCB9-97E9-E311-940D-005056BD2B49");
            Entity emailRecord = GetEmailObject(fromUserId, toContactId);

            string templateTitle = "Test Template - Global";
            EmailTemplate emailTemplate = GetEmailTemplate(templateTitle);

            Guid documentId = new Guid("69410885-8457-E811-80DC-005056BE7607");
            string documentLogicalName = "dot_document";

            SendEmailFromTemplateRequest emailUsingTemplateReq = new SendEmailFromTemplateRequest
            {
                // The Email Object created
                Target = emailRecord,

                // The Email Template Id
                TemplateId = emailTemplate.EmailTemplateId,

                // Template Regarding Record Id
                RegardingId = documentId,

                //Template Regarding Record’s Logical Name
                RegardingType = documentLogicalName
            };
            SendEmailFromTemplateResponse emailUsingTemplateResp = (SendEmailFromTemplateResponse)crmService.Execute(emailUsingTemplateReq);
            Guid _emailId = emailUsingTemplateResp.Id;
        }

There is another way to create email templates for custom entities using following solution

https://github.com/rtebar/dynamics-custom-emails

References:

https://www.crmsoftwareblog.com/2015/11/dynamic-values-custom-entities-email-templates/

https://www.inogic.com/blog/2017/06/tip-using-sendemailfromtemplaterequest-to-directly-send-an-email-using-the-email-template/

https://crmbook.powerobjects.com/basics/email-templates-and-mail-merge/

https://msdn.microsoft.com/en-us/library/gg334461.aspx

https://www.crmsoftwareblog.com/2015/11/dynamic-values-custom-entities-email-templates/

https://www.inogic.com/blog/2017/06/tip-using-sendemailfromtemplaterequest-to-directly-send-an-email-using-the-email-template/


This was originally posted here.

Comments

*This post is locked for comments