Set a default email template for new emails on cases.

By Carlton Colter - Last updated: Friday, October 2, 2009 - Save & Share - Leave a Comment

In response to Peter Bergman’s post on the CRM forums, I traversed Microsoft’s code. I determined that the ApplyTemplate loads a specific page, and on that page, the ok button sets the information on the calling page. I melded the ApplyTemplate and ok methods together to create LoadTemplate provided below. With the right parameters, it will set the default template.

function LoadTemplate(templateid, objectid, objecttypecode)
{
    var command = new RemoteCommand("EmailTemplateService", "GetInstantiatedEmailTemplate");
    command.SetParameter("templateId", templateid );
    command.SetParameter("objectId", objectid);
    command.SetParameter("objectTypeCode", objecttypecode);

    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  );
            if( !IsNull( o.EmailSubject ) && o.EmailSubject.length > 0 )
            {
                crmForm.all.subject.DataValue =
                    CrmEncodeDecode.CrmHtmlDecode(o.EmailSubject).substr(0, crmForm.all.subject.MaxLength);
            }
        }
    }
}

To set the default template, you need the templateid, you can get this by opening the template and hitting CTRL-N, or you can just operate in tab mode and see all the id’s and urls at all times. I prefer that view… The below code uses the templateid to set a default email template when a new email is created from a case, account, and contact. You could easily extend this for other templates and entities. Currently I am setting them all to the same template, which won’t work.

var CRM_FORM_TYPE_CREATE = 1;
if (crmForm.FormType==CRM_FORM_TYPE_CREATE)
{
    var otype = 0;
    var tmpid = '';
    switch (crmForm.all.regardingobjectid.DataValue[0].typename)
    {
        case 'incident': otype=112; tmpid='{F8E179BF-8E7C-DA11-BF9A-00142216345E}'; break;
        case 'account':  otype=1;   tmpid='{F8E179BF-8E7C-DA11-BF9A-00142216345E}'; break;
	case 'contact':  otype=2;   tmpid='{F8E179BF-8E7C-DA11-BF9A-00142216345E}'; break;
        default: return;
    }

    var oid = crmForm.all.regardingobjectid.DataValue[0].id;

    if (tmpid==undefined || tmpid==null) return;

    LoadTemplate(tmpid, oid, otype);
}

This could be extended using my business unit lookup or getting roles assigned to a user..

Let me know if you have any questions – or if you figure out a way to get the id of a template from the name of one…

Posted in Scripts • Tags: , , , , , , , , , Top Of Page

Write a comment