<?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>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>Lookup data from a related entity (Lookup) using JScript and the XrmServiceToolkit</title>
		<link>http://mscrmblogger.com/2012/05/10/lookup-data-from-a-related-entity-lookup-using-jscript-and-the-xrmservicetoolkit/</link>
		<comments>http://mscrmblogger.com/2012/05/10/lookup-data-from-a-related-entity-lookup-using-jscript-and-the-xrmservicetoolkit/#comments</comments>
		<pubDate>Thu, 10 May 2012 20:22:56 +0000</pubDate>
		<dc:creator>Carlton Colter</dc:creator>
				<category><![CDATA[API-SDK]]></category>
		<category><![CDATA[Extensions]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[Web Resources]]></category>
		<category><![CDATA[CodePlex]]></category>
		<category><![CDATA[crm]]></category>
		<category><![CDATA[crm 2011]]></category>
		<category><![CDATA[CRM Online]]></category>
		<category><![CDATA[crm sdk]]></category>
		<category><![CDATA[crm2011]]></category>
		<category><![CDATA[field]]></category>
		<category><![CDATA[forms]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jscript]]></category>
		<category><![CDATA[lookup]]></category>
		<category><![CDATA[Microsoft Dynamics CRM 2011]]></category>
		<category><![CDATA[onchange]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[Web Resource]]></category>
		<category><![CDATA[XrmServiceToolkit]]></category>

		<guid isPermaLink="false">http://mscrmblogger.com/?p=1007</guid>
		<description><![CDATA[Lookup information from a related entity using the XrmServiceToolkit or just the REST endpoint.]]></description>
			<content:encoded><![CDATA[<p>Do you need the value from a related entity, like a lookup?  I had written this for CRM 4 a while back, and people use it, but I occasionally get questions about looking up other values in CRM.  This bit of code will help you find those values on a related object.  Below are two options to do this.  Option 1: using the REST SDK and JSON2, and then Option 2: using Jaimie Ji&#8217;s <a href="http://xrmservicetoolkit.codeplex.com/" target="_blank">XRM Service Toolkit</a>.</p>
<p>Jaimie Ji&#8217;s <a href="http://xrmservicetoolkit.codeplex.com/" target="_blank">XRM Service Toolkit</a> provides a comprehensive set of JScript libraries for interacting with the SOAP and REST SDK through JavaScript.  While Option 1 in this particular use case is fairly simple, when you start needing to do more advanced things, you may want to look at using that kit.  It is fairly comprehensive and he keeps it up to date.</p>
<h1>Option 1: Using the REST SDK and JSON2</h1>
<p>Using the REST SDK is great, and there are some really good examples in the CRM SDK help file.  Now in order to use the below code-block, you need to use JSON.</p>
<p>JSON, or JavaScript Object Notation, is a text format that is used to interchange data.  JSON2.js is a lightweight javascript that convert the strings to javascript objects and vice-versa.  The CRM REST SDK can return data in JSON format.</p>
<p>To get the code for JSON2, you can go <a href="https://github.com/douglascrockford/JSON-js" target="_blank">here</a>.  You will need to embed this code into your javascript file or include json2.js along with the code below.  Without it, you will not be able to parse the data properly, and the code below uses the JSON library in json2.js.</p>
<p>Ok, below we have 3 functions:</p>
<ul>
<li><b>getServerUrl</b> :  which gets the server URL &#8211; taken from the <a href="http://crmrestkit.codeplex.com/" target+"_blank">CrmRestKit</a></li>
<li><b>Lookup_Changed</b> : what you call when a lookup is changed</li>
<li><b>retrieveReqCallBack</b> : the callback function that performs any actions</li>
<li>
</li>
</ul>
<p>You would put Lookup_Changed in the change event of a lookup field and specify the attributes like one of the following:</p>
<ul>
<li>&#8216;contactid&#8217;,'Contact&#8217;,'contactlookup&#8217;</li>
<li>&#8216;contactid&#8217;,'Contact&#8217;,'contactlookup&#8217;,['Telephone1']</li>
<li>&#8216;contactid&#8217;,'Contact&#8217;,'contactlookup&#8217;,['Telephone1','FullName]</li>
</ul>
<p>Then you&#8217;d put your actions inside the retrieveReqCallBack.</p>
<pre name="code" class="javascript">
function getServerUrl() {
    // From CrmRestKit.js
    var localServerUrl = window.location.protocol + &quot;/&quot; + window.location.host;
    var context = parent.Xrm.Page.context;

    if (context.isOutlookClient() &amp;&amp; !context.isOutlookOnline()) {
        return localServerUrl;
    }
    else {
        var crmServerUrl = context.getServerUrl();
        crmServerUrl = crmServerUrl.replace(/^(http|https):\/\/([_a-zA-Z0-9\-\.]+)(:([0-9]{1,5}))?/, localServerUrl);
        crmServerUrl = crmServerUrl.replace(/\/$/, &quot;&quot;);
    }
    return crmServerUrl;
}

function Lookup_Changed(attributeName, entityName, callbackId, columns) {
    var serverUrl = Xrm.Page.context.getServerUrl();
    var ODataPath = serverUrl + &quot;/XRMServices/2011/OrganizationData.svc&quot;;

    var lookup = Xrm.Page.data.entity.attributes.get(attributeName).getValue();
    if (lookup===null) {
        return false;
    }

    var id = lookup[0].id;
    if (id == null) {
        return false;
    }

    var retrieveReq = new XMLHttpRequest();

    var url = ODataPath + &quot;/&quot;+entityName+&quot;Set(guid'&quot; + id + &quot;')&quot;;
    if (columns !== undefined &amp;&amp; columns !== null) {
        url = url + &quot;?$select=&quot; + columns.join(',');
    }

    retrieveReq.open(&quot;GET&quot;, url, true);
    retrieveReq.setRequestHeader(&quot;Accept&quot;, &quot;application/json&quot;);
    retrieveReq.setRequestHeader(&quot;Content-Type&quot;, &quot;application/json; charset=utf-8&quot;);
    retrieveReq.onreadystatechange = function () {
        if (this.readyState == 4 /* complete */) {
            if (this.status == 200) {
                var data;
                var jData;
                jData = JSON.parse(this.responseText);
                if (jData &amp;&amp; jData.d &amp;&amp; jData.d.results &amp;&amp; jData.d.results.length &gt; 0) {
                    data = jData.d.results[0];
                } else if (jData &amp;&amp; jData.d) {
                    data = jData.d;
                }

                if (data == null) {
                    return;
                }
                retrieveReqCallBack(callbackId, data);
            }
        }
    };
    retrieveReq.send();
    return true;
}

function retrieveReqCallBack(calltype, data) {
    var toLookup=function(data) {
        if (data==null || data==&quot;&quot; || (data.Id==null)) {
            return null;
        }

        var result=new Array();
        result[0] = {};
        result[0].id = data.Id;
        result[0].name = data.Name;
        result[0].entityType = data.LogicalName;
        return result;
    }

    switch (calltype) {
        case 'pricelookup':
            Xrm.Page.data.entity.attributes.get(&quot;new_pricecategorylookup&quot;).setValue(toLookup(c.new_pricecategorylookup);
            break;
        case 'contactlookup':
            Xrm.Page.data.entity.attributes.get(&quot;insp_poctelephonenumber&quot;).setValue(c.Telephone1);
            break;
        default:
            break;
    }
}
</pre>
<h1>Option 2: Using the XrmServiceToolkit with similar functions</h1>
<p>Ok, now let&#8217;s assume you are using the <a href="http://xrmservicetoolkit.codeplex.com/" target="_blank">XRM Service Toolkit</a>, and you want to do the same thing, using a Lookup_Changed function and the retrieveReqCallBack function.  Instead of embedding JSON and a method to get the server url, you can use the following:</p>
<pre name="code" class="javascript">
function Lookup_Changed(attributeName, entityName, callbackId, columns) {
    var lookup = Xrm.Page.data.entity.attributes.get(attributeName).getValue();
    if (lookup===null) {
        return false;
    }

    if (lookup[0].id == null) {
        return false;
    }

	XrmServiceToolkit.Rest.Retrieve(
        entityName,
        lookup[0].id,
        columns,
		null,
        function (result) {
            retrieveReqCallBack(callbackId, result);
        },
        function (error) {
            throw error;
        },
		true
    );
}

function retrieveReqCallBack(calltype, data) {
    var toLookup=function(data) {
        if (data==null || data==&quot;&quot; || (data.Id==null)) {
            return null;
        }

        var result=new Array();
        result[0] = {};
        result[0].id = data.Id;
        result[0].name = data.Name;
        result[0].entityType = data.LogicalName;
        return result;
    }

    switch (calltype) {
        case 'pricelookup':
            Xrm.Page.data.entity.attributes.get(&quot;new_pricecategorylookup&quot;).setValue(toLookup(c.new_pricecategorylookup);
            break;
        case 'contactlookup':
            Xrm.Page.data.entity.attributes.get(&quot;insp_poctelephonenumber&quot;).setValue(c.Telephone1);
            break;
        default:
            break;
    }
}
</pre>
<p>The <a href="http://xrmservicetoolkit.codeplex.com/" target="_blank">XRM Service Toolkit</a> does a lot, and I would suggest checking it out.</p>
]]></content:encoded>
			<wfw:commentRss>http://mscrmblogger.com/2012/05/10/lookup-data-from-a-related-entity-lookup-using-jscript-and-the-xrmservicetoolkit/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>InfoPath to CRM: Accepting Emailed InfoPath Forms</title>
		<link>http://mscrmblogger.com/2011/11/21/infopath-to-crm-accepting-emailed-infopath-forms/</link>
		<comments>http://mscrmblogger.com/2011/11/21/infopath-to-crm-accepting-emailed-infopath-forms/#comments</comments>
		<pubDate>Mon, 21 Nov 2011 19:05:51 +0000</pubDate>
		<dc:creator>Carlton Colter</dc:creator>
				<category><![CDATA[API-SDK]]></category>
		<category><![CDATA[Extensions]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[Plugins]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[Web Resources]]></category>
		<category><![CDATA[contact]]></category>
		<category><![CDATA[control]]></category>
		<category><![CDATA[crm]]></category>
		<category><![CDATA[crm 2011]]></category>
		<category><![CDATA[forms]]></category>
		<category><![CDATA[InfoPath]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jscript]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[microsoft dynamics crm]]></category>
		<category><![CDATA[Microsoft Dynamics CRM 2011]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[Web Resource]]></category>

		<guid isPermaLink="false">http://mscrmblogger.com/?p=889</guid>
		<description><![CDATA[Everybody loves to use InfoPath forms.  And let's face it, we all wish we could just take them and put them into CRM.  This post delves into how I integrated InfoPath &#38; CRM to dynamically allow InfoPath forms received by email into CRM records.  It is still <i>under development</i>, but the source code has been partially tested and works.]]></description>
			<content:encoded><![CDATA[<p>Everybody loves to use InfoPath forms.  And let&#8217;s face it, we all wish we could just take them and put them into CRM.  What I&#8217;ve written is a simple way to do that without the hassle of figuring out how to bind the InfoPath form schema to a custom WCF service.  It still doesn&#8217;t do everything I want it to, but it works, so I figured I&#8217;d go ahead and put it out there for everyone to look at, play with, and even use.</p>
<p>If you are interested in this solution, you can download the following:</p>
<ul>
<li><a href='http://mscrmblogger.com/wp-content/uploads/2011/11/InfoPath_1_0_0_0_managed.zip'>Managed Solution</a> (this is the only thing you need)</li>
<li><a href='http://mscrmblogger.com/wp-content/uploads/2011/11/InfoPath_1_0_0_0.zip'>Unmanaged Solution</a> (in case you want to modify mine, like add grid support, it is on my to-do list)</li>
<li><a href='http://mscrmblogger.com/wp-content/uploads/2011/11/InfoPath4CRMSourceCode.zip'>Source Code</a> (this is in case you would prefer to use a WCF service or if you just want to play around with my code)</li>
</ul>
<p>Once you&#8217;ve installed the managed solution, you&#8217;ll need to do the following to create / setup an InfoPath form:</p>
<ol>
<li>
<p>For this particular InfoPath integration, you need to create InfoPath Forms that mirror a CRM Entity.<br />
<strong>Note: </strong><em>You can always create a new entity for the form data.</em></p>
<p style="text-align: center"><img src="http://mscrmblogger.com/wp-content/uploads/2011/11/112111_1905_InfoPathtoC13.png" alt=""/></p>
</li>
<li>
<p>Rename the default form Group to the name of the entity (ex: Contact)</p>
<p style="text-align: center"><img src="http://mscrmblogger.com/wp-content/uploads/2011/11/112111_1905_InfoPathtoC23.png" alt=""/></p>
</li>
<li>
<p>Inside CRM create the InfoPath form (it is an entity included in the solution), select the entity and save the form.
</p>
<p style="text-align: center"><img src="http://mscrmblogger.com/wp-content/uploads/2011/11/112111_1905_InfoPathtoC31.png" alt=""/></p>
</li>
<li>
<p>Once it has been saved, you can copy the ID using the &#8220;Copy ID&#8221; link in the header.<br />
<br /><strong>Note: </strong>If you do not select the <em>Allow Any CRM Field </em>checkbox, you will need to add the individual Fields, which can be found in the left navigation.</p>
</li>
<li>
<p>In the InfoPath Form Designer, add a field named SubmissionCode with the Default Value as the ID you just copied.<br />
<br /><b>Note:</b> Using the submission code allows us to check and see if the form is valid, active, and allowed.</p>
<p style="text-align: center"><img src="http://mscrmblogger.com/wp-content/uploads/2011/11/submissioncode.png" alt=""/></p>
</li>
<li>
<p>Add a field for each of the fields in CRM that you want to be available, selecting the appropriate data type.<br />
<br /><b>Note:</b> Unfortunately I have not had the time to test the fields and have been using the checkbox on the infopath form record to enable any/all CRM fields.  However, I did not want to delay releasing the code and information as it currently is.  If someone identifies a problem with this or any other section of the code, I&#8217;ll be happy to look into it and work on resolving the issue.</p>
<p style="text-align: center"><img src="http://mscrmblogger.com/wp-content/uploads/2011/11/112111_1905_InfoPathtoC53.png" alt=""/></p>
</li>
<li>
<p>Layout the fields on the form, then click File &gt; Submit Options &gt; To Email</p>
<p style="text-align: center"><img src="http://mscrmblogger.com/wp-content/uploads/2011/11/112111_1905_InfoPathtoC63.png" alt=""/></p>
</li>
<li>
<p>Enter the Queue email or where you want new forms to be submitted.</p>
<p style="text-align: center"><img src="http://mscrmblogger.com/wp-content/uploads/2011/11/112111_1905_InfoPathtoC73.png" alt=""/></p>
</li>
<li>
<p>Click Next.</p>
<p style="text-align: center"><img src="http://mscrmblogger.com/wp-content/uploads/2011/11/112111_1905_InfoPathtoC83.png" alt=""/></p>
</li>
<li>
<p>Click Finish.</p>
<p style="text-align: center"><img src="http://mscrmblogger.com/wp-content/uploads/2011/11/112111_1905_InfoPathtoC93.png" alt=""/></p>
</li>
<li>Save and close the form designer.</li>
<li>
<p>Open the form and test submission.</p>
<p style="text-align: center"><img src="http://mscrmblogger.com/wp-content/uploads/2011/11/112111_1905_InfoPathtoC103.png" alt=""/></p>
</li>
</ol>
<p>Once the email comes into CRM, the process of reading the form is easy; you just open it up and click the <img src="http://mscrmblogger.com/wp-content/uploads/2011/11/112111_1905_InfoPathtoC113.png" alt=""/> Import InfoPath button on the ribbon.</p>
<p>Usually I would post the source-code and comment on how it works in great detail, but this time, it really depends on which component.  There is a plugin and WCF service written in C# as well as two Jscript files, one for the buttons and one for the InfoPath form inside CRM that creates a drop-down of the entities in CRM.  I would suggest downloading the unmanaged solution and source-code if you want to delve into those items.</p>
<p>Now, for complex situations or form conversions, binding the InfoPath schema is the way to go.  Using that method (the one described in detail on <a href="http://blogs.msdn.com/b/philoj/archive/2005/11/08/490200.aspx">Philo&#8217;s Weblog</a>) you can import the InfoPath form schema into Visual Studio:</p>
<ol>
<li>Create an InfoPath form and lay it out the way you want it.
</li>
<li>Extract the form files (File | Extract Form Files) to a location you can find later.
</li>
<li>Close the InfoPath form designer.
</li>
<li>Open the Visual Studio command prompt.
</li>
<li>Change to the directory where you extracted the files<br /><strong>cd c:\myinfopathfiles<br />
</strong></li>
<li>Run xsd against the myschema.xsd in that directory, and specify the namespace you would like to use.<br /><strong>Note: </strong>My WCF namespace is CrmInfoPathService and my plugin namespace is InfoPathPlugin.<br /><strong>xsd.exe myschema.xsd /classes /l:cs /n:InfoPathPlugin<br />
</strong></li>
<li>Then you can deserialize the xml into the object and store it in CRM or any other system using the logic embedded in your code.</li>
</ol>
<p>The key point to my solution was not to do something that everyone does on occasion, but to make something so that I can leverage it in the future and not have to create a new integration every time I wanted to leverage a new form.  Now, on that note, here are the list of things that if you delve into, I&#8217;d like to know, a.k.a. my to-do list for this project:
</p>
<ul>
<li>Add a button onto the grid for activities</li>
<li>Create a way to handle child entities.</li>
<li>Modify the plugin/WCF code to link the created entities to the form email using connections.</li>
<li>Change the button on the form to monitor for the results (success/failure) and report back to the user.</li>
<li><i>Any great ideas my readers think should be added!</i></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://mscrmblogger.com/2011/11/21/infopath-to-crm-accepting-emailed-infopath-forms/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Changing OptionSet Values on Yes/No Form Field Change</title>
		<link>http://mscrmblogger.com/2011/11/01/crm2011optionsetvalues/</link>
		<comments>http://mscrmblogger.com/2011/11/01/crm2011optionsetvalues/#comments</comments>
		<pubDate>Tue, 01 Nov 2011 16:17:12 +0000</pubDate>
		<dc:creator>Carlton Colter</dc:creator>
				<category><![CDATA[Extensions]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[Web Resources]]></category>
		<category><![CDATA[boolean]]></category>
		<category><![CDATA[crm]]></category>
		<category><![CDATA[crm 2011]]></category>
		<category><![CDATA[CRM Online]]></category>
		<category><![CDATA[crm2011]]></category>
		<category><![CDATA[field]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jscript]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[microsoft dynamics crm]]></category>
		<category><![CDATA[Microsoft Dynamics CRM 2011]]></category>
		<category><![CDATA[onchange]]></category>
		<category><![CDATA[picklist]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[Web Resource]]></category>

		<guid isPermaLink="false">http://mscrmblogger.com/?p=870</guid>
		<description><![CDATA[<p>Recently someone posted a question about the article I had written for CRM 4 as an example.  Here is the 2011 version to hopefully help people find the proper information.</p>]]></description>
			<content:encoded><![CDATA[<p>Recently I&#8217;ve received some questions on how to change the optionset (previously called picklist) values in CRM 2011 the same way I did in my <a href="http://mscrmblogger.com/2009/10/03/changing-picklist-values-on-yesno-form-field-change/">previous CRM 4 post</a> about using the OnChange event of a yes/no field to change the values of a picklist.</p>
<p><a href="http://msdn.microsoft.com/en-us/library/gg334266.aspx">This technet page shows the methods available for the different types of controls.</a></p>
<p><strong>Here is an example of how to change the optionset based on a yes/no field:</strong></p>
<pre name="code" class="javascript">
function BuildOptionSet(picklist, valuelist)
{
	picklist.clearOptions();
	for(var i=0; i&lt;valuelist.length; i++)
	{
		var listitem = valuelist[i];
		picklist.addOption(listitem[0], listitem[1]);
	}
}

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

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

var uselista = Xrm.Page.data.entity.attributes.get("YesNo").getValue();
var optionset = Xrm.Page.ui.controls.get("OptionSet");

if (uselista==true) {
   BuildOptionSet(optionset, lista);
} else {
   BuildOptionSet(optionset, listb);
}
</pre>
<p>You just need to change the values of lista and listb and the YesNo and OptionSet lines to reflect the fields on your form, and add it to the onchange event of the dropdown Yes/No form field.</p>
]]></content:encoded>
			<wfw:commentRss>http://mscrmblogger.com/2011/11/01/crm2011optionsetvalues/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CRM 2011: Slider Form Control Web Resource</title>
		<link>http://mscrmblogger.com/2011/10/04/crmslider/</link>
		<comments>http://mscrmblogger.com/2011/10/04/crmslider/#comments</comments>
		<pubDate>Tue, 04 Oct 2011 20:23:11 +0000</pubDate>
		<dc:creator>Carlton Colter</dc:creator>
				<category><![CDATA[API-SDK]]></category>
		<category><![CDATA[Extensions]]></category>
		<category><![CDATA[General API Usage]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[Web Resources]]></category>
		<category><![CDATA[control]]></category>
		<category><![CDATA[crm]]></category>
		<category><![CDATA[crm 2011]]></category>
		<category><![CDATA[CRM Online]]></category>
		<category><![CDATA[crm2011]]></category>
		<category><![CDATA[forms]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jscript]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[microsoft dynamics crm]]></category>
		<category><![CDATA[Microsoft Dynamics CRM 2011]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[slidebar]]></category>
		<category><![CDATA[slider]]></category>
		<category><![CDATA[Web Resource]]></category>

		<guid isPermaLink="false">http://mscrmblogger.com/?p=804</guid>
		<description><![CDATA[Here is another web-resource html control to expand your CRM forms.  It is a slider control for CRM with step control.  It provides a graphical way to slide a value up and down.]]></description>
			<content:encoded><![CDATA[<p>Here is another web-resource html control to expand your CRM forms.  It is a slider control for CRM with step control.</p>
<p>Below is an example of the slider control in action, and you can <a href='http://mscrmblogger.com/wp-content/uploads/2011/10/SliderFormControl_1_0_managed.zip'>click here to download a solution</a> to import the web-resources into your CRM deployment (it is a managed solution with no entities, just like the <a href="http://mscrmblogger.com/2011/09/20/crm-2011-star-rating-control/" target="_blank">star rating control</a>).  If you want the unmanaged solution you can download it <a href='http://mscrmblogger.com/wp-content/uploads/2011/10/SliderFormControl_1_0.zip'>here</a>.</p>
<p><a href="http://mscrmblogger.com/wp-content/uploads/2011/10/slider2.png"><img src="http://mscrmblogger.com/wp-content/uploads/2011/10/slider2.png" alt="" title="Slider Form Control" width="816" height="360" class="aligncenter size-full wp-image-806" /></a></p>
<p>Ok, once you have the web-resource loaded into your CRM deployment, you&#8217;ll need to create a new field to store the slider value and put it on the form, but uncheck the visible box.</p>
<p>Add the Web-Resource to the form, check the box to <strong>display the label on the form</strong>, and then enter your parameters.  I like using a pipe separator.  The available parameters are:</p>
<ul>
<li><strong>min</strong>: minimum value</li>
<li><strong>max</strong>: maximum value</li>
<li><strong>field</strong>: the CRM field to store the value in</li>
<li><strong>step</strong>: the step increase</li>
<li><strong>stepoverride</strong>: don&#8217;t use the step when a value is manually entered</li>
</ul>
<p><a href="http://mscrmblogger.com/wp-content/uploads/2011/10/Slider_Score_Resource1.png"><img src="http://mscrmblogger.com/wp-content/uploads/2011/10/Slider_Score_Resource1.png" alt="" title="Slider_Score_Resource1" width="486" height="615" class="aligncenter size-full wp-image-825" /></a></p>
<p>Make sure to click on the formatting tab and set the <strong>number of rows to 1</strong>, <strong>scrolling to Never</strong>, and uncheck the box to <strong>display the border</strong>.</p>
<p><a href="http://mscrmblogger.com/wp-content/uploads/2011/10/Slider_Score_Resource2.png"><img src="http://mscrmblogger.com/wp-content/uploads/2011/10/Slider_Score_Resource2.png" alt="" title="Slider_Score_Resource2" width="486" height="622" class="aligncenter size-full wp-image-826" /></a></p>
<p>Then move the hidden field down and out the way.  If you have a lot of fields you are accessing through form scripts you could just drop them into a hidden section.</p>
<p><a href="http://mscrmblogger.com/wp-content/uploads/2011/10/Score-Field.png"><img src="http://mscrmblogger.com/wp-content/uploads/2011/10/Score-Field.png" alt="" title="Score Field" width="544" height="251" class="aligncenter size-full wp-image-828" /></a></p>
<p>Here is the code for the slider control HTML web resource.</p>
<pre name="code" class="html">
&lt;html&gt;
&lt;head&gt;
  &lt;title&gt;Slider Control&lt;/title&gt;
  &lt;script type=&quot;text/javascript&quot;&gt;
var Settings = {};
Settings.Min = 0;
Settings.Max = 100;
Settings.Step = 5;
Settings.Value = 50;
Settings.Field = null;
Settings.AllowStepOverride = false;

var Slider = {};
Slider.Drag = false;

Slider.Initialize = function Slider(id,min,max,step,value,onchange) {
  Settings.Min = min;
  Settings.Max = max;
  Settings.Step = step;
  Slider.Value = value;

  var ihtml = &quot;&quot;;
  ihtml += &quot;&lt;table width='100%' cellspacing='0' cellpadding='0'&gt;&lt;tr&gt;&quot;;
  ihtml += &quot;&lt;td class='left-bracket'&gt;&amp;nbsp;&lt;/td&gt;&lt;td class='value'&gt;&quot;;
  ihtml += &quot;&lt;input class='value' id='value' type='text' onchange='Slider.SetValue();' /&gt;&quot;;
  ihtml += &quot;&lt;/td&gt;&lt;td class='right-bracket'&gt;&amp;nbsp;&lt;/td&gt;&quot;;
  ihtml += &quot;&lt;td class='slider-left' /&gt;&lt;td class='slider-area' id='&quot;+id+&quot;-area' &gt;&quot;;
  ihtml += &quot;&lt;div id='&quot;+id+&quot;-selector' class='slider-selector' /&gt;&lt;/td&gt;&quot;
  ihtml += &quot;&lt;td class=&#39;slider-right&#39; /&gt;&lt;/tr&gt;&lt;/td&gt;&lt;/table&gt;&quot;;
  var p = document.getElementById(id);
  p.innerHTML = ihtml;

  Slider.Element = document.getElementById(id+&quot;-area&quot;);
  Slider.Element.attachEvent(&quot;onmousedown&quot;,Slider.OnMouseClick);

  Slider.Id = id;
  Slider.Selector = document.getElementById(id+&quot;-selector&quot;);
  Slider.Selector.attachEvent(&quot;onmousedown&quot;,Slider.OnMouseDown);
  Slider.Selector.attachEvent(&quot;onmousemove&quot;,Slider.OnMouseMove);
  Slider.Selector.attachEvent(&quot;onmouseup&quot;,Slider.OnMouseUp);
  Slider.Selector.attachEvent(&quot;onmouseleave&quot;,Slider.OnMouseUp);

  Slider.UpdatePosition();
}

Slider.SliderRatio = function() {
  var num =  Settings.Max - Settings.Min;
  var den = parseFloat(Slider.Element.offsetWidth)-parseFloat(Slider.Selector.offsetWidth);
  return den/num;
};

Slider.UpdatePosition = function () {
  Slider.Position = Math.round(Slider.Value*Slider.SliderRatio());
  Slider.SetPosition();
};

Slider.OnMouseClick = function(e) {
  Slider.Position = e.clientX - (8+Slider.Selector.offsetLeft-parseInt(Slider.Selector.style.left.replace(&quot;px&quot;,&quot;&quot;)));
  Slider.SetPosition();
  Slider.OnChange(Slider.Value);
}

Slider.SetPosition = function() {
  if (Slider.Position&lt;0) Slider.Position = 0;
  var max = Slider.Element.offsetWidth-Slider.Selector.offsetWidth;
  if (Slider.Position&gt;max) Slider.Position = max;

  Slider.Selector.style.left = Slider.Position + &quot;px&quot;;

  var i = Math.round(Slider.Position/Slider.SliderRatio()) + Settings.Min;
  i = (Math.floor(i/Settings.Step))*Settings.Step;

  Slider.Value = i;

  var val = document.getElementById('value');
  if (val===null) return;
  val.value = i;
}

Slider.SetValue = function() {
  var val = document.getElementById('value');
  if (Settings.AllowStepOverride) {
    Slider.Value = val.value;
  } else {
    Slider.Value = Math.round(val.value/Settings.Step)*Settings.Step;
  }
  if (Slider.Value&lt;Settings.Min) Slider.Value = Settings.Min;
  if (Slider.Value&gt;Settings.Max) Slider.Value = Settings.Max;
  Slider.UpdatePosition();
}

Slider.OnMouseDown = function(e)  {
  Slider.Drag = true;
}

Slider.OnMouseMove = function(e) {
  if (Slider.Drag) {
    Slider.Position = e.clientX - (8+Slider.Selector.offsetLeft-parseInt(Slider.Selector.style.left.replace(&quot;px&quot;,&quot;&quot;)));
    Slider.SetPosition();
  }
};

Slider.OnMouseUp = function(e) {
  if (Slider.Drag) {
    Slider.Drag = false;
    Slider.OnChange(Slider.Value);
  }
};

Slider.OnChange = function(val) {
  parent.Xrm.Page.data.entity.attributes.get(Settings.Field).setValue(val);
};

var InitializeSlider = function() {
  var data = WebResource.GetDataParams();
  var sliderBox = document.getElementById(&quot;sliderBox&quot;);

  for (var i in data)
  {
    switch (data[i][0].toLowerCase())
	{
	  case 'min': Settings.Min = parseInt(data[i][1],10); break;
	  case 'max': Settings.Max = parseInt(data[i][1],10); break;
	  case 'step': Settings.Step = parseInt(data[i][1],10); break;
	  case 'value': Settings.Value = parseInt(data[i][1],10); break;
	  case 'field': Settings.Field = data[i][1]; break;
	  case 'stepoverride': Settings.AllowStepOverride = (data[i][1]=='true'); break;
	  default:break;
	}
  }

  Slider.Initialize(&quot;slider&quot;,Settings.Min,Settings.Max,Settings.Step,Settings.Value,null)

  if (Settings.Field!=null) {
    var val = parent.Xrm.Page.data.entity.attributes.get(Settings.Field).getValue();
	if (val!==null) {
	  var num = parseInt(val);
	  Slider.Value = num;
	  Slider.UpdatePosition();
	}
  }
};

var WebResource = {};

WebResource.GetDataParams = function()
{ //modified version of: http://technet.microsoft.com/en-us/library/gg327945.aspx
  //Get the any query string parameters and load them
  //into the vals array

  var vals = new Array();
  if (location.search !== &quot;&quot;)
  {
    vals = location.search.substr(1).split(&quot;&amp;&quot;);
    for (var i in vals)
    {
      vals[i] = vals[i].replace(/\+/g, &quot; &quot;).split(&quot;=&quot;);
    }

    //look for the parameter named 'data'
    var found = false;
        var datavals;
    for (var j in vals)
    {
      if (vals[j][0].toLowerCase() == &quot;data&quot;)
      {
        found = true;
        datavals = decodeURIComponent(vals[j][1]).split(&quot;|&quot;);
        for (var k in datavals)
        {
          datavals[k] = datavals[k].replace(/\+/g, &quot; &quot;).split(&quot;=&quot;);
        }
        break;
      }
    }
    if (found) { return datavals; }
  }
  return null;
};
  &lt;/script&gt;
  &lt;style type=&quot;text/css&quot;&gt;
html, body {
  padding: 0px;
  margin: 0px;
  border: 0px;
  background-color: rgb(246, 248, 250);
  overflow:hidden;
}
.slider-left, .slider-area, .slider-right {
  background-image:url(img/bar.png);
  background-repeat:repeat-x;
  height:17px;
  padding:0px;
  margin:0px;
  cursor:hand;
}
.slider-left, .slider-right {
  width: 7px;
}
div.slider-selector {
  background-image:url(img/selector.png);
  background-repeat:no-repeat;
  width:15px;
  height:17px;
  position: relative;
  cursor:hand;
}
td.left-bracket, td.right-bracket {
  background-repeat:no-repeat;
  height:17px;
  width:6px;
  display:inline;
}
td.left-bracket {
  background-image:url(img/left_bracket.png);

}
td.right-bracket {
  background-image:url(img/right_bracket.png);
}
td.value {
  background-image:url(img/val_bk.png);
  background-repeat:repeat-x;
  height:17px;
  width:30px;
}
input.value {
  border: none;
  font:11px segoe ui,tahoma,arial;
  height:17px;
  width:30px;
  background-color:transparent;
  vertical-align:top;
  padding-top:1px;
  text-align:center;
}
  &lt;/style&gt;
&lt;/head&gt;
&lt;body onload=&quot;InitializeSlider();&quot;&gt;
&lt;div id=&quot;slider&quot;&gt;&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>Thanks to <a href="http://manishmistry.wordpress.com" target="_blank">Manish Mistry</a> comment on my star control for refreshing the iframe when you change the value, you could do the same on this control.  However, you could also just set the value and update the position. The only reason I get the control&#8217;s object and it&#8217;s id is because I&#8217;m not 100% sure the id will always match, even though all my tests indicate it does.</p>
<pre name="code" class="javascript">
var control = Xrm.Page.ui.controls.get('WebResource_Score');
var id      = control.getObject().id;
var frame   = document.frames[id];
frame.Slider.Value = 50;
frame.Slider.UpdatePosition();
</pre>
<p>One of my TODO&#8217;s is to join my web resources HTML form controls into a single library once I have enough of them so that people can leverage them in an easier manner.  If anyone has any ideas for custom controls for Dynamics CRM, let me know and I&#8217;ll see if I can build it.</p>
]]></content:encoded>
			<wfw:commentRss>http://mscrmblogger.com/2011/10/04/crmslider/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>CRM 2011: Star Rating Form Control</title>
		<link>http://mscrmblogger.com/2011/09/20/crm-2011-star-rating-control/</link>
		<comments>http://mscrmblogger.com/2011/09/20/crm-2011-star-rating-control/#comments</comments>
		<pubDate>Wed, 21 Sep 2011 02:06:14 +0000</pubDate>
		<dc:creator>Carlton Colter</dc:creator>
				<category><![CDATA[API-SDK]]></category>
		<category><![CDATA[Extensions]]></category>
		<category><![CDATA[General API Usage]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[Scoperta]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[Web Resources]]></category>
		<category><![CDATA[control]]></category>
		<category><![CDATA[crm]]></category>
		<category><![CDATA[crm 2011]]></category>
		<category><![CDATA[forms]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jscript]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[microsoft dynamics crm]]></category>
		<category><![CDATA[Microsoft Dynamics CRM 2011]]></category>
		<category><![CDATA[rating]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[Web Resource]]></category>

		<guid isPermaLink="false">http://mscrmblogger.com/2011/09/20/crm-2011-star-rating-control/</guid>
		<description><![CDATA[<p>Another HTML control example that adds a star rating form control to CRM 2011 as a web-resource.  It includes a managed solution that you can download and install as well as the source and all images used in creating the solution.</p>]]></description>
			<content:encoded><![CDATA[<p>To expand on the technique used for my previous post on <a href="http://mscrmblogger.com/2011/05/11/crm-2011-making-the-state-field-a-drop-down/">making the state field a drop down</a> here is a star rating field control so that you can get a nice visual star rating control that is configurable.</p>
<p>Below is an example of the star rating control in action, and you can <a href='http://mscrmblogger.com/wp-content/uploads/2011/09/RatingControl_1_0_managed.zip'>click here to download a solution</a> to import the web-resources into your CRM deployment (it is a managed solution with no entities).</p>
<p><img src="http://mscrmblogger.com/wp-content/uploads/2011/09/092111_0206_CRM2011Star11.png" alt="" class="aligncenter size-full" /></p>
<p>Ok, once you have the web-resource loaded into your CRM deployment, you&#8217;ll need to create a new field to store the rating and put it on the form, but uncheck the visible box.</p>
<p><img src="http://mscrmblogger.com/wp-content/uploads/2011/09/092111_0206_CRM2011Star21.png" alt="" class="aligncenter size-full" /></p>
<p>Add the Web-Resource to the form, and check the box to <strong>display the label on the form</strong>.</p>
<p>Then enter your parameters (I like using a pipe as a separator).  The available parameters are:</p>
<ul>
<li><strong>min</strong>: minimum star rating – really should always be 1</li>
<li><strong>max</strong>: maximum star rating – maybe 5 or 10</li>
<li><strong>field</strong>: the CRM field to store the value in</li>
<li><strong>showvalue</strong>: whether or not to display the numerical value</li>
</ul>
<p><img src="http://mscrmblogger.com/wp-content/uploads/2011/09/092111_0206_CRM2011Star31.png" alt="" class="aligncenter size-full" /></p>
<p>Make sure to click on the formatting tab and set the <strong>number of rows to 1</strong>, <strong>scrolling to Never</strong>, and check the box to <strong>display the border</strong>.</p>
<p><img src="http://mscrmblogger.com/wp-content/uploads/2011/09/092111_0206_CRM2011Star41.png" alt="" class="aligncenter size-full" /></p>
<p>Then move the hidden field down and out the way.  If you have a lot of fields you are accessing through form scripts you could just drop them into a hidden section.</p>
<p><img src="http://mscrmblogger.com/wp-content/uploads/2011/09/092111_0206_CRM2011Star51.png" alt="" class="aligncenter size-full" /></p>
<p>Now that you know how to set it up, let&#8217;s go ahead and look at the code.</p>
<pre name="code" class="html">
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
&lt;head&gt;
  &lt;title&gt;Rating Control&lt;/title&gt;
  &lt;link href=&quot;/_common/styles/fonts.css.aspx?lcid=1033&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot; /&gt;
  &lt;link href=&quot;/_common/styles/global.css.aspx?lcid=1033&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot; /&gt;
  &lt;link href=&quot;/_common/styles/select.css.aspx?lcid=1033&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot; /&gt;
  &lt;script type=&quot;text/javascript&quot;&gt;
var Rating = {};

// Default Values
Rating.ShowValue = false;
Rating.Max = 5;
Rating.Min = 1;
Rating.Field = '';
Rating.Value = null;

Rating.Highlight = function(index)
{
  var val = document.getElementById('rating_value');
  if (index===null) {
    index = -1;
    val.innerHTML = '[Not Set]';
  } else {
    val.innerHTML = '['+index+'/'+Rating.Max+']';
  }
  for(var i=Rating.Min; i&lt;=Rating.Max;i++)
  {
    if (i&lt;=index) {
      document.getElementById(&quot;rating_&quot;+i).className = &quot;staron&quot;;
    } else {
      document.getElementById(&quot;rating_&quot;+i).className = &quot;staroff&quot;;
    }
  }
};

Rating.HolderRollover = function() {
  if (Rating.Value !== null) {
    var r = document.getElementById('rating_remove');
    r.className = 'stardel';
  }
};

Rating.HolderRollout = function() {
  var r = document.getElementById('rating_remove');
  r.className = 'stardel2';
};

Rating.Rollover = function(index) {
  Rating.Highlight(index);
};

Rating.Rollout = function()
  {
  Rating.Highlight(Rating.Value);
};

Rating.Set = function(index) {
  Rating.Value = index;
  if (index&lt;0) {
    parent.Xrm.Page.data.entity.attributes.get(Rating.Field).setValue(null);
  } else {
    parent.Xrm.Page.data.entity.attributes.get(Rating.Field).setValue(Rating.Value);
  }
  Rating.Highlight(index);
};

Rating.Initialize = function() {
  var data = WebResource.GetDataParams();
  for (var i in data)
  {
    switch (data[i][0].toLowerCase())
        {
          case 'min': Rating.Min = parseInt(data[i][1],10); break;
          case 'max': Rating.Max = parseInt(data[i][1],10); break;
          case 'field': Rating.Field = data[i][1]; break;
          case 'showvalue': Rating.ShowValue = (data[i][1] == 'true'); break;
          default:break;
        }
  }
  var d = document.getElementById('starholder');
  for (var j=Rating.Min;j&lt;=Rating.Max;j++)
  {
    var el = &quot;&lt;span id='rating_&quot;+j+&quot;' &quot; +
             &quot;onmouseover='Rating.Rollover(&quot;+j+&quot;);' &quot; +
             &quot;onmouseout='Rating.Rollout();' &quot; +
             &quot;onclick='Rating.Set(&quot;+j+&quot;);' /&gt;&quot;;
    d.innerHTML += el;
  }

  var rel = &quot;&lt;span id='rating_remove' class='stardel2' &quot; +
            &quot;onclick='Rating.Set(null);' onmouseover='Rating.Rollover(null);' /&gt;&quot;;
  d.innerHTML += rel;

  var val = &quot;&lt;span id='rating_value' class='value' &quot; +
            (Rating.ShowValue ? &quot;&quot; : &quot;style='display:none' &quot;) +
            &quot;/&gt;&quot;;
  d.innerHTML += val; 

  Rating.Value = parent.Xrm.Page.data.entity.attributes.get(Rating.Field).getValue();
  Rating.Highlight(Rating.Value);
};

var WebResource = {};

WebResource.GetDataParams = function()
{ //modified version of: http://technet.microsoft.com/en-us/library/gg327945.aspx
  //Get the any query string parameters and load them
  //into the vals array

  var vals = new Array();
  if (location.search !== &quot;&quot;)
  {
    vals = location.search.substr(1).split(&quot;&amp;&quot;);
    for (var i in vals)
    {
      vals[i] = vals[i].replace(/\+/g, &quot; &quot;).split(&quot;=&quot;);
    }

    //look for the parameter named 'data'
    var found = false;
        var datavals;
    for (var j in vals)
    {
      if (vals[j][0].toLowerCase() == &quot;data&quot;)
      {
        found = true;
        datavals = decodeURIComponent(vals[j][1]).split(&quot;|&quot;);
        for (var k in datavals)
        {
          datavals[k] = datavals[k].replace(/\+/g, &quot; &quot;).split(&quot;=&quot;);
        }
        break;
      }
    }
    if (found) { return datavals; }
  }
  return null;
};
  &lt;/script&gt;
  &lt;style type=&quot;text/css&quot;&gt;
.staroff,.staron,.stardel,.stardel2 {
	height:15px;
	margin:1px 1px 1px 2px;
	width:15px
}

staroff,.staron,.stardel {
	background:no-repeat
}

.starholder {
	background:#FFF;
	padding:1px
}

.stardel {
	background:url(remove.png)
}

.staron {
	background:url(staron.png)
}

.staron,.staroff {
	margin-right:5px
}

.staroff {
	background:url(staroff.png)
}

.value {
	font:11px segoe ui,tahoma,arial;
	color:#000;
	height:17px;
	vertical-align:middle
}
  &lt;/style&gt;
&lt;/head&gt;

&lt;body onload=&quot;Rating.Initialize();&quot;&gt;
  &lt;table cellspacing=&quot;0&quot;
         cellpadding=&quot;0&quot;
         width=&quot;100%&quot;
         summary=&quot;star table&quot;&gt;
    &lt;tr&gt;
      &lt;td id=&quot;cell_dd&quot;&gt;
        &lt;div id=&quot;starholder&quot;
             style=&quot;width:100%;&quot;
             onmouseover=&quot;Rating.HolderRollover();&quot;
             onmouseout=&quot;Rating.HolderRollout();&quot;&gt;&lt;/div&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<table>
<tr>
<td>These are the image files used:</td>
<td>
<a href="http://mscrmblogger.com/wp-content/uploads/2011/09/msblog_ratingstaron.png"><img src="http://mscrmblogger.com/wp-content/uploads/2011/09/msblog_ratingstaron.png" alt="" title="msblog_ratingstaron" width="15" height="15" class="aligncenter size-full wp-image-785" /></a></td>
<td>
<a href="http://mscrmblogger.com/wp-content/uploads/2011/09/msblog_ratingstaroff.png"><img src="http://mscrmblogger.com/wp-content/uploads/2011/09/msblog_ratingstaroff.png" alt="" title="msblog_ratingstaroff" width="15" height="15" class="aligncenter size-full wp-image-784" /></a></td>
<td>
<a href="http://mscrmblogger.com/wp-content/uploads/2011/09/msblog_ratingremove.png"><img src="http://mscrmblogger.com/wp-content/uploads/2011/09/msblog_ratingremove.png" alt="" title="msblog_ratingremove" width="15" height="15" class="aligncenter size-full wp-image-783" /></a></td>
<td>(<i>they are hidden in the managed solution</i>)</td>
</tr>
</table>
]]></content:encoded>
			<wfw:commentRss>http://mscrmblogger.com/2011/09/20/crm-2011-star-rating-control/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>CRM 2011: Making the state field a drop down</title>
		<link>http://mscrmblogger.com/2011/05/11/crm-2011-making-the-state-field-a-drop-down/</link>
		<comments>http://mscrmblogger.com/2011/05/11/crm-2011-making-the-state-field-a-drop-down/#comments</comments>
		<pubDate>Wed, 11 May 2011 16:08:12 +0000</pubDate>
		<dc:creator>Carlton Colter</dc:creator>
				<category><![CDATA[Extensions]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[Web Resources]]></category>
		<category><![CDATA[crm 2011]]></category>
		<category><![CDATA[dropdown]]></category>
		<category><![CDATA[editable dropdown]]></category>
		<category><![CDATA[form]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jscript]]></category>
		<category><![CDATA[microsoft dynamics crm]]></category>
		<category><![CDATA[Microsoft Dynamics CRM 2011]]></category>
		<category><![CDATA[state]]></category>
		<category><![CDATA[stateorprovince]]></category>
		<category><![CDATA[Web Resource]]></category>

		<guid isPermaLink="false">http://mscrmblogger.com/?p=456</guid>
		<description><![CDATA[<p><b>Do you want a drop down box for the state, but you don't want to break core CRM functionality?</b></p>
<p>In CRM 4.0 I had posted an article about <a href="http://mscrmblogger.com/2009/05/11/change-crm-address-state-to-a-drop-down-box/">changing the CRM Address State to a drop down box</a>.  I had modified the script to work with CRM 2011, but it was still unsupported and some people had problems making it work on other entities.  This version uses an HTML web resource and uses all supported methods.</p>]]></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>In CRM 4.0 I had posted an article about <a href="http://mscrmblogger.com/2009/05/11/change-crm-address-state-to-a-drop-down-box/">changing the CRM Address State to a drop down box</a>.  While I had gone back and updated the code to work with CRM 2011, some people still had problems using it.  Also, it wasn&#8217;t the best way to do it in CRM 2011.  With the web-resources in CRM 2011 there is a completely supported way of creating a state drop down!</p>
<p>You can use an html web-resource!  Below is the source for the html web resource, then you just need to hide the state drop down on your form and add the web resource and set the data parameter to the field id, such as address1_stateorprovince.</p>
<p>This helps by providing a set of default option in a drop-down box.  It also supports (** Other) which will show a text box for entering something not in the drop down.  This web-resource works on any entity where you need a state drop down.</p>
<p><b>Here is the configuration for the HTML Web Resource on the Contact form:</b></p>
<div id="attachment_473" class="wp-caption aligncenter" style="width: 794px"><a href="http://mscrmblogger.com/wp-content/uploads/2011/05/statedropdown-resource-properties.png"><img src="http://mscrmblogger.com/wp-content/uploads/2011/05/statedropdown-resource-properties.png" alt="State Drop Down Web Resource Properties" title="State Drop Down Web Resource Properties" width="784" height="597" class="size-full wp-image-473" /></a><p class="wp-caption-text">State Drop Down Web Resource Properties</p></div>
<p><b>StateDropDown.html:</b></p>
<pre name="code" class="html">
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;State Drop Down&lt;/title&gt;
&lt;link href=&quot;/_common/styles/fonts.css.aspx?lcid=1033&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot;/&gt;
&lt;link href=&quot;/_common/styles/global.css.aspx?lcid=1033&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot;/&gt;
&lt;link href=&quot;/_common/styles/select.css.aspx?lcid=1033&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot;/&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
var datafield;
function getQuerystring(key)
{
    var work = key.replace(/[\[]/,&quot;\\\[&quot;).replace(/[\]]/,&quot;\\\]&quot;);
    var regex = new RegExp(&quot;[\\?&amp;]&quot;+work+&quot;=([^&amp;#]*)&quot;);
    var qs = regex.exec(window.location.href);
    if (qs == null) return null;
    return qs[1];
}

function onLoad()
{
    var updatefield = getQuerystring(&quot;data&quot;);
    if (top.location.href.indexOf(&quot;/print/print.aspx&quot;) != -1)
    {
        // While this part of the onload is unsupported, there is not another way to do it
        // because Xrm.Page.data.entity is not supported in print-preview
        var data_cell = document.getElementById(&quot;cell_dd&quot;);
        var crmField = top.frames[0].document.getElementById(updatefield+&quot;_d&quot;);
        data_cell.innerHTML = crmField.innerHTML;
        return;
    }
    datafield = parent.Xrm.Page.data.entity.attributes.get(updatefield);
    var value = datafield.getValue();
    var dropdown = document.getElementById(&quot;custom_state&quot;);

    // fix for outlook issue identified by Ryan
    // do not pass null value to IsOptionInDropDown
    if (value==null || value==&quot;&quot;) {
        dropdown.value = &quot;!1&quot;; // Set to select a state by default
        return; // stop processing - empty value
    }

    // If it is a dropdown value, set the dropdown and exit onload
    if (IsOptionInDropDown(value)) return;

    // no value found for drop down, value is not null or empty, therefor set other
    dropdown.value = &quot;other&quot;;
    var other = document.getElementById(&quot;other_state&quot;);
    other.value = value;
    SetOtherVisible(true);
}

function IsOptionInDropDown(value)
{
    // Replace fixes for common name variants
    switch (value) {
        case "d.c.": value="dc";
            break;
        default:
            break;
    }

    // Change to lowercase
    var lvalue=value.toLowerCase();

    var dropdown = document.getElementById("custom_state");
    var options = dropdown.options;
    var i = 0;
    for(i=0;i&lt;options .length;i++)
    {
        if (options[i].value.toLowerCase() == lvalue || options[i].text.toLowerCase() == lvalue)
        {
            dropdown.value = options[i].value;
            SetOtherVisible(false);
            return true;
        }
    }
    return false;
}

function DropDownChanged(value)
{
    if (value !== null &amp;&amp; value!==&quot;&quot;) {
        if (value.charAt(0)=='!') { // Fixed By Joseph Martine - charAt or substring not [0]
            StateChanged(&quot;&quot;);// Fixed By Joseph Martine
            return;
        }
        if (value==&quot;other&quot;) {
            SetOtherVisible(true);
            return;
        } else {
            SetOtherVisible(false); // Fixed By Joseph Martine
        }
        var os = document.getElementById(&quot;other_state&quot;);
        os.value = value;
        StateChanged(value);
    }
}

function TextBoxChanged(value)
{
    StateChanged(value);
    IsOptionInDropDown(value); // This also updates the visible in its check...
    return true;
}

function StateChanged(value)
{
    // Update Xrm.Page data field
    datafield.setValue(value);
}

function SetOtherVisible(visibility)
{
    var ddcell = document.getElementById(&quot;cell_dd&quot;);
    var textcell = document.getElementById(&quot;cell_tb&quot;);
    if (visibility) {
        ddcell.style.width=&quot;50%&quot;;
        textcell.style.display=&quot;table-cell&quot;;
        textcell.style.width=&quot;50%&quot;;
    } else {
        textcell.style.display=&quot;none&quot;;
        ddcell.style.width=&quot;100%&quot;;
        textcell.style.width=&quot;100%&quot;;
    }
}

&lt;/script&gt;
&lt;/head&gt;
&lt;body onload=&quot;onLoad();&quot;&gt;
&lt;table cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; width=&quot;100%&quot;&gt;&lt;tr&gt;&lt;td id=&quot;cell_dd&quot;&gt;
&lt;select name=&quot;custom_state&quot; req=&quot;2&quot; height=&quot;4&quot; style=&quot;IME-MODE: auto;width:100%;&quot;
class=&quot;ms-crm-SelectBox&quot; id=&quot;custom_state&quot; onChange=&quot;DropDownChanged(this.value)&quot;&gt;
    &lt;option value=&quot;!1&quot;&gt;---Select a State-------&lt;/option&gt;
    &lt;option value=&quot;!2&quot;&gt;---Canada---------------&lt;/option&gt;
    &lt;option value=&quot;AB&quot;&gt;Alberta&lt;/option&gt;
    &lt;option value=&quot;BC&quot;&gt;British Columbia&lt;/option&gt;
    &lt;option value=&quot;MB&quot;&gt;Manitoba&lt;/option&gt;
    &lt;option value=&quot;NB&quot;&gt;New Brunswick&lt;/option&gt;
    &lt;option value=&quot;NL&quot;&gt;Newfoundland and Labrador&lt;/option&gt;
    &lt;option value=&quot;NT&quot;&gt;Northwest Territories&lt;/option&gt;
    &lt;option value=&quot;NS&quot;&gt;Nova Scotia&lt;/option&gt;
    &lt;option value=&quot;NU&quot;&gt;Nunavut&lt;/option&gt;
    &lt;option value=&quot;ON&quot;&gt;Ontario&lt;/option&gt;
    &lt;option value=&quot;PE&quot;&gt;Prince Edward Island&lt;/option&gt;
    &lt;option value=&quot;QC&quot;&gt;Quebec&lt;/option&gt;
    &lt;option value=&quot;SK&quot;&gt;Saskatchewan&lt;/option&gt;
    &lt;option value=&quot;YT&quot;&gt;Yukon&lt;/option&gt;
  &lt;option value=&quot;!3&quot;&gt;---United States--------&lt;/option&gt;
    &lt;option value=&quot;AL&quot;&gt;Alabama&lt;/option&gt;
    &lt;option value=&quot;AK&quot;&gt;Alaska&lt;/option&gt;
    &lt;option value=&quot;AZ&quot;&gt;Arizona&lt;/option&gt;
    &lt;option value=&quot;AR&quot;&gt;Arkansas&lt;/option&gt;
    &lt;option value=&quot;CA&quot;&gt;California&lt;/option&gt;
    &lt;option value=&quot;CO&quot;&gt;Colorado&lt;/option&gt;
    &lt;option value=&quot;CT&quot;&gt;Connecticut&lt;/option&gt;
    &lt;option value=&quot;DC&quot;&gt;District of Columbia&lt;/option&gt;
    &lt;option value=&quot;DE&quot;&gt;Delaware&lt;/option&gt;
    &lt;option value=&quot;FL&quot;&gt;Florida&lt;/option&gt;
    &lt;option value=&quot;GA&quot;&gt;Georgia&lt;/option&gt;
    &lt;option value=&quot;HI&quot;&gt;Hawaii&lt;/option&gt;
    &lt;option value=&quot;ID&quot;&gt;Idaho&lt;/option&gt;
    &lt;option value=&quot;IL&quot;&gt;Illinois&lt;/option&gt;
    &lt;option value=&quot;IN&quot;&gt;Indiana&lt;/option&gt;
    &lt;option value=&quot;IA&quot;&gt;Iowa&lt;/option&gt;
    &lt;option value=&quot;KS&quot;&gt;Kansas&lt;/option&gt;
    &lt;option value=&quot;KY&quot;&gt;Kentucky&lt;/option&gt;
    &lt;option value=&quot;LA&quot;&gt;Louisiana&lt;/option&gt;
    &lt;option value=&quot;ME&quot;&gt;Maine&lt;/option&gt;
    &lt;option value=&quot;MD&quot;&gt;Maryland&lt;/option&gt;
    &lt;option value=&quot;MA&quot;&gt;Massachusetts&lt;/option&gt;
    &lt;option value=&quot;MI&quot;&gt;Michigan&lt;/option&gt;
    &lt;option value=&quot;MN&quot;&gt;Minnesota&lt;/option&gt;
    &lt;option value=&quot;MS&quot;&gt;Mississippi&lt;/option&gt;
    &lt;option value=&quot;MO&quot;&gt;Missouri&lt;/option&gt;
    &lt;option value=&quot;MT&quot;&gt;Montana&lt;/option&gt;
    &lt;option value=&quot;NE&quot;&gt;Nebraska&lt;/option&gt;
    &lt;option value=&quot;NV&quot;&gt;Nevada&lt;/option&gt;
    &lt;option value=&quot;NH&quot;&gt;New Hampshire&lt;/option&gt;
    &lt;option value=&quot;NJ&quot;&gt;New Jersey&lt;/option&gt;
    &lt;option value=&quot;NM&quot;&gt;New Mexico&lt;/option&gt;
    &lt;option value=&quot;NY&quot;&gt;New York&lt;/option&gt;
    &lt;option value=&quot;NC&quot;&gt;North Carolina&lt;/option&gt;
    &lt;option value=&quot;ND&quot;&gt;North Dakota&lt;/option&gt;
    &lt;option value=&quot;OH&quot;&gt;Ohio&lt;/option&gt;
    &lt;option value=&quot;OK&quot;&gt;Oklahoma&lt;/option&gt;
    &lt;option value=&quot;OR&quot;&gt;Oregon&lt;/option&gt;
    &lt;option value=&quot;PA&quot;&gt;Pennsylvania&lt;/option&gt;
    &lt;option value=&quot;RI&quot;&gt;Rhode Island&lt;/option&gt;
    &lt;option value=&quot;SC&quot;&gt;South Carolina&lt;/option&gt;
    &lt;option value=&quot;SD&quot;&gt;South Dakota&lt;/option&gt;
    &lt;option value=&quot;TN&quot;&gt;Tennessee&lt;/option&gt;
    &lt;option value=&quot;TX&quot;&gt;Texas&lt;/option&gt;
    &lt;option value=&quot;UT&quot;&gt;Utah&lt;/option&gt;
    &lt;option value=&quot;VT&quot;&gt;Vermont&lt;/option&gt;
    &lt;option value=&quot;VA&quot;&gt;Virginia&lt;/option&gt;
    &lt;option value=&quot;WA&quot;&gt;Washington&lt;/option&gt;
    &lt;option value=&quot;WV&quot;&gt;West Virginia&lt;/option&gt;
    &lt;option value=&quot;WI&quot;&gt;Wisconsin&lt;/option&gt;
    &lt;option value=&quot;WY&quot;&gt;Wyoming&lt;/option&gt;
    &lt;option value=&quot;other&quot;&gt;** Other&lt;/option&gt;
&lt;/select&gt;
&lt;/td&gt;&lt;td id=&quot;cell_tb&quot; style=&quot;padding-left:15px;display:none;&quot;&gt;
&lt;input class=&quot;ms-crm-Text&quot; style=&quot;ime-mode: auto;width:100%;&quot; type=&quot;text&quot;
maxLength=&quot;50&quot; id=&quot;other_state&quot; req=&quot;0&quot; onChange=&quot;TextBoxChanged(this.value);&quot; /&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://mscrmblogger.com/2011/05/11/crm-2011-making-the-state-field-a-drop-down/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<item>
		<title>CRM 2011: Required Fields</title>
		<link>http://mscrmblogger.com/2011/04/10/crm-2011-required-fields/</link>
		<comments>http://mscrmblogger.com/2011/04/10/crm-2011-required-fields/#comments</comments>
		<pubDate>Sun, 10 Apr 2011 22:00:53 +0000</pubDate>
		<dc:creator>Carlton Colter</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[crm]]></category>
		<category><![CDATA[crm 2011]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jscript]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Microsoft Dynamics CRM 2011]]></category>
		<category><![CDATA[script]]></category>

		<guid isPermaLink="false">http://mscrmblogger.com/?p=410</guid>
		<description><![CDATA[This afternoon I was at the Microsoft Dynamics CRM booth at Convergence when someone asked me about required fields in CRM 2011. They wanted to know how to make fields required, but have different forms with different required fields for the same entity. First, in CRM 2011, when you make a field required by editing <a href='http://mscrmblogger.com/2011/04/10/crm-2011-required-fields/'>[...]</a>]]></description>
			<content:encoded><![CDATA[<p>This afternoon I was at the Microsoft Dynamics CRM booth at Convergence when someone asked me about required fields in CRM 2011.  They wanted to know how to make fields required, but have different forms with different required fields for the same entity.</p>
<p>First, in CRM 2011, when you make a field required by editing the field, you are making it required for all forms (that the field is on).</p>
<p><a href="http://mscrmblogger.com/wp-content/uploads/2011/04/RequirementLevel.png"><img src="http://mscrmblogger.com/wp-content/uploads/2011/04/RequirementLevel.png" alt="" title="Requirement Level when editing a field" width="653" height="267" class="aligncenter size-full wp-image-412" /></a></p>
<p>If you want to make a field required just for a particular form, you can use a form onload event to change the requirement level.  One of the great things about CRM 2011 is just how much is exposed to JScript through Xrm.Page.  There are <a href="http://msdn.microsoft.com/en-us/library/gg334409.aspx">methods on the attribute</a> to <a href="http://msdn.microsoft.com/en-us/library/gg334409.aspx#BKMK_getRequiredLevel">get the required level</a> and <a href="http://msdn.microsoft.com/en-us/library/gg334409.aspx#BKMK_setRequiredLevel">set the required level</a>.  Let&#8217;s suppose for example that we want to make the First Name required, but only on this form, then we could use the following onload function with the parameters &#8216;required&#8217;, &#8216;firstname&#8217; to make the field required.  If we needed to add multiple fields, then we would just specify them after &#8216;firstname&#8217;, such as &#8216;required&#8217;,'firstname&#8217;,'address1_city&#8217;.</p>
<pre name="code" class="javascript">
function updateRequirementLevel()
{
    var level = arguments[0];
    for (var i=1; i < arguments.length; i++)
    {
        var attribute = Xrm.Page.data.entity.attributes.get(arguments[i]);
        attribute.setRequiredLevel(level);
    }
}
</pre>
<p>That will make each field you specify after the level, required or recommended based on the level you pass as the first parameter.  Calling the function updateRequirementLevel('required','firstname'); will make the firstname required.</p>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://mscrmblogger.com/2011/04/10/crm-2011-required-fields/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>CRM 2011: Change CRM Address State to a drop-down</title>
		<link>http://mscrmblogger.com/2011/03/07/crm-2011-change-crm-address-state-to-a-drop-down/</link>
		<comments>http://mscrmblogger.com/2011/03/07/crm-2011-change-crm-address-state-to-a-drop-down/#comments</comments>
		<pubDate>Mon, 07 Mar 2011 15:01:33 +0000</pubDate>
		<dc:creator>Carlton Colter</dc:creator>
				<category><![CDATA[Extensions]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[Web Resources]]></category>
		<category><![CDATA[address]]></category>
		<category><![CDATA[contact]]></category>
		<category><![CDATA[crm]]></category>
		<category><![CDATA[crm 2011]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jscript]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Microsoft Dynamics CRM 2011]]></category>
		<category><![CDATA[onload event]]></category>
		<category><![CDATA[script]]></category>

		<guid isPermaLink="false">http://mscrmblogger.com/?p=338</guid>
		<description><![CDATA[<p><b>Do you want a drop down box for the state, but you don't want to break core CRM functionality?</b></p>
<p>In CRM 4.0 I had posted an article about <a href="http://mscrmblogger.com/2009/05/11/change-crm-address-state-to-a-drop-down-box/">changing the CRM Address State to a drop down box</a>.  I have went back and updated the code a little (fixed a bug) and it will work with CRM 2011.</p>
<p>Ok, this script uses the same methods I used in CRM 4 to 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 *'.</p>
<p>This script will work on the onload for a lead, contact, account, and sales-contact.</p>]]></description>
			<content:encoded><![CDATA[<p><b>This page is nolonger current.  Please view: <a href="http://mscrmblogger.com/2011/05/11/crm-2011-making-the-state-field-a-drop-down/">http://mscrmblogger.com/2011/05/11/crm-2011-making-the-state-field-a-drop-down/</a></b></p>
<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>In CRM 4.0 I had posted an article about <a href="http://mscrmblogger.com/2009/05/11/change-crm-address-state-to-a-drop-down-box/">changing the CRM Address State to a drop down box</a>.  I have went back and updated the code a little (fixed a bug) and it will work with CRM 2011.</p>
<p>Ok, this script uses the same methods I used in CRM 4 to 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>This script does use the _d html objects, which, to my knowledge is considered unsupported in CRM 2011.  CRM 2011 does expose these data elements and control elements using Xrm.Page and this script doesn&#8217;t use them, and I may come back and update this script, but since it works in CRM 4 and in CRM 2011, I figured I&#8217;d repost it.</p>
<p><b>StateDropDown.js 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;
    }

}
</pre>
<p>Then in the form&#8217;s onload event, just add the method <b>ddlState</b> with no parameters.</p>
]]></content:encoded>
			<wfw:commentRss>http://mscrmblogger.com/2011/03/07/crm-2011-change-crm-address-state-to-a-drop-down/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CRM 2011: Toggle Visibility (Hide/Show) &#8211; Visibility.js</title>
		<link>http://mscrmblogger.com/2011/02/24/crm-2011-toggle-visibility/</link>
		<comments>http://mscrmblogger.com/2011/02/24/crm-2011-toggle-visibility/#comments</comments>
		<pubDate>Thu, 24 Feb 2011 22:57:49 +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 2011]]></category>
		<category><![CDATA[forms]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jscript]]></category>
		<category><![CDATA[Microsoft Dynamics CRM 2011]]></category>
		<category><![CDATA[onchange]]></category>
		<category><![CDATA[onload event]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[toggle field]]></category>
		<category><![CDATA[visibility]]></category>

		<guid isPermaLink="false">http://mscrmblogger.com/?p=314</guid>
		<description><![CDATA[Visibility.js provides a simple way to set the visibility of navigation, section, tab, and form control elements using JScript.  It also allows you to change the visibility using the onClick event of a checkbox.]]></description>
			<content:encoded><![CDATA[<p>Often times we need to toggle the visiblity of an element based on the value of a field.  This JScript file adds the methods to accomplish this by calling the setVisibility function.  For example, in an onChange event for new_mypicklist I can call:</p>
<pre name="code" class="javascript">
setVisibility('section','new_mypicklist','section_main',42);
</pre>
<p>This hides the section_main section if the value is not 42, which simplifies the process of hiding and showing relevant information.</p>
<p>I also like to use checkboxes to handle the visibility of elements, but checkbox values dont&#8217; change until they lose focus.  To work around the issue, I register an onClick event during the form load instead of using the onChange event.  This unsupported method allows me to modify the value onClick and change the visibility immediately.  To use this I just need to call it in the onload:</p>
<pre name="code" class="javascript">
registerToggle('section', 'new_checkbox', 'section_main');
</pre>
<p>If you wanted to hide it when new_checkbox was false (or no), then you would need to specify the value, 0.</p>
<pre name="code" class="javascript">
registerToggle('section', 'new_checkbox', 'section_main',0);
</pre>
<p><b>JScript: visibility.js</b></p>
<p>This is the main javascript file&#8230; visibility.js that you would need to add and reference as a web resource in CRM 2011, everything except for the registerToggle uses supported methods.</p>
<pre name="code" class="javascript">
function registerToggle(t, attr, c, v) {
    if (typeof (v) === &quot;undefined&quot; || v === null) {
        var v = 1;
    }
    var ctrl = Xrm.Page.ui.controls.get(attr);
    var a = ctrl.getAttribute();
    var el = document.getElementById(attr);

    // Build Toggle Function
    var f = &quot;var ef=function() { &quot; +
              &quot;var a = Xrm.Page.data.entity.attributes.get(attr); &quot; +
              &quot;a.setValue(!a.getValue()); &quot; +
              &quot;setVisibility('&quot; + t + &quot;','&quot; + attr + &quot;','&quot; + c + &quot;',&quot; + v + &quot;);&quot; +
              &quot; };&quot;&#59;

    eval(f);

    // Attach to click event
    el.attachEvent('onclick', ef, false);

    // Set visibility
    setVisibility(t, attr, c, v);
}

function setVisibility(t, attr, c, v) {
    switch (t.toLowerCase().charAt(0)) {
        //tab
        case 't': setTabVisibility(attr, c, v);
            break;
        //section
        case 's': setSectionVisibility(attr, c, v);
            break;
        //control
        case 'c': setControlVisibility(attr, c, v);
            break;
        //navigation
        case 'n': setNavigationVisibility(attr, c, v);
            break;
    }
}

function setNavigationVisibility(attributename, navitemname, value) {
    var attribute = Xrm.Page.data.entity.attributes.get(attributename);
    var navitem = Xrm.Page.ui.navigation.items.get(navitemname);
    if (navitem === null)
    {
        return;
    }
    navitem.setVisible(attribute.getValue() == value);
}

function setTabVisibility(attributename, tabname, value) {
    var attribute = Xrm.Page.data.entity.attributes.get(attributename);
    var tab = Xrm.Page.ui.tabs.get(tabname);
    if (tab === null)
    {
        return;
    }
    tab.setVisible(attribute.getValue() == value);
}

function setSectionVisibility(attributename, sectionname, value) {
    var attribute = Xrm.Page.data.entity.attributes.get(attributename);
    var tabs = Xrm.Page.ui.tabs.get();
    for(var i in tabs) {
        var tab = tabs[i];
        var section = tab.sections.get(sectionname);
        if (section !== null) {
            section.setVisible(attribute.getValue() == value);
            return;
        }
    }
}

function setControlVisibility(attributename, controlname, value) {
    var attribute = Xrm.Page.data.entity.attributes.get(attributename);
    var control = Xrm.Page.ui.controls.get(controlname);
    if (control === null)
    {
        return;
    }
    control.setVisible(attribute.getValue() == value);
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://mscrmblogger.com/2011/02/24/crm-2011-toggle-visibility/feed/</wfw:commentRss>
		<slash:comments>28</slash:comments>
		</item>
		<item>
		<title>Link two CRM 2011 grids on a form together (parent-child-grids)</title>
		<link>http://mscrmblogger.com/2011/02/23/crm2011-parent-child-grids-jscript/</link>
		<comments>http://mscrmblogger.com/2011/02/23/crm2011-parent-child-grids-jscript/#comments</comments>
		<pubDate>Wed, 23 Feb 2011 19:47:34 +0000</pubDate>
		<dc:creator>Carlton Colter</dc:creator>
				<category><![CDATA[Extensions]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[Unsupported]]></category>
		<category><![CDATA[Web Resources]]></category>
		<category><![CDATA[crm]]></category>
		<category><![CDATA[crm 2011]]></category>
		<category><![CDATA[grid]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jscript]]></category>
		<category><![CDATA[Microsoft Dynamics CRM 2011]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[subgrid]]></category>

		<guid isPermaLink="false">http://mscrmblogger.com/?p=276</guid>
		<description><![CDATA[Have you ever tried to link the grids in CRM together?  CRM 2011 has the capability to add grids to a form and link them to the form object, but not to another grid.  This JScript will link one grid to another, refreshing the subgrid when a different row is selected on the parent grid.]]></description>
			<content:encoded><![CDATA[<p>Have you ever tried to link the grids in CRM together?  CRM 2011 has the capability to add grids to a form and link them to the form object, but not to another grid.  This JScript will link one grid to another, refreshing the subgrid when a different row is selected on the parent grid.</p>
<p>I have not figured out how to make the ribbon on the child grid know that it is a parented grid and have new items use the existing relationship maps.  If you figure out a way to make the ribbon work that way, please let me know.  This is an unsupported change, it does hack the CRM 2011 grid, but it does work and may save you from having to write a custom grid to drop on a page.</p>
<p><b>JScript Code:</b></p>
<p><i>Note: In CRM 2011 UR1, Xrm.Page.getControl(gridid) does not work for custom subgrids &#8211; the script below has been updated to work around this issue.</i></p>
<pre name="code" class="javascript">
var GridClick = function(pgrid,sgrid,sfield,defaultid)
{
    //var ids = Xrm.Page.getControl(pgrid)._control.get_innerControl().get_selectedIds();
    var ids = document.getElementById(pgrid).control.get_selectedIds();
    var id = defaultid;
    if (ids.length &gt; 0) {
        id = ids[0];
    }    

    var updateFetchXml = function(fetchxml, field, id)
    {
        var offset = 19;
        var fi = fetchxml.indexOf('&lt;condition attribute=&quot;'+field+'&quot; operator=&quot;eq&quot; ');
        var nfetchxml = '';
        var closings = '';
        if (fi &gt;= 0) {
            nfetchxml = fetchxml.substring(0,fi);
            offset = fetchxml.indexOf('/&gt;',fi)-fi+2;
        } else {
            fi = fetchxml.indexOf('&lt;filter type=&quot;and&quot;&gt;');
            if (fi &lt; 0) {
                offset = 0;
                fi = fetchxml.indexOf('&lt;entity');
                fi = fetchxml.indexOf('/&gt;',fi)+2;
                closings = '&lt;/filter&gt;';
            }
            nfetchxml = fetchxml.substring(0,fi);
            nfetchxml += '&lt;filter type=&quot;and&quot;&gt;';
        }

        nfetchxml += '&lt;condition attribute=&quot;' + field + '&quot; ';
		nfetchxml += 'operator=&quot;eq&quot; value=&quot;'+id+'&quot; /&gt;';
        nfetchxml += closings;
        nfetchxml += fetchxml.substring(fi+offset);
        return nfetchxml;
    };

    var updateXml = function(grid,xmlfield,subgridfield)
    {
        try {
            //var g = Xrm.Page.getControl(grid)._control.get_innerControl();
            var g = document.getElementById(grid).control;
            var a = g.getParameter(xmlfield)
            var b = updateFetchXml(a, subgridfield, id);
            g.setParameter(xmlfield,b);
        } catch (e) { }
    };

    // Update the fetchXml

    updateXml(sgrid,&quot;fetchXml&quot;,sfield);

    // It does not use either of these for filtering, but...
    // I'm not sure where else these could be referenced
    updateXml(sgrid,&quot;effectiveFetchXml&quot;,sfield);
    updateXml(sgrid,&quot;fetchXmlForFilters&quot;,sfield);

    // Refresh the grid
    //Xrm.Page.getControl(sgrid).refresh();
    document.getElementById(sgrid).control.refresh();

};

var registerSubGridUpdate = function(pgrid, sgrid, sfield) {
    var p = document.getElementById(pgrid);
    var s = document.getElementById(sgrid);
    if (p.readyState!=&quot;complete&quot; || s.readyState!=&quot;complete&quot;) {
        // delay one second and try again.  This is required due to grids becoming asynchronous
        setTimeout('registerSubGridUpdate(&quot;' + pgrid + '&quot;,&quot;'+sgrid+'&quot;,&quot;'+sfield+'&quot;)', 1000); //'
        return;
    }
    var el = document.getElementById(pgrid);

    // Build Toggle Function
    var f = &quot;var ef=function() { &quot; +
              &quot;GridClick('&quot;+pgrid+&quot;','&quot;+sgrid+&quot;','&quot;+sfield+
              &quot;','{00000000-0000-0000-0000-000000000000}');&quot;+
              &quot; };&quot;;

    eval(f);

    // Attach to click event
    el.attachEvent(&quot;onclick&quot;, ef, false);

    // Enable FetchXML in the grid
    document.getElementById(sgrid+'_filterSet').control.RefreshWithFilters();

    // Update to return nothing since it can't link to guid empty
    // or click on the first data row, selecting it.

    // The getControl for subgrids nolonger works in UR1.
    // var r = Xrm.Page.getControl(pgrid)._control.get_innerControl().get_allRecordIds();
    var r = document.getElementById(pgrid).control.get_allRecordIds();

    if (r.length==0) {
        GridClick(pgrid,sgrid,sfield, &quot;{00000000-0000-0000-0000-000000000000}&quot;);
    } else {
        var row = document.getElementById('checkBox_'+r[0]);
        do {
            row = row.parentNode;
        } while (row.nodeName !== &quot;TR&quot;);
        row.fireEvent(&quot;onclick&quot;);
    }
    try {
        document.getElementById(sgrid+&quot;_filterSet&quot;).control.ToggleFilters(true);
    } catch (e) { }
    //Xrm.Page.getControl(sgrid).refresh();
    document.getElementById(sgrid).control.refresh();
};
</pre>
<p>You will need to reference the JScript Web-Resource that you are storing the above script, and enter the parameters:
</p>
<ul>
<li>Parent Grid ID</li>
<li>Sub-Grid ID</li>
<li>Sub-Grid&#8217;s ID fieldname that refers to the parent grid&#8217;s primary id</li>
</ul>
<p>You can find the grid id in the grid properties.  It is the unique name of the grid.</p>
<div id="attachment_354" class="wp-caption aligncenter" style="width: 496px"><a href="http://mscrmblogger.com/wp-content/uploads/2011/03/gridid.png"><img src="http://mscrmblogger.com/wp-content/uploads/2011/03/gridid.png" alt="Grid ID Image" title="Grid Properties Grid ID Field" width="486" height="241" class="size-full wp-image-354" /></a><p class="wp-caption-text">This is where the grid&#039;s id field is in the grid properties.</p></div>
<p>If the parent&#8217;s primary id field is contactid, and the child grid&#8217;s lookup field is new_lookupid, then the sub-grid&#8217;s fieldname should be new_lookupid.</p>
<p>Below is a screenshot of how to setup the form onload.</p>
<p><a href="http://mscrmblogger.com/wp-content/uploads/2011/02/FormOnload.png"><img class="aligncenter size-medium wp-image-281" title="Form Onload Event" src="http://mscrmblogger.com/wp-content/uploads/2011/02/FormOnload.png" alt="Screenshot of the Form Onload Event" /></a></p>
<p><i>This does hack the sub-grid and is considered an unsupported change.</i></p>
]]></content:encoded>
			<wfw:commentRss>http://mscrmblogger.com/2011/02/23/crm2011-parent-child-grids-jscript/feed/</wfw:commentRss>
		<slash:comments>31</slash:comments>
		</item>
	</channel>
</rss>

