Search This Blog

Saturday, October 25, 2014

Filter Lookup values in CRM 2011 and CRM 2013 without Javascript

In this Example I filter cities of a country. Follow following steps:

  •  Every country have many cities, so i create a 1:N relationship of cities with country entity i.e. in city entity i have lookup of country entity. 
  • Now in Account entity i create two lookups one of Country entity and second city entity.

  • In city entity I added some sample records of cities and also select their country so that city will be filtered related to that country.                                                                                     



  • Now open lookup of city and edit filter record settings as shown in below image. 





  • Filtered records are shown below that When I select country Pakistan only cities related to Pakistan are shown and when I select country Afghanistan only cities related to Afghanistan are shown.                                                                                                                                  















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;
}