I ran across this article a few weeks ago and want to make a copy of it here just in case it gets removed from it’s source.
Creating Email Templates in Microsoft Dynamics CRM a very simple task, on the surface. You write what the Subject will be, followed by the Body of the message, and just like that you’ve created an email template. However, there are instances when you might want to add some Dynamic values based on the record. Inserting dynamic values from out of the box entities like User, Account, or Contact is pretty straightforward. At the top of the template select Insert/Update:
From there, simply select the Record type, and the field you want to insert. Here’s an example of what using the Contact’s First name would look like within the template:
That’s easy enough, but what if you want to add Dynamic values from a custom entity? According to the Software Development Kit (SDK), it says:
. . .
Well it doesn’t say anything actually, but not to worry, there is a way. In the template, where you want the value to appear, type within 2 brackets an exclamation point 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’d like a default value if nothing was found, after the semi colon add the default value.
{!<entitylogicalname>: <fieldlogicalname>; <Default Text>}
For example, let’s say we have an entity called football team, and we want our template to use the Football Team’s Team Name field, and if it’s empty use Denver Broncos. In order to do this, we would type:
{!new_footballteam : new_teamname; Denver Broncos}
After saving it will look like this:
Single line of text fields are easy enough, works the same for most fields: currency and numbers. Dates will return the Date and Time value. In order to return just Dates add at the end of the fieldlogicalname, /@date. Similarly for just the Time portion, add /@time, for example:
{!new_footballteam:new_firstsuperbowlvictory/@date;}
{!new_footballteam:new_firstsuperbowlvictory/@time;}
Option Set Values will return the numerical value. In order to return the Friendly name add /@name,
{!new_footballteam:new_conference/@name;}
Lookups return the GUID value, to return the Primary Attribute’s Friendly Name, add /@name,
{!new_footballteam:new_currentquarterback/@name;}
These are all of the methods we’ve found so far. We are hoping to find a way to retrieve data from related Entities, but as of now it seems to be nearly impossible.If you have any feedback around tricks with email templates, comment to add your feedback.
Don’t miss PART 2 of this blog, it can be seen here: http://www.crmsoftwareblog.com/2017/01/dynamic-values-email-templates-part-2/
As a result of some great questions that I received on my first blog, Dynamic Values for Custom Entities in Email Templates, I wanted to write a follow up blog highlighting the resolutions to some of those inquires. I’m going to assume you’ve read the previous entry, which is posted above.
Template Types
When creating a new email template, you first get prompted to select an Email Template Type.
If you select a Template Type aside from Global, you must have a record matching that type in either the Regarding field or any of the Send To, CC, or BCC fields. You will also be able to easily insert dynamic values into the template for that record type. If you so choose, you could write out the dynamic values as I had shown, but it’s much easier to use the out of the box method to select them.
With global templates, you can insert the template regardless of what entity record you select:
The Global Template Type is what you’d want to use for custom entities, or any other entity not listed in the template type drop down menu. And just to reiterate, regardless of the way you insert values, whether you use the out of the box insert method or you manually type it in, you can only insert values from one record.
The Inclusion of HTML
There were also some great questions around HTML. For starters, all of these dynamic values can be used, like all templates, with HTML code. For example, if we wanted to bold a dynamic value, we could put the dynamic value between a bold tag, seen here:
After inserting the template, you’ll notice the bold text is now visible:
Furthermore, if you have a field value that is a URL, you can insert this value into the hrefon an anchor tag and create a dynamic hyperlink in your email:
This will be displayed as:
Dynamic URL Pointing to CRM Record
Someone asked about creating a dynamic URL that pointed to an actual record within Dynamics CRM. I was optimistic that something similar to the ‘Record URL (Dynamic)’ that you can find when creating a dialog existed. But unfortunately, this isn’t the case.
However, I did find a work around! In order to open a record with a URL, all you need is the server name (which is just your CRM URL), the entity logical name, and the entity ID. Then using those three components, we can include them within our href on our anchor tag:
{Server Name}/main.aspx?id={Entity Id}&newWindow=true&etn={Entity Name}&pagetype=entityrecord
*Note: You might notice we have a few other options. “newWindow=true” just means we want to open this URL in a new window. “Pagetype=entityrecord”, just means we want to open the entity record.
We should know the Entity Logical Name already. If not, it should be easy to find, and looking at the URL, we can get the Server Name. So the only thing we are missing is the Entity Record ID, but we can get this using our dynamic value. Usually (you may need to check the fields on your entity to confirm this), the ID field is the entity name plus ‘id’. For my football team entity, this field is new_footballteamid. If we let our server have the IP name of 192.168.1.1, we now have all the information we need to create the URL and insert it into the href of our anchor tag.
This will be displayed as:
Well that’s all for now. As always, please don’t hesitate to ask any questions!