<?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; toggle field</title>
	<atom:link href="http://mscrmblogger.com/tag/toggle-field/feed/" rel="self" type="application/rss+xml" />
	<link>http://mscrmblogger.com</link>
	<description>Achieving it all with Microsoft Dynamics CRM™</description>
	<lastBuildDate>Wed, 30 Nov 2011 00:34:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<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>24</slash:comments>
		</item>
		<item>
		<title>Toggle Fields in a CRM form using JScript</title>
		<link>http://mscrmblogger.com/2009/08/26/toggle-fields-in-a-crm-form-using-javascript/</link>
		<comments>http://mscrmblogger.com/2009/08/26/toggle-fields-in-a-crm-form-using-javascript/#comments</comments>
		<pubDate>Wed, 26 Aug 2009 20:39:48 +0000</pubDate>
		<dc:creator>Stephan Bayer</dc:creator>
				<category><![CDATA[Extensions]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[crm 4]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[microsoft dynamics CRM 4]]></category>
		<category><![CDATA[onload event]]></category>
		<category><![CDATA[radio button]]></category>
		<category><![CDATA[toggle field]]></category>

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

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

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

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

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

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

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

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

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

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

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

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

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

	}

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

