Home > MS CRM 4.0 > Dependent Picklist for Country ,state

Dependent Picklist for Country ,state


Following sample shows how to build dependent picklist in CRM,
 

var mainField = "nsi_continent";

var

parentField = "nsi_country";

var

childField = "new_state";

var

stateTextList = new NSiTextList(childField, "State/Province", null, "", "", true);

var

countryTextList = new NSiTextList(parentField, "Country/Region", stateTextList, "country", "state", true);

var

originalCountry = countryTextList.Picklist.defaultSelected;

var

continentTextList = new NSiTextList(mainField, "Continent", countryTextList, "continent", "country", true);

if

(crmForm.FormType == 1)

{

continentTextList.Picklist.defaultSelected =

"North America";

countryTextList.Picklist.defaultSelected =

"United States";

crmForm.all[mainField].DataValue = continentTextList.Picklist.defaultSelected;

crmForm.all[parentField].DataValue = countryTextList.Picklist.defaultSelected;

NSiGetItems("United States", stateTextList.Picklist, stateTextList.Picklist.defaultSelected, "country", "state");

}

var

originalContinent = continentTextList.Picklist.defaultSelected;

NSiGetContinents(continentTextList);

NSiGetItems(originalContinent, countryTextList.Picklist, countryTextList.Picklist.defaultSelected,

"continent", "country");

NSiGetItems(crmForm.all[parentField].DataValue, stateTextList.Picklist, stateTextList.Picklist.defaultSelected,

"country", "state");

function

nsi_trim(str)

{

try

{

if (str != null && str.length > 0)

str = str.replace(/^ss*/,

).replace(/ss*$/, );

else str = ;

}

catch (e) { }

return str;

}

function

NSiTextList(txtFieldId, sName, dependentField, sTag1, sTag2, bEnableOnchange)

{

var textList = this;

var selectedIndex = -1;

var textCtrl = crmForm.all[txtFieldId];

if (textCtrl == null)

return alert("TextList: " + txtFieldId + " Is Missing");

textList.Picklist = document.createElement(

"SELECT");

textList.Picklist.id = textCtrl.id +

"_picklist";

textList.Picklist.name = sName;

textList.Picklist.req = textCtrl.req;

textList.Picklist.tabIndex = textCtrl.tabIndex;

textList.Picklist.className =

"ms-crm-SelectBox ";

textList.Picklist.defaultSelected = nsi_trim(textCtrl.DataValue);

textList.textCtrl = textCtrl;

textList.dependentField = dependentField;

textList.sTag1 = sTag1;

textList.sTag2 = sTag2;

textCtrl.parentElement.appendChild(textList.Picklist);

textCtrl.style.display =

‘none’;

textList.Add =

function(text, value) { return textList.Picklist.AddOption(text, value); }

function UpdateStateField()

{

document.getElementById(textList.textCtrl.id).DataValue = textList.Picklist.SelectedText;

if (textList.Picklist.selectedIndex > 0 && textList.dependentField != null)

{

NSiGetItems(textList.Picklist.SelectedText, textList.dependentField.Picklist, textList.dependentField.Picklist.SelectedText,

textList.sTag1, textList.sTag2);

}

}

if (bEnableOnchange)

{

textList.Picklist.attachEvent("onchange", UpdateStateField);

}

}

function

NSiGetContinents(textList)

{

try

{

var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");

xmlDoc.async =

false;

xmlDoc.load(

"/ISV/NSI/_data/Country.xml");

var continents = xmlDoc.getElementsByTagName("continent");

var selectedIndex = 0;

textList.Add(

"", "");

for (var i = 0; i < continents.length; i++)

{

textList.Add(nsi_trim(continents[i].firstChild.nodeValue), i + 1);

if (selectedIndex == 0 && textList.Picklist.defaultSelected == nsi_trim(continents[i].firstChild.nodeValue))

selectedIndex = i + 1;

}

if (selectedIndex > 0)

textList.Picklist.DataValue = selectedIndex;

textList.Picklist.defaultSelected = selectedIndex;

}

catch (e) { }

}

function

NSiGetItems(picklist1_DataValue, picklist2, picklist2_SelectedText, sTag1, sTag2)

{

try

{

picklist2.length = 0;

picklist1_DataValue = nsi_trim(picklist1_DataValue);

picklist2_SelectedText = nsi_trim(picklist2_SelectedText);

var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");

xmlDoc.async =

false;

xmlDoc.load(

"/ISV/NSI/_data/Country.xml");

var picklist1Items = xmlDoc.getElementsByTagName(sTag1);

var picklist2Items = null;

for (var i = 0; i < picklist1Items.length; i++)

{

if (nsi_trim(picklist1Items[i].firstChild.nodeValue) == picklist1_DataValue)

{

picklist2Items = picklist1Items[i].getElementsByTagName(sTag2);

break;

}

}

var selectedIndex = 0;

picklist2.AddOption(

"", "");

for (var x = 0; picklist2Items != null && x < picklist2Items.length; x++)

{

picklist2.AddOption(nsi_trim(picklist2Items(x).firstChild.nodeValue), x + 1);

if (picklist2_SelectedText == nsi_trim(picklist2Items(x).firstChild.nodeValue))

{

selectedIndex = x + 1;

}

}

if (selectedIndex > 0)

picklist2.DataValue = selectedIndex;

picklist2.defaultSelected = selectedIndex;

if (picklist2.length == 1 && picklist2.previousSibling.id == "new_state")

{

picklist2.style.display =

"none";

crmForm.SetFieldReqLevel(

"new_state", 0);

picklist2.previousSibling.style.display =

"inline";

picklist2.DataValue =

"-1";

picklist2.defaultSelected = -1;

}

else if (picklist2.previousSibling.id == "new_state")

{

picklist2.style.display =

"inline";

picklist2.previousSibling.style.display =

"none";

if (IsPending == "1" || crmForm.all.nsi_approved.DataValue == false)

crmForm.SetFieldReqLevel(

"new_state", 1);

}

}

catch (e) { }

}

sample xml file

 

<

continent>

Australia

<

country>Australia

<

state>New South Wales</state>

<

state>Queensland</state>

<

state>South Australia</state>

<

state>Tasmania</state>

<

state>Victoria </state>

<

state>Western Australia</state>

</

country>

<

country>Papua New Guinea</country>

</

continent>

Advertisements
Categories: MS CRM 4.0
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: