Showing posts with label Email Template. Show all posts
Showing posts with label Email Template. Show all posts

Monday, September 26, 2016

Email template : Show Date or Time portion only for DateTime attribute

Sometime when sending emails using email templates we do not want to show time portion, e.g. BirthDate.

Contact record

Email Template

Email using template

 To remove time portion from BirthDate in email template, you need to modify template XML.
  1. Create Solution, with email template
  2. Export Solution to local machine
  3. Open Customization.xml file.
  4. Look for email template
  5. In Email template XML, check  presentationxml tag.

        It will look like

  <template><text><![CDATA[Dear&#160;]]></text><slugs><slug><entity>contact</entity><attribute>fullname</attribute></slug><default></default></slugs><text><![CDATA[ <font face="Tahoma, Verdana, Arial" size=2 style="display:inline;"></font><div>Your date of birth in system is&#160;]]></text><slugs><slug><entity>contact</entity>&lt;attribute>birthdate</attribute></slug><default></default></slugs><text><![CDATA[ </div><div><br></div>]]></text></template>

6. This is decoded HTML, you can use online HTML decoders to decode, or look for attribute, e.g. BirthDate

 < template >< text > <![CDATA[Dear ]] >< /text >< slugs >< slug >< entity >contact< /entity >fullname< /attribute >< /slug >< default >< /default>< /slugs >< text >< ![CDATA[?< font face="Tahoma, Verdana, Arial" size=2 style="display:inline;" >< /font >< div >Your date of birth in system is ]] >< /text >< slugs >< slug >< entity >contact< /entity >< attribute >birthdate < /attribute >< /slug >< default >< /default >< /slugs >< text >< ![CDATA[?< /div >< div >< br >< /div >]]>< /text >< /template >

7.Change  < attribute >birthdate < /attribute >  to  < attribute >birthdate/@date < /attribute >

8. Encode template HTML, and put it back into Customization.xml file in presentationxml tag. 

9.  Save customization file, Zip and re-import to CRM.

10. Don't Forgot to publish solution.

Note: It might be when you re-import solution some special character added in template.

Remove special character, save template

Now try now email with same template, and you will see only date portion of BirthDay.

In case want to show only time from datetime field, in email template then change 

7.Change  < attribute >birthdate < /attribute >  to  < attribute >birthdate/@time < /attribute >

Tuesday, February 24, 2015

Add Account/Contact GUID in email template

In CRM Email template, you cannot add record id, but sometime we need to send GUID in email or need to send record URL which will contain record ID/ GUID.

To Add GUID in email template,

1. Create one text attribute in Account or contact entity.. say Account GUID.

2. Add this attribute on form, so that we can able to use it in JavaScript.

3. Add following JavaScript code on form load.

function getRecordId() {
            var accountGuid = Xrm.Page.getAttribute('new_accountguid').getValue()

            if (Xrm.Page.ui.getFormType() == 2 && accountGuid == null) {
                var accountId =;

Here I am getting account id and updating in custom attribute. We can do this only if existing record is opened. 

If want to populate this custom Account GUID attribute on save of account record, then need to create custom plugin and register on Post-Save method. Make sure.. this plugin is not going into Loop by adding some condition like if new_accountguid is null then only execute & update account. 

4. Now open or create email template for account and add this custom attribute in email template

5. Now use this email template for Account.. 

Another option will be by updating Template body XML. but for this need to update template XML using SQL query.

CRM stores templates in Template table. 

to get particular template, 

select Body from Template where Title='Account Reconnect'

Email template body is represented in XML and xml is look like

if email template is like

Notice that account name is selected like

      < xsl:when test="account/name>
        < xsl:value-of select="account/name" / >

      < /xsl:when >

If changed this to

      < xsl:when test="account/accountid>
        <xsl:value-of select="account/accountid" / >

      < /xsl:when >

and updated back template body using SQL statement

Now when you use this email template then you will see GUID in email template.