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();
Posted by Stephan Bayer at 3:39 pm
Tagged with: crm 4, javascript, microsoft dynamics CRM 4, onload event, radio button, toggle field

