<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>MSCRM Blogger &#187; email</title>
	<atom:link href="http://mscrmblogger.com/tag/email/feed/" rel="self" type="application/rss+xml" />
	<link>http://mscrmblogger.com</link>
	<description>Achieving it all with Microsoft Dynamics CRM™</description>
	<lastBuildDate>Thu, 10 May 2012 20:24:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Set a default email template for new emails on cases.</title>
		<link>http://mscrmblogger.com/2009/10/02/set-a-default-email-template-for-new-emails-on-cases/</link>
		<comments>http://mscrmblogger.com/2009/10/02/set-a-default-email-template-for-new-emails-on-cases/#comments</comments>
		<pubDate>Fri, 02 Oct 2009 22:31:58 +0000</pubDate>
		<dc:creator>Carlton Colter</dc:creator>
				<category><![CDATA[Extensions]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[Web Resources]]></category>
		<category><![CDATA[crm]]></category>
		<category><![CDATA[crm 4]]></category>
		<category><![CDATA[crm4]]></category>
		<category><![CDATA[email]]></category>
		<category><![CDATA[forms]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jscript]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[microsoft dynamics CRM 4]]></category>
		<category><![CDATA[onload event]]></category>

		<guid isPermaLink="false">http://mscrmblogger.com/?p=152</guid>
		<description><![CDATA[<p>In response to <a href="http://social.microsoft.com/Forums/en-US/crmdevelopment/thread/2d2c28ba-891d-4ebc-9c54-92c7cc21a034">Peter Bergman's post on the CRM forums</a>, 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.</p>]]></description>
			<content:encoded><![CDATA[<p>In response to <a href="http://social.microsoft.com/Forums/en-US/crmdevelopment/thread/2d2c28ba-891d-4ebc-9c54-92c7cc21a034">Peter Bergman&#8217;s post on the CRM forums</a>, I traversed Microsoft&#8217;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.</p>
<pre name="code" class="javascript">
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 ) &#038;&#038; o.EmailSubject.length > 0 )
            {
                crmForm.all.subject.DataValue =
                    CrmEncodeDecode.CrmHtmlDecode(o.EmailSubject).substr(0, crmForm.all.subject.MaxLength);
            }
        }
    }
}
</pre>
<p>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&#8217;s and urls at all times. I prefer that view&#8230;  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&#8217;t work.</p>
<pre name="code" class="javascript">
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);
}
</pre>
<p>This could be extended using my <a href="http://mscrmblogger.com/2009/09/30/get-current-users-business-unit-for-hiding-fields/">business unit</a> lookup or getting roles assigned to a user..</p>
<p>Let me know if you have any questions &#8211; or if you figure out a way to get the id of a template from the name of one&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://mscrmblogger.com/2009/10/02/set-a-default-email-template-for-new-emails-on-cases/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

