<?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; onload event</title>
	<atom:link href="http://mscrmblogger.com/tag/onload-event/feed/" rel="self" type="application/rss+xml" />
	<link>http://mscrmblogger.com</link>
	<description>Achieving it all with Microsoft Dynamics CRM™</description>
	<lastBuildDate>Fri, 09 Jul 2010 14:57:43 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>CRM Common Javascript File</title>
		<link>http://mscrmblogger.com/2009/10/13/crm-common-javascript-file/</link>
		<comments>http://mscrmblogger.com/2009/10/13/crm-common-javascript-file/#comments</comments>
		<pubDate>Tue, 13 Oct 2009 21:10:46 +0000</pubDate>
		<dc:creator>Carlton Colter</dc:creator>
				<category><![CDATA[Scripts]]></category>
		<category><![CDATA[crm]]></category>
		<category><![CDATA[crm 4]]></category>
		<category><![CDATA[crm4]]></category>
		<category><![CDATA[jscript]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[microsoft dynamics CRM 4]]></category>
		<category><![CDATA[onchange]]></category>
		<category><![CDATA[onload event]]></category>

		<guid isPermaLink="false">http://mscrmblogger.com/?p=186</guid>
		<description><![CDATA[Need to reference an external javascript file to make functions available in change events and in an onload?  Here's how!]]></description>
			<content:encoded><![CDATA[<p>I use a lot of the same function on multiple pages, and I think a lot of you do to.  You can create a common javascript file and load it during an onload event, extending the functionality of your CRM.  This is particularly useful because it reduces the amount of changes you have to make when you want to change how something works.</p>
<p>Code reuse is important, and I believe in creating libraries and using libraries. Javascript files are libraries, and they are cached.  So if you update a Javascript file you are loading this way, make sure your users clear their cache.</p>
<pre name="code" class="javascript">
function loadjs(filename, onload){
  var scriptfile=document.createElement('script');
  scriptfile.setAttribute("type","text/javascript");
  scriptfile.setAttribute("src", filename);
  scriptfile.onreadystatechange=onload;
  document.getElementsByTagName("head")[0].appendChild(scriptfile);
}

loadjs("/MoreJavascript/tools.js", function(){
	if (this.readyState == 'loaded' || this.readyState == 'complete') {
		FunctionInJavascriptFile();
		this.onreadystatechanged=null;
	}
});
</pre>
]]></content:encoded>
			<wfw:commentRss>http://mscrmblogger.com/2009/10/13/crm-common-javascript-file/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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[Scripts]]></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>
		<item>
		<title>Get Current User&#8217;s Business Unit for Hiding Fields</title>
		<link>http://mscrmblogger.com/2009/09/30/get-current-users-business-unit-for-hiding-fields/</link>
		<comments>http://mscrmblogger.com/2009/09/30/get-current-users-business-unit-for-hiding-fields/#comments</comments>
		<pubDate>Wed, 30 Sep 2009 14:34:15 +0000</pubDate>
		<dc:creator>Carlton Colter</dc:creator>
				<category><![CDATA[Scripts]]></category>
		<category><![CDATA[businessunit]]></category>
		<category><![CDATA[crm]]></category>
		<category><![CDATA[crm 4]]></category>
		<category><![CDATA[crm4]]></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>
		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://mscrmblogger.com/?p=136</guid>
		<description><![CDATA[In my <a href="http://mscrmblogger.com/2009/09/30/hide-fields-or-tabs-by-role-with-javascript/">previous post about hiding fields and tabs using roles</a>, I outlined how use roles to hide fields, etc.  Here is a modification of the method to hide sections by business unit.]]></description>
			<content:encoded><![CDATA[<p>In my <a href="http://mscrmblogger.com/2009/09/30/hide-fields-or-tabs-by-role-with-javascript/">previous post about hiding fields and tabs using roles</a>, I outlined how use roles to hide fields, etc.  Here is a modification of the method to hide sections by business unit.</p>
<p><b>The syntax is UserHasBusinessUnit(['BU1','BU2','BU3']);</b></p>
<pre name="code" class="javascript">
function UserHasBusinessUnit(businessUnits)
{
  var mybu = GetMyBusinessUnit();

  for (j = 0; j &lt; businessUnits.length; j++)
  {
	// If there is a match, return true, found
	if (mybu == businessUnits[j]) return true;
  }  

  //otherwise return false
  return false;
}
function GetMyBusinessUnit() {
    var xml = &quot;&quot; +
	&quot;&lt;?xml version='1.0' encoding='utf-8'?&gt;&quot; +
	&quot;&lt;soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'&quot; +
	&quot; xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'&quot; +
	&quot; xmlns:xsd='http://www.w3.org/2001/XMLSchema'&gt;&quot; +
	GenerateAuthenticationHeader() +
	&quot;&lt;soap:Body&gt;&quot; +
	&quot;&lt;Fetch xmlns='http://schemas.microsoft.com/crm/2007/WebServices'&gt;&quot; +
	&quot;&lt;fetchXml&gt;&quot; +
	&quot; &amp;lt;fetch mapping='logical' count='1'&amp;gt;&quot; +
	&quot; &amp;lt;entity name='businessunit'&amp;gt;&quot; +
	&quot; &amp;lt;attribute name='name' /&amp;gt;&quot; +
	&quot; &amp;lt;filter&amp;gt;&quot; +
	&quot; &amp;lt;condition attribute='businessunitid' operator='eq-businessid' /&amp;gt;&quot; +
	&quot; &amp;lt;/filter&amp;gt;&quot; +
	&quot; &amp;lt;/entity&amp;gt;&quot; +
	&quot; &amp;lt;/fetch&amp;gt;&quot; +
	&quot;&lt;/fetchXml&gt;&quot; +
	&quot;&lt;/Fetch&gt;&quot; +
	&quot;&lt;/soap:Body&gt;&quot; +
	&quot;&lt;/soap:Envelope&gt;&quot;;

    var xmlHttpRequest = new ActiveXObject(&quot;Msxml2.XMLHTTP&quot;);
    xmlHttpRequest.Open(&quot;POST&quot;, &quot;/mscrmservices/2007/CrmService.asmx&quot;, false);
    xmlHttpRequest.setRequestHeader(&quot;SOAPAction&quot;, &quot;http://schemas.microsoft.com/crm/2007/WebServices/Fetch&quot;);
    xmlHttpRequest.setRequestHeader(&quot;Content-Type&quot;, &quot;text/xml; charset=utf-8&quot;);
    xmlHttpRequest.setRequestHeader(&quot;Content-Length&quot;, xml.length);
    xmlHttpRequest.send(xml);

    var resultXml = xmlHttpRequest.responseXML;

	var resultSet = resultXml.text;
    resultSet.replace('&amp;lt;', '&lt; ');
    resultSet.replace('&amp;gt;', '&gt;');

    var oXmlDoc = new ActiveXObject(&quot;Microsoft.XMLDOM&quot;);
    oXmlDoc.async = false;
    oXmlDoc.loadXML(resultSet);

	var result = oXmlDoc.getElementsByTagName('name');  

	return result[0].text;
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://mscrmblogger.com/2009/09/30/get-current-users-business-unit-for-hiding-fields/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Hide Fields or Tabs by Role with Javascript</title>
		<link>http://mscrmblogger.com/2009/09/30/hide-fields-or-tabs-by-role-with-javascript/</link>
		<comments>http://mscrmblogger.com/2009/09/30/hide-fields-or-tabs-by-role-with-javascript/#comments</comments>
		<pubDate>Wed, 30 Sep 2009 13:13:32 +0000</pubDate>
		<dc:creator>Carlton Colter</dc:creator>
				<category><![CDATA[Scripts]]></category>
		<category><![CDATA[crm]]></category>
		<category><![CDATA[crm4]]></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>
		<category><![CDATA[roles]]></category>
		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://mscrmblogger.com/?p=131</guid>
		<description><![CDATA[Do you need to hide fields or tabs based on security roles.  Here is an easy way to add the functionality to your CRM, use my code example.]]></description>
			<content:encoded><![CDATA[<p>Here are a set of methods to assist in hiding tabs or sections based on role, it is a modified version of <a href="http://jianwang.blogspot.com/2008/01/crm-40-check-current-users-security.html" target="_blank">Jimmy Wang&#8217;s version</a>.</p>
<p>First, we need a set of functions to facilitate our our process.  We need to get the roles of the current user, <b><i>GetCurrentUserRoles</i></b>.  Then we need to see if the user has the role, <b><i>UserHasRole</i></b>.  Finally we can hide the fields (<b><i>HideFieldByRole</i></b>) or tabs (<b><i>HideTabByRole</i></b>).</p>
<p>
<b>GetCurrentUserRoles</b><br />
The first thing we need to do is get a list of roles according to the current user.  There are a couple of different ways to do this.  You could use the RemoteCommand to get the current user and then their roles, a blog post by <a href="http://www.crowehorwath.com/cs/blogs/crm/archive/2008/05/08/hide-show-fields-in-crm-4-0-based-on-security-role.aspx" target="_blank">Zahara Hirani on Hide Show Fields in CRM 4 based on security role</a> outlines this.  However, I do not want to make multiple request for the same information.  I want to get the roles for the current user in one line.  Below shows the GetCurrentUserRoles that implements a RetrieveMultiple query to get the names of the roles that a user has in one request.</p>
<pre name="code" class="javascript">
function GetCurrentUserRoles()
{
  var xml = &quot;&quot; +
    &quot;&lt;?xml version=\&quot;1.0\&quot; encoding=\&quot;utf-8\&quot;?&gt;&quot; +
    &quot;&lt;soap:Envelope xmlns:soap=\&quot;&quot; +
    &quot;http://schemas.xmlsoap.org/soap/envelope/&quot; +
    &quot;\&quot; xmlns:xsi=\&quot;http://www.w3.org/2001/XMLSchema-instance\&quot;&quot; +
    &quot; xmlns:xsd=\&quot;http://www.w3.org/2001/XMLSchema\&quot;&gt;&quot; +
    GenerateAuthenticationHeader() +
    &quot; &lt;soap:Body&gt;&quot; +
    &quot; &lt;RetrieveMultiple xmlns=\&quot;&quot; +
    &quot;http://schemas.microsoft.com/crm/2007/WebServices\&quot;&gt;&quot; +
    &quot; &lt;query xmlns:q1=\&quot;&quot; +
    &quot;http://schemas.microsoft.com/crm/2006/Query&quot; +
    &quot;\&quot; xsi:type=\&quot;q1:QueryExpression\&quot;&gt;&quot; +
    &quot; &lt;q1:EntityName&gt;role&lt;/q1:EntityName&gt;&quot; +
    &quot; &lt;q1:ColumnSet xsi:type=\&quot;q1:ColumnSet\&quot;&gt;&quot; +
    &quot; &lt;q1:Attributes&gt;&quot; +
    &quot; &lt;q1:Attribute&gt;name&lt;/q1:Attribute&gt;&quot; +
    &quot; &lt;/q1:Attributes&gt;&quot; +
    &quot; &lt;/q1:ColumnSet&gt;&quot; +
    &quot; &lt;q1:Distinct&gt;false&lt;/q1:Distinct&gt;&quot; +
    &quot; &lt;q1:LinkEntities&gt;&quot; +
    &quot; &lt;q1:LinkEntity&gt;&quot; +
    &quot; &lt;q1:LinkFromAttributeName&gt;roleid&lt;/q1:LinkFromAttributeName&gt;&quot; +
    &quot; &lt;q1:LinkFromEntityName&gt;role&lt;/q1:LinkFromEntityName&gt;&quot; +
    &quot; &lt;q1:LinkToEntityName&gt;systemuserroles&lt;/q1:LinkToEntityName&gt;&quot; +
    &quot; &lt;q1:LinkToAttributeName&gt;roleid&lt;/q1:LinkToAttributeName&gt;&quot; +
    &quot; &lt;q1:JoinOperator&gt;Inner&lt;/q1:JoinOperator&gt;&quot; +
    &quot; &lt;q1:LinkEntities&gt;&quot; +
    &quot; &lt;q1:LinkEntity&gt;&quot; +
    &quot; &lt;q1:LinkFromAttributeName&gt;systemuserid&lt;/q1:LinkFromAttributeName&gt;&quot; +
    &quot; &lt;q1:LinkFromEntityName&gt;systemuserroles&lt;/q1:LinkFromEntityName&gt;&quot; +
    &quot; &lt;q1:LinkToEntityName&gt;systemuser&lt;/q1:LinkToEntityName&gt;&quot; +
    &quot; &lt;q1:LinkToAttributeName&gt;systemuserid&lt;/q1:LinkToAttributeName&gt;&quot; +
    &quot; &lt;q1:JoinOperator&gt;Inner&lt;/q1:JoinOperator&gt;&quot; +
    &quot; &lt;q1:LinkCriteria&gt;&quot; +
    &quot; &lt;q1:FilterOperator&gt;And&lt;/q1:FilterOperator&gt;&quot; +
    &quot; &lt;q1:Conditions&gt;&quot; +
    &quot; &lt;q1:Condition&gt;&quot; +
    &quot; &lt;q1:AttributeName&gt;systemuserid&lt;/q1:AttributeName&gt;&quot; +
    &quot; &lt;q1:Operator&gt;EqualUserId&lt;/q1:Operator&gt;&quot; +
    &quot; &lt;/q1:Condition&gt;&quot; +
    &quot; &lt;/q1:Conditions&gt;&quot; +
    &quot; &lt;/q1:LinkCriteria&gt;&quot; +
    &quot; &lt;/q1:LinkEntity&gt;&quot; +
    &quot; &lt;/q1:LinkEntities&gt;&quot; +
    &quot; &lt;/q1:LinkEntity&gt;&quot; +
    &quot; &lt;/q1:LinkEntities&gt;&quot; +
    &quot; &lt;/query&gt;&quot; +
    &quot; &lt;/RetrieveMultiple&gt;&quot; +
    &quot; &lt;/soap:Body&gt;&quot; +
    &quot;&lt;/soap:Envelope&gt;&quot; +
    &quot;&quot;;  

  var xmlHttpRequest = new ActiveXObject(&quot;Msxml2.XMLHTTP&quot;);  

  xmlHttpRequest.Open(&quot;POST&quot;, &quot;/mscrmservices/2007/CrmService.asmx&quot;, false);
  xmlHttpRequest.setRequestHeader(&quot;SOAPAction&quot;,
    &quot; http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple&quot;);
  xmlHttpRequest.setRequestHeader(&quot;Content-Type&quot;, &quot;text/xml; charset=utf-8&quot;);
  xmlHttpRequest.setRequestHeader(&quot;Content-Length&quot;, xml.length);
  xmlHttpRequest.send(xml);  

  var resultXml = xmlHttpRequest.responseXML;
  return(resultXml);
}
</pre>
<p>The next method is a simple method for searching the XML result from GetCurrentUserRoles.  By passing it the roles you are looking for and the roles the user has, it will determine if there is a match.  The roles it is passed is separated by a pipe (|).</p>
<pre name="code" class="javascript">
function UserHasRole(roleNames, rolesXML)
{
  // split roleNames on pipe
  var matchon = roleNames.split('|');

  if(rolesXML != null)
  {
    //select the node text
    var roles = rolesXML.selectNodes("//BusinessEntity/q1:name");
    if(roles != null)
    {
      for( i = 0; i < roles.length; i++)
      {
        for (j = 0; j < matchon.length; j++)
	{
	  // If there is a match, return true, found
	  if (roles[i].text == matchon[j]) return true;
        }
      }
    }
  }
  //otherwise return false
  return false;
}
</pre>
<p>Finally, we can now implement helper methods to allow the hiding of a tab or a field if a user DOES NOT have a specific role.</p>
</pre>
<pre name="code" class="javascript">
function HideTabByRole(role, roles, tabnumber)
// Tab number starts on 0
{
  var tab = document.getElementById('tab'+tabnumber+'Tab');
  var usrRole = UserHasRole(role, roles);
  if(!usrRole)
  {
    tab.style.display = "none";
  }
}

function HideFieldByRole(role, roles, field,cfield,dfield)
{
  var usrRole = UserHasRole(role, roles);
  if(!usrRole)
  {
    field.style.visibility = 'hidden';
    field.style.position = 'absolute';
    cfield.style.visibility = 'hidden';
    cfield.style.position = 'absolute';
    dfield.visibility = 'hidden';
    dfield.style.position = 'absolute';
  }
}
</pre>
<p>To implement this process, all you have to do is put every piece of code in this article inside your onload with the following customized for your situation.</p>
<pre name="code" class="javascript">
var UserRoles = GetCurrentUserRoles();

HideTabByRole('Account Managers|SystemAdministrators', UserRoles, 3);

HideFieldByRole('Account Managers', UserRoles,
                crmForm.all.parentcustomerid,
                crmForm.all.parentcustomerid_c,
                crmForm.all.parentcustomerid_d);
</pre>
<p>If you have any questions of problems implementing this in your environment, please let me know, and I&#8217;ll do my best to help you get it working</p>
]]></content:encoded>
			<wfw:commentRss>http://mscrmblogger.com/2009/09/30/hide-fields-or-tabs-by-role-with-javascript/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Auto-Update Duration/End Time JScript</title>
		<link>http://mscrmblogger.com/2009/09/28/auto-update-durationend-time-jscript/</link>
		<comments>http://mscrmblogger.com/2009/09/28/auto-update-durationend-time-jscript/#comments</comments>
		<pubDate>Mon, 28 Sep 2009 15:21:30 +0000</pubDate>
		<dc:creator>Carlton Colter</dc:creator>
				<category><![CDATA[Scripts]]></category>
		<category><![CDATA[crm4]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[microsoft dynamics CRM 4]]></category>
		<category><![CDATA[onload event]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[tasks]]></category>

		<guid isPermaLink="false">http://mscrmblogger.com/?p=113</guid>
		<description><![CDATA[Sometimes you need to be able to have preset values, and have the duration auto-update.  In this example, I will make the actual durations auto-update when one of the fields is changed.  If the duration is changed, then the end date will be changed.]]></description>
			<content:encoded><![CDATA[<p>Sometimes you need to be able to have preset values, and have the duration auto-update.  In this example, I will make the actual durations auto-update when one of the fields is changed.  If the duration is changed, then the end date will be changed.  Special thanks to Stephan Bayer for thinking of the idea.  We worked together to bring you this implementation of the <i>Auto-Update Duration/End Time JScript</i>.</p>
<p><b>Check out the video of how it works</b></p>
<p><object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/6Ccsn58SnFE&#038;hl=en&#038;fs=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/6Ccsn58SnFE&#038;hl=en&#038;fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object></p>
<p>Granted, in my implementation I added a field for time deductions, but I have commented it out in the code.  If you add a duration, just change the script to use that field.  Also, please make sure to add the fields to the required parameters for the event.  I would hate for someone&#8217;s code to stop working because a field was removed from the form.</p>
<p><b>This code is for the Start, End, and Deduct (if you have it) on change events:</b></p>
<pre name="code" class="javascript">
// Get End and Start
var start = crmForm.all.actualstart.DataValue;
var end = crmForm.all.actualend.DataValue;
// Can't calculate without any data
if (start==undefined || start==null || end==undefined || end==null) {
	return;
}

// Get 1 day in milliseconds
var one_minute=1000*60;

// Get time in days...
var time = (end-start)/one_minute;

/*
// Get the deduction
var deduct = crmForm.all.it_timeentry_deduct.DataValue
if (deduct!=undefined &#038;&#038; deduct!=null)
{
  time = time - deduct;
}
*/

// Can't be less than 0.
if (time&lt;0) time=0;

// Set the duration
crmForm.all.actualdurationminutes.DataValue = time;
</pre>
<p><b>This code is for the onchange event of the Duration field:</b></p>
<pre name="code" class="javascript">
var start = crmForm.all.actualstart.DataValue;
var duration = crmForm.all.actualdurationminutes.DataValue;
// Can't calculate without any data
if (start==undefined || start==null || duration==undefined || duration==null)
{
	return;
}

var end = start;
end.setMinutes(end.getMinutes()+duration);

/*
var deduct = crmForm.all.it_timeentry_deduct.DataValue
if (deduct!=undefined &#038;&#038; deduct!=null)
{
  end.setMinutes(end.getMinutes()+deduct);
}
*/

crmForm.all.actualend.DataValue=end;
</pre>
<p>To preset the duration, start time and end time, I added the following script to the form&#8217;s onload event</p>
<pre name="code" class="javascript">
var CRM_FORM_TYPE_CREATE = 1;
var CRM_FORM_TYPE_UPDATE = 2;

if (crmForm.FormType==CRM_FORM_TYPE_CREATE)
{
	var duration = 10;
	var start = new Date();
	crmForm.all.actualstart.DataValue = start;
	crmForm.all.actualdurationminutes.DataValue = duration;
	var start = crmForm.all.actualstart.DataValue;

	var end = start;
	end.setMinutes(end.getMinutes()+duration);

	crmForm.all.actualend.DataValue=end;
}
</pre>
<p>If you have any questions about this implementation or would like assistance implementing it somewhere, please post a comment and let us know.</p>
]]></content:encoded>
			<wfw:commentRss>http://mscrmblogger.com/2009/09/28/auto-update-durationend-time-jscript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Toggle Fields in a CRM form using Javascript</title>
		<link>http://mscrmblogger.com/2009/08/26/toggle-fields-in-a-crm-form-using-javascript/</link>
		<comments>http://mscrmblogger.com/2009/08/26/toggle-fields-in-a-crm-form-using-javascript/#comments</comments>
		<pubDate>Wed, 26 Aug 2009 20:39:48 +0000</pubDate>
		<dc:creator>Stephan Bayer</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[crm 4]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[microsoft dynamics CRM 4]]></category>
		<category><![CDATA[onload event]]></category>
		<category><![CDATA[radio button]]></category>
		<category><![CDATA[toggle field]]></category>

		<guid isPermaLink="false">http://mscrmblogger.com/?p=76</guid>
		<description><![CDATA[This script can be used to create a form in CRM that has fields appear based on a a radio (bit) yes/no.  If yes, answer an additional question, if no, keep the other field hidden.]]></description>
			<content:encoded><![CDATA[<p>This script can be used to create a form in CRM that has fields appear based on a certain condition (If yes, answer an additional question, if no, keep hidden)</p>
<p><object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/VxBFHdmYO2Q&#038;hl=en&#038;fs=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/VxBFHdmYO2Q&#038;hl=en&#038;fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object></p>
<p><strong>Steps: </strong></p>
<p>1. Create Form<br />
2. Identify Radio button you want to control the toggle of another field.<br />
3. Identify the field you want to hide.<br />
4. Modify below javascript code after line 46 and place into Form Properties-> Onload Event</p>
<pre name="code" class="javascript">
function registerEvent(id,eventname,eventfunction)
{
	// Register the *eventfunction* to the target *id* object for any event named *eventname*
	var obj = document.getElementById(id);
	if (obj==null) return;

	if (obj.addEventListener) {
		obj.addEventListener (eventname,eventfunction,false);
	} else if (obj.attachEvent) {
		obj.attachEvent (eventname,eventfunction);
	}
}

function ToggleElement(id, yesid)
{
	// get object and toggle between none and block display
	var obj = document.getElementById(id);
	if (obj==null) return;

	var yesobj = document.getElementById(yesid);
	if (yesobj==null) return;

	if (yesobj.checked) {
		obj.style.display='block';
	} else {
		obj.style.display='none';
	}
}

function AddRadioButtonSectionToggle(radioid,sectionid,no)
{
	var x = '2';
	if (no==true) x='1';
	// Register the click events for the toggles.
	registerEvent(radioid+'1','onclick',function() { ToggleElement(sectionid,radioid+x);});
	registerEvent(radioid+'2','onclick',function() { ToggleElement(sectionid,radioid+x);});

	// Toggle it now!
	ToggleElement(sectionid,radioid+x);
}

function ToggleSectionFormLoad()
{
	// Enable Radio Button Section Toggle

	// false=for yes
	AddRadioButtonSectionToggle('rad_it_aproposaccount','it_apropospatternafterid_c',false);
	AddRadioButtonSectionToggle('rad_it_aproposaccount','it_apropospatternafterid_d',false);

	AddRadioButtonSectionToggle('rad_it_existingphone','{0d269004-9f91-de11-890f-0050569b7606}',false);
	AddRadioButtonSectionToggle('rad_it_existingphone','it_phonemacaddressoripaddress_c',true);
	AddRadioButtonSectionToggle('rad_it_existingphone','it_phonemacaddressoripaddress_d',true);

	AddRadioButtonSectionToggle('rad_it_activedirectorymstnet','it_mstaccountpatternafterid_c',false);
	AddRadioButtonSectionToggle('rad_it_activedirectorymstnet','it_mstaccountpatternafterid_d',false);

	AddRadioButtonSectionToggle('rad_it_activedirectorymscrmcrmlocal','it_patternemaildistributionlistmemid_c',false);//false if radio button default to no
	AddRadioButtonSectionToggle('rad_it_activedirectorymscrmcrmlocal','it_patternemaildistributionlistmemid_d',false);

	AddRadioButtonSectionToggle('rad_it_projectaccess','it_projectaccesstype_c',false);
	AddRadioButtonSectionToggle('rad_it_projectaccess','it_projectaccesstype_d',false);
	AddRadioButtonSectionToggle('rad_it_projectaccess','{d0306d89-6d92-de11-890f-0050569b7606}',false);
	AddRadioButtonSectionToggle('rad_it_project_issupervisor_timesheetmanger','it_timesheetmanagerid_c',true);//true if radio button default to yes
	AddRadioButtonSectionToggle('rad_it_project_issupervisor_timesheetmanger','it_timesheetmanagerid_d',true);

	}

ToggleSectionFormLoad();
</pre>
]]></content:encoded>
			<wfw:commentRss>http://mscrmblogger.com/2009/08/26/toggle-fields-in-a-crm-form-using-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
