Default email templates / signatures in CRM 4

I added some code to a customer’s system to set a default email template (signature). This code is avaiable from various places through Google. However, there were some strange inconsitencies when we added it to their live system – in some cases the signature would load, sometimes it wouldn’t.

Eventually I figured out that sometimes Microsoft’s own onLoad code was removing the signature I had set. Basically it was doing a query to find any email body (because the same form / code is used when looking at email history) and when it found none it was overwriting the email body (along with my signature).

In-fact, it was setting the body to something like…

I added a StripHTML function and a 500msec delay to my code (plus a bunch of additional null checks) which seems to have solved the problem.

Here’s the code for the onLoad of the email form. If you want to use it, don’t forget to change the GUID on line 7 for your specific template.

function sig() {

  if (crmForm.FormType != 1) {return;}

  // Pre-fill a template signature
  var drawSignature = false;
  var emailTemplateToLoad = "D802E626-4DB1-E011-81DD-0015C5FF4D30";   // sandbox "BAA8272F-F7AD-E011-93CB-0015C5FF4CDB"

  // Check if description is blank or similar
  var theDescription = crmForm.all.description.DataValue;
  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
    if (!crmForm.all.regardingobjectid) {return;}
    var RegardingItems = crmForm.all.regardingobjectid.DataValue;

    if (RegardingItems) {

      var regardingObjectId = RegardingItems[0].id;
      var regardingObjectType = RegardingItems[0].type;

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

      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") {
          oXml = CreateXmlDocument(false);
          oXml.loadXML(result.ReturnValue);
          o.EmailBody = oXml.selectSingleNode("template/body").text;
          o.EmailSubject = oXml.selectSingleNode("template/subject").text;
          crmForm.all.description.InsertValue(o.EmailBody);
        }
      }

    }

  } // drawSignature == true

}

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

var a = window.setTimeout(sig, 500);
Advertisements
Leave a comment

7 Comments

  1. Patrick

     /  November 14, 2011

    This does not create the template and does not give an error message

    Reply
  2. cyberdem

     /  July 5, 2012

    Thank you, you saved me a day. It is working except subject

    Reply
  3. Mohsinkhan

     /  September 4, 2012

    Thanks it is Working Fine.It is very Helpful.

    Reply
  4. mohsinkhan

     /  December 4, 2012

    Hi Pete,
    Can We add more than one Email Template in this code to Load
    Regards
    Mohsinkhan

    Reply
  5. Johnd847

     /  May 15, 2014

    I am truly thankful to the holder of this website who has shared this fantastic article at here. bdgcecdgeeck

    Reply
  6. Johnf25

     /  May 15, 2014

    Very interesting subject , appreciate it for posting . All human beings should try to learn before they die what they are running from, and to, and why. by James Thurber. aaeaefgdkdkd

    Reply
  7. Massimiliano

     /  October 29, 2014

    Hi this solution not running in crm 2013. Any idea? Please

    Reply

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s

%d bloggers like this: