Home > Extensions > Web Resources > Scripts > Toggle Fields in a CRM form using JScript

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)

Steps:

1. Create Form
2. Identify Radio button you want to control the toggle of another field.
3. Identify the field you want to hide.
4. Modify below javascript code after line 46 and place into Form Properties-> Onload Event

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();

Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

© 2011 MSCRM Blogger

This site does not necessarily represent the views of Microsoft. The material posted on this blog are provided AS-IS, and you are to use the content at your own risk. Some of the code and information on this site may use unsupported methods. If you experience any problems using the information provided through this website, please post a comment and we will do our best to assist you, however, we can in no way guarantee support or resolution.

Microsoft Dynamics®, Silverlight®, Visual Studio®, and the corresponding logos are a registered trademarks owned by Microsoft Corporation. MSCRM Blogger has made every effort to supply trademark information about company names, products, and services mentioned on this blog. All third party trademarks are the property of their respective owners. Any rights not expressly granted herein are reserved.