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);
          o.EmailBody = oXml.selectSingleNode("template/body").text;
          o.EmailSubject = oXml.selectSingleNode("template/subject").text;


  } // drawSignature == true


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

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

Send E-Mail from custom entities in CRM 4

A load of customers are still running CRM 4 and still manage to come up with problems I can’t just find solutions to with Google!

This time it was automatically populating the To: field of a new email when initiated from a custom entity. In this case, the custom entity has a related Contact but would work just as well with an associated Account or User. Unfortunately you can’t just have it send directly to a email address without some fancy plugin footwork – it has to be a CRM record.

I have a plugin which attaches any files from notes to the email when it gets sent so I suppose this could be extended to support plain-text email addresses. Maybe one day I’ll get around to documenting it here – trying to keep the blog focussed on CRM 2011 as much as possible.

Yet again, this requires Daniel Cai’s excellent CRM Web Service Toolkit.

var regarding = crmForm.all.regardingobjectid.DataValue;
if (regarding) {
   if (regarding[0].type == 10038) {

    var venueId = regarding[0].id;

    var fetchXml = [
    "<fetch mapping='logical' count='1'>",
      "<entity name='msa_eventvenue'>",
        "<attribute name='new_contactid' />",
          "<condition attribute='msa_eventvenueid' operator='eq' value='" + venueId + "' />",

    var fetchData = CrmServiceToolkit.Fetch(fetchXml);
    if (fetchData.length > 0) {

      var lookupObject = new Object; = fetchData[0].getValue("new_contactid"); = fetchData[0].getValue("new_contactid","name");
      lookupObject.typename = "contact";

      var lookupObjectArray = new Array();
      lookupObjectArray[0] = lookupObject; = lookupObjectArray;