<?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; Scripts</title>
	<atom:link href="http://mscrmblogger.com/category/scripts/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>Lookup a lookup on another entity using JScript</title>
		<link>http://mscrmblogger.com/2009/10/15/lookup-a-lookup-on-another-entity-using-jscript/</link>
		<comments>http://mscrmblogger.com/2009/10/15/lookup-a-lookup-on-another-entity-using-jscript/#comments</comments>
		<pubDate>Thu, 15 Oct 2009 20:37:32 +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[lookup]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[microsoft dynamics CRM 4]]></category>

		<guid isPermaLink="false">http://mscrmblogger.com/?p=188</guid>
		<description><![CDATA[Do you need the value of a lookup?  I have written this bit of code to look a lookup value stored on another entity.  Like finding the Customer lookup on an Incident (Case).  Here is a bit of code that can lookup a lookup on any entity in CRM.]]></description>
			<content:encoded><![CDATA[<p>Do you need the value of a lookup?  I have written this bit of code to look a lookup value stored on another entity.  Like finding the Customer lookup on an Incident (Case).</p>
<ul>
<li>ID: The unique id value of the entity&#8217;s object</li>
<li>ENTITY: The name of the entity to lookup</li>
<li>IDFIELD: The entity&#8217;s primary id field name, like accountid, incidentid, etc.</li>
<li>LOOKUPFIELD: The name of the field you want to lookup, like customerid</li>
<li>RETURNTYPE: The name of the entity&#8217;s return type, like contact or account.  If there are multiple possible, then specify &#8220;multiple&#8221; and it will pass back the type instead of type name.  If you specify &#8220;value&#8221; it will return just the value.</li>
</ul>
<hr />
<pre name="code" class="javascript">
function getEntityLookup(id, entity, idfield, lookupfield, returntype) {
    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&gt;&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='&quot; + entity + &quot;'&amp;gt;&quot; +
	&quot; &amp;lt;attribute name='&quot; + lookupfield + &quot;' /&amp;gt;&quot; +
	&quot; &amp;lt;filter&amp;gt;&quot; +
	&quot; &amp;lt;condition attribute='&quot; + idfield + &quot;' operator='eq' value='&quot; + id + &quot;' /&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&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/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(lookupfield);
    if (returntype == 'value') return result[0].text;

    var lookupItem = new Object();
    for (var i = 0; i &lt; result[0].attributes.length; i++) {
        var att = result[0].attributes[i];
        if (returntype == 'multiple' &amp;&amp; att.name == &quot;type&quot;) lookupItem.type = att.value;
        if (att.name == &quot;name&quot;) lookupItem.name = att.value;
    }
    if (returntype != 'multiple') lookupItem.typename = returntype;

    lookupItem.id = result[0].text;

    return lookupItem;
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://mscrmblogger.com/2009/10/15/lookup-a-lookup-on-another-entity-using-jscript/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<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>Changing PickList Values on Yes/No Form Field Change</title>
		<link>http://mscrmblogger.com/2009/10/03/changing-picklist-values-on-yesno-form-field-change/</link>
		<comments>http://mscrmblogger.com/2009/10/03/changing-picklist-values-on-yesno-form-field-change/#comments</comments>
		<pubDate>Sun, 04 Oct 2009 02:32:02 +0000</pubDate>
		<dc:creator>Carlton Colter</dc:creator>
				<category><![CDATA[Scripts]]></category>
		<category><![CDATA[boolean]]></category>
		<category><![CDATA[crm]]></category>
		<category><![CDATA[crm 4]]></category>
		<category><![CDATA[crm4]]></category>
		<category><![CDATA[field]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jscript]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[microsoft dynamics CRM 4]]></category>
		<category><![CDATA[onchange]]></category>
		<category><![CDATA[picklist]]></category>

		<guid isPermaLink="false">http://mscrmblogger.com/?p=156</guid>
		<description><![CDATA[<p>Recently on the Microsoft forums someone asked how to change the values of a picklist based on the yes no value of a boolean field.  Just add this script to the OnChange event of the yes/no field.</p>]]></description>
			<content:encoded><![CDATA[<p>Recently on the Microsoft forums someone asked how to change the values of a picklist based on the yes no value of a boolean field.  Just add this script to the OnChange event of the yes/no field.</p>
<p><a href="http://technet.microsoft.com/en-us/library/aa681845.aspx">This technet page shows some of the functions on different form fields.</a></p>
<p><strong>Here is an example of how to change the picklist based on a yes/no field:</strong></p>
<pre name="code" class="javascript">
function CreatePicklistValue(Name,Value)
{
	var retval = new Array();
	retval[0]  = Name;
	retval[1]  = Value;
	return retval;
}

function BuildPickList(picklist, valuelist)
{
	var options = picklist.Options;
	for(var i=0; i&lt;options.length; i++)
	{
		var option = options[i];
		picklist.DeleteOption(option.DataValue);
	}
	for(var j=0; j&lt;valuelist.length; j++)
	{
		var listitem = valuelist[j];
		picklist.AddOption(listitem[0], listitem[1]);
	}
}

var lista = new Array();
lista[0] = CreatePicklistValue('Alpha',0);
lista[1] = CreatePicklistValue('Beta',1);

var listb = new Array();
listb[0] = CreatePicklistValue('Charlie',2);
listb[1] = CreatePicklistValue('Delta',3);

var uselista = crmForm.all.YesNo.DataValue;
var picklist = crmForm.all.Picklist;

if (uselista==true) {
   BuildPickList(picklist, lista);
} else {
   BuildPickList(picklist, listb);
}
</pre>
<p>You just need to change the values of lista and listb, and the crmForm.all.YesNo and crmForm.all.Picklist.</p>
]]></content:encoded>
			<wfw:commentRss>http://mscrmblogger.com/2009/10/03/changing-picklist-values-on-yesno-form-field-change/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>Remove special characters on value change.</title>
		<link>http://mscrmblogger.com/2009/10/01/remove-special-characters-on-value-change/</link>
		<comments>http://mscrmblogger.com/2009/10/01/remove-special-characters-on-value-change/#comments</comments>
		<pubDate>Thu, 01 Oct 2009 15:02:21 +0000</pubDate>
		<dc:creator>Carlton Colter</dc:creator>
				<category><![CDATA[Scripts]]></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[onchange]]></category>
		<category><![CDATA[script]]></category>

		<guid isPermaLink="false">http://mscrmblogger.com/?p=150</guid>
		<description><![CDATA[This blog entry contains some helpful information about removing special characters from a string using javascript.  I was able to modify it to be just a few  of code in an onchange event.  Just rename new_name to the attribute your correcting.

if (crmForm.all.new_name.DataValue &#038;&#038; crmForm.all.new_name.DataValue.length >0)
{
crmForm.all.new_name.DataValue = crmForm.all.new_name.DataValue.replace(/[^a-zA-Z 0-9]+/g,'');
}

]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.developersnippets.com/2007/05/12/remove-special-characters-like-etc-from-a-string-using-javascript/">This blog entry</a> contains some helpful information about removing special characters from a string using javascript.  I was able to modify it to be just a few  of code in an onchange event.  Just rename new_name to the attribute your correcting.</p>
<pre name="code" class="javascript">
if (crmForm.all.new_name.DataValue &#038;&#038; crmForm.all.new_name.DataValue.length >0)
{
crmForm.all.new_name.DataValue = crmForm.all.new_name.DataValue.replace(/[^a-zA-Z 0-9]+/g,'');
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://mscrmblogger.com/2009/10/01/remove-special-characters-on-value-change/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>Change CRM Address State to a drop down box.</title>
		<link>http://mscrmblogger.com/2009/05/11/change-crm-address-state-to-a-drop-down-box/</link>
		<comments>http://mscrmblogger.com/2009/05/11/change-crm-address-state-to-a-drop-down-box/#comments</comments>
		<pubDate>Tue, 12 May 2009 01:35:21 +0000</pubDate>
		<dc:creator>Carlton Colter</dc:creator>
				<category><![CDATA[Scripts]]></category>
		<category><![CDATA[account]]></category>
		<category><![CDATA[address]]></category>
		<category><![CDATA[contact]]></category>
		<category><![CDATA[core]]></category>
		<category><![CDATA[crm]]></category>
		<category><![CDATA[crm4]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[lead]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[state]]></category>

		<guid isPermaLink="false">http://mscrmblogger.com/?p=47</guid>
		<description><![CDATA[Do you want a drop down box for the state, but you don't want to break core CRM functionality?  This little onload script will show the states for the United States and Canada (You can always modify it to have more options).  This will help restrict what people put in the state field by providing them a drop down box.  It will also allow them to view the old text box and enter any custom information just by clicking on '* ENTER A CUSTOM PROVINCE *'.]]></description>
			<content:encoded><![CDATA[<p><b>Do you want a drop down box for the state, but you don&#8217;t want to break core CRM functionality?</b></p>
<p>Ok.  This little onload script will show the states for the United States and Canada (You can always modify it to have more options).  This will help restrict what people put in the state field by providing them a drop down box.  It will also allow them to view the old text box and enter any custom information just by clicking on &#8216;* ENTER A CUSTOM PROVINCE *&#8217;.</p>
<p>This script will work on the onload for a lead, contact, account, and sales-contact.</p>
<p><i>Please post a comment and let me know what you think!</i></p>
<p>If you&#8217;re a consultant and you use this, please at least give us credit and provide the customer with the web address to our blog.</p>
<p><b>Onload Jscript Code:</b></p>
<pre name="code" class="javascript">
function ddlState ()
{

	// Get the state or province cell.
	var provinceid = 'stateorprovince';
	var statecell = document.getElementById(provinceid + '_d');

	if (!statecell)
	{
		provinceid = 'address1_stateorprovince';
		statecell = document.getElementById(provinceid + '_d');
	}

	var stateobject = document.getElementById(provinceid);
	stateobject.style.display = 'none';

	var statevalue = stateobject.value;

	var ddl = document.createElement('select');
	ddl.setAttribute('id','new_stateorprovince');
	ddl.setAttribute('class','ms-crm-SelectBox');
	ddl.setAttribute('req','2');
	ddl.setAttribute('height','4');
	ddl.setAttribute('style','IME-MODE: auto');

	// Build some lists.
	var CStateName = new Array();
	var CStateAbbr = new Array();
	var USStateName = new Array();
	var USStateAbbr = new Array();

	// If nothing is entered for state, show SELECT A STATE
	var found=false;
	if (statevalue.length&lt;1)
	{
		stateoption=document.createElement('option');
		stateoption.setAttribute('value','');
		stateoption.innerHTML = ' ** SELECT A STATE ** ';
		ddl.appendChild(stateoption);
		found=true;
	}

	// Build Canada Array
	CStateName[0]  = "Alberta";                   CStateAbbr[0]  = "AB";
	CStateName[1]  = "British Columbia";          CStateAbbr[1]  = "BC";
	CStateName[2]  = "Manitoba";                  CStateAbbr[2]  = "MB";
	CStateName[3]  = "New Brunswick";             CStateAbbr[3]  = "NB";
	CStateName[4]  = "Newfoundland and Labrador"; CStateAbbr[4]  = "NL";
	CStateName[5]  = "Northwest Territories";     CStateAbbr[5]  = "NT";
	CStateName[6]  = "Nova Scotia";               CStateAbbr[6]  = "NS";
	CStateName[7]  = "Nunavut";                   CStateAbbr[7]  = "NU";
	CStateName[8]  = "Ontario";                   CStateAbbr[8]  = "ON";
	CStateName[9]  = "Prince Edward Island";      CStateAbbr[9]  = "PE";
	CStateName[10] = "Quebec";                    CStateAbbr[10] = "QC";
	CStateName[11] = "Saskatchewan";              CStateAbbr[11] = "SK";
	CStateName[12] = "Yukon";                     CStateAbbr[12] = "YT";

	// Build US Array
	USStateName[0]  = "Alabama";        USStateAbbr[0]  = "AL";
	USStateName[1]  = "Alaska";         USStateAbbr[1]  = "AK";
	USStateName[2]  = "Arizona";        USStateAbbr[2]  = "AZ";
	USStateName[3]  = "Arkansas";       USStateAbbr[3]  = "AR";
	USStateName[4]  = "California";     USStateAbbr[4]  = "CA";
	USStateName[5]  = "Colorado";       USStateAbbr[5]  = "CO";
	USStateName[6]  = "Connecticut";    USStateAbbr[6]  = "CT";
	USStateName[7]  = "District of Columbia"; USStateAbbr[7]  = "DC";
	USStateName[8]  = "Delaware";       USStateAbbr[8]  = "DE";
	USStateName[9]  = "Florida";        USStateAbbr[9]  = "FL";
	USStateName[10]  = "Georgia";       USStateAbbr[10]  = "GA";
	USStateName[11] = "Hawaii";         USStateAbbr[11] = "HI";
	USStateName[12] = "Idaho";          USStateAbbr[12] = "ID";
	USStateName[13] = "Illinois";       USStateAbbr[13] = "IL";
	USStateName[14] = "Indiana";        USStateAbbr[14] = "IN";
	USStateName[15] = "Iowa";           USStateAbbr[15] = "IA";
	USStateName[16] = "Kansas";         USStateAbbr[16] = "KS";
	USStateName[17] = "Kentucky";       USStateAbbr[17] = "KY";
	USStateName[18] = "Louisiana";      USStateAbbr[18] = "LA";
	USStateName[19] = "Maine";          USStateAbbr[19] = "ME";
	USStateName[20] = "Maryland";       USStateAbbr[20] = "MD";
	USStateName[21] = "Massachusetts";  USStateAbbr[21] = "MA";
	USStateName[22] = "Michigan";       USStateAbbr[22] = "MI";
	USStateName[23] = "Minnesota";      USStateAbbr[23] = "MN";
	USStateName[24] = "Mississippi";    USStateAbbr[24] = "MS";
	USStateName[25] = "Missouri";       USStateAbbr[25] = "MO";
	USStateName[26] = "Montana";        USStateAbbr[26] = "MT";
	USStateName[27] = "Nebraska";       USStateAbbr[27] = "NE";
	USStateName[28] = "Nevada";         USStateAbbr[28] = "NV";
	USStateName[29] = "New Hampshire";  USStateAbbr[29] = "NH";
	USStateName[30] = "New Jersey";     USStateAbbr[30] = "NJ";
	USStateName[31] = "New Mexico";     USStateAbbr[31] = "NM";
	USStateName[32] = "New York";       USStateAbbr[32] = "NY";
	USStateName[33] = "North Carolina"; USStateAbbr[33] = "NC";
	USStateName[34] = "North Dakota";   USStateAbbr[34] = "ND";
	USStateName[35] = "Ohio";           USStateAbbr[35] = "OH";
	USStateName[36] = "Oklahoma";       USStateAbbr[36] = "OK";
	USStateName[37] = "Oregon";         USStateAbbr[37] = "OR";
	USStateName[38] = "Pennsylvania";   USStateAbbr[38] = "PA";
	USStateName[39] = "Rhode Island";   USStateAbbr[39] = "RI";
	USStateName[40] = "South Carolina"; USStateAbbr[40] = "SC";
	USStateName[41] = "South Dakota";   USStateAbbr[41] = "SD";
	USStateName[42] = "Tennessee";      USStateAbbr[42] = "TN";
	USStateName[43] = "Texas";          USStateAbbr[43] = "TX";
	USStateName[44] = "Utah";           USStateAbbr[44] = "UT";
	USStateName[45] = "Vermont";        USStateAbbr[45] = "VT";
	USStateName[46] = "Virginia";       USStateAbbr[46] = "VA";
	USStateName[47] = "Washington";     USStateAbbr[47] = "WA";
	USStateName[48] = "West Virginia";  USStateAbbr[48] = "WV";
	USStateName[49] = "Wisconsin";      USStateAbbr[49] = "WI";
	USStateName[50] = "Wyoming";        USStateAbbr[50] = "WY";

	// Build the drop down
	var stateoption;
	stateoption=document.createElement('option');
	stateoption.setAttribute('value','');
	stateoption.innerHTML = '---United States--------';
	ddl.appendChild(stateoption);

	var i=0;
	for(i=0;i&lt;usstatename .length;i++)
	{
		stateoption=document.createElement('option');
		stateoption.setAttribute('value',USStateAbbr[i]);
		if (USStateAbbr[i]==statevalue)
		{
			stateoption.setAttribute('selected','selected');
			found = true;
		}
		stateoption.innerHTML = USStateAbbr[i] +
		' (' + USStateName[i] + ')';
		ddl.appendChild(stateoption);
	}

	stateoption=document.createElement('option');
	stateoption.setAttribute('value','');
	stateoption.innerHTML = '---Canada---------------';
	ddl.appendChild(stateoption);

	var i=0;
	for(i=0;i&lt;CStateName.length;i++)
	{
		stateoption=document.createElement('option');
		stateoption.setAttribute('value',CStateAbbr[i]);
		if (CStateAbbr[i]==statevalue)
		{
			stateoption.setAttribute('selected','selected');
			found = true;
		}
		stateoption.innerHTML = CStateAbbr[i] + ' (' + CStateName[i] + ')';
		ddl.appendChild(stateoption);
	}

	stateoption=document.createElement('option');
	stateoption.setAttribute('value','customselection');
	stateoption.innerHTML = ' ** ENTER A CUSTOM PROVINCE ** ';

	if (found==false)
	{
		stateoption=document.createElement('option');
		stateoption.setAttribute('value',statevalue);
		stateoption.setAttribute('selected','selected');
		stateoption.innerHTML = statevalue + ' (Custom)';
		ddl.appendChild(stateoption);
	}

	ddl.appendChild(stateoption);

	// Add the drop down and size accordingly.
	statecell.appendChild(ddl);
	ddl.onchange = textState;
	ddl.style.width = '100%';
}

function textState()
{
	// Find drop down and real textbox
	provinceid = 'stateorprovince';

	var stateobject = document.getElementById(provinceid);
	if (!stateobject)
	{
		provinceid = 'address1_stateorprovince';
		stateobject = document.getElementById(provinceid);
	}
	var ddl = document.getElementById('new_stateorprovince');

	// if dropdown value is customselection, show real textbox
	if (ddl.value=='customselection')
	{
		// Get the state or province cell.
		var statecell = document.getElementById(provinceid + '_d');
		statecell.removeChild(ddl);
		stateobject.style.display = 'inline';
	} else {
		// if dropdown is not customselection,
		// put value from ddl into real textbox.
		stateobject.value = ddl.value;
	}

}
ddlState();
</pre>
]]></content:encoded>
			<wfw:commentRss>http://mscrmblogger.com/2009/05/11/change-crm-address-state-to-a-drop-down-box/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
