Search This Blog

Showing posts with label Change optionset Value on change of other optionset value. Show all posts
Showing posts with label Change optionset Value on change of other optionset value. Show all posts

Thursday, October 23, 2014

Change Optionset Values on change of other optionset values using JavaScript in Microsoft Dynamics CRM 2011 and 2013

function optionSetChanged() // apply this function of change of country field
{

    // on the base of country change cities

    var _collection = getCollection();
    var _selectedCity = null;
    var _cityOptionset = Xrm.Page.ui.controls.get("city");  
if (_cityOptionset != null)
    _selectedCity = _cityOptionset.getAttribute().getValue();
var _cityOptions = _cityOptionset.getAttribute().getOptions();
    var _selectedCountry = Xrm.Page.getAttribute("Country").getText();    
    // If Country is empty, then clear the City field.
    if (_selectedCountry == "")
{
        _cityOptionset.clearOptions();
    }
    else
{
        for (var i = 0; i < _collection.length; i++)
{          
if (_selectedCountry.toLowerCase() == _collection[i].Country.toLowerCase())
{                _
cityOptionset.clearOptions();
            for (var j = 0; j < _collection[i].Cities.length; j++)
{                  
for (var k = 0; k < _cityOptions.length; k++)
{                      
if (_collection[i].Cities[j].toLowerCase() == _cityOptions[k].text.toLowerCase())
{                          
_cityOptionset.addOption(_cityOptions[k]);
                        break;
                        }
                    }
                }
                break;
            }
        }    
if (_cityOptionset != null && _selectedCity != null)
            _cityOptionset.getAttribute().setValue(_selectedCity);
    }
}


function getCollection() {

    var _collection = new Array();  
var India_Cities = new Array("NewYork");
    var India_obj = { Country: "USA", Cities: India_Cities };
    _collection.push(India_obj);

    var Srilanka_Cities = new Array("Lahore");
    var SriLanka_obj = { Country: "pakistan", Cities: Srilanka_Cities };
    _collection.push(SriLanka_obj);

    var USA_Cities = new Array("Mumbai");
    var USA_obj = { Country: "India", Cities: USA_Cities };
    _collection.push(USA_obj);

    return _collection;
}