RSS

How to append Email Template to Email Entity Form on Load through JavaScript

13 Jan

Introduction

Generally, we add a Signature at the bottom of Email before we send. One of my projects needed the same requirement. We need to insert one Signature Email Template into the description body of Email Form on form load. I have found a solution and now I am going to describe it.

Steps

  1. Create a web resource of Script type and add to the Email Form library
  2. Add the “AddSignatureToEmail” function to the form “onload” event
  3. Create a Email Template containing Signature body and copy the Template Id
  4. Replace that Template Id in the following code for variable “emailTemplateToLoad

Code

Following is the Code snippet.

function AddSignatureToEmail() {
	// Check the form type should be 'create' type
	if (Xrm.Page.ui.getFormType() != 1) { return; }

	// Pre-fill a template signature
	var drawSignature = false;
	var emailTemplateToLoad = "b348fc8b-1e6e-e311-9d64-6c3be5a86df8 "; //created email template Id

	// Check if description is blank or similar
	var theDescription = Xrm.Page.getAttribute("description").getValue();
	if (theDescription == null) {
		drawSignature = true;
	} else if (theDescription == undefined) {
		drawSignature = true;
	} else if (stripHTML(theDescription) == "") {
		drawSignature = true;
	} else if (stripHTML(theDescription).length < 10) {
		drawSignature = true;
	}

	if (drawSignature) {
		// Get Regarding object details
		var regardingobjectid = Xrm.Page.getAttribute("regardingobjectid");
		if (regardingobjectid==undefined) { return; }
		var RegardingItems = Xrm.Page.getAttribute("regardingobjectid").getValue();

		// The signature will not append if regarding field remains blank.
		if (RegardingItems!=null) {
			var regardingObjectId = RegardingItems[0].id;
			var regardingObjectType = RegardingItems[0].type;

			if (regardingObjectId == null || regardingObjectId == "") { return; }
			if (regardingObjectType == null || regardingObjectType == "") { return; }

			// Retrieving email template details

			var command = new RemoteCommand("EmailTemplateService", "GetInstantiatedEmailTemplate");
			command.SetParameter("templateId", emailTemplateToLoad);
			command.SetParameter("objectId", regardingObjectId);
			command.SetParameter("objectTypeCode", regardingObjectType);
			var result = command.Execute();

			if (result.Success) {
				var o = new Object();
				o.EmailBody = "";
				o.EmailSubject = "";
				if (typeof (result.ReturnValue) == "string") {
					// Create a Xml Document of the return value to retrieve actual data
					oXml = CreateXmlDocument(result.ReturnValue);
					o.EmailBody = oXml.lastChild.lastElementChild.textContent;

					Xrm.Page.getAttribute("description").setValue(o.EmailBody);
				}
			}
		}	
	}
}

function stripHTML(signature) {
	var tmp = document.createElement("DIV");
	tmp.innerHTML = signature;
	return tmp.textContent || tmp.innerText;
}

function CreateXmlDocument(signatureXmlStr) {
    // Function to create Xml formate of return email template data
	var parseXml;

	if (window.DOMParser) {
		parseXml = function (xmlStr) {
			return (new window.DOMParser()).parseFromString(xmlStr, "text/xml");
		};
	}
	else if (typeof window.ActiveXObject != "undefined" && new window.ActiveXObject("Microsoft.XMLDOM")) {
		parseXml = function (xmlStr) {	
			var xmlDoc = new window.ActiveXObject("Microsoft.XMLDOM");
			xmlDoc.async = "false";
			xmlDoc.loadXML(xmlStr);
			
			return xmlDoc;
		};
	} 
	else {
		parseXml = function () { return null; }
	}

	var xml = parseXml(signatureXmlStr);
	if (xml) {
		return xml;
	}
}

var a = window.setTimeout(AddSignatureToEmail, 1500);

By Sukanta Mangal
Software Engineer @Team DynamicsCRM.
Mindfire Solutions

Advertisements
 
3 Comments

Posted by on January 13, 2014 in WebResources

 

Tags: , , , , , , , , , , , , , , , , , ,

3 responses to “How to append Email Template to Email Entity Form on Load through JavaScript

  1. Flavia Takushi

    April 10, 2014 at 2:13 pm

    Hi Sukanta,

    This article is very interesting and useful! Thank you!
    I want to use this solution to fill pre determined text (maybe email template) into the body. But my concern is if the original text would remain in the body in case of replying an incoming email.
    Would it work?

    Thanks in advance,
    Flavia

     
    • DynamicsCRM@MindfireSolutions

      April 15, 2014 at 6:16 am

      Hello Flavia,

      Thanks for taking interest on our Blog. 🙂

      We have not tested for Reply emails, but we think it will not work for that, because on beginning of new email form open signature will remove all content present in description. So, it only work for new email. But we suggest you to test this and let us know, if you find something interesting.

      Regards,
      DynamicsCRM@MindfireSolutions

       
  2. Flavia Takushi

    April 10, 2014 at 2:21 pm

    Just to complement… I know the functionality I’m asking for is like the native email template.
    But I want to create a custom way, because I need to be able to filter, classify and search for email templates. Native one doesn’t meet my needs as we have a long list, which is difficult to find and use to reply emails.

    Thanks,
    Flavia

     

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: