Hide and show items in a picklist in CRM 4.0
If you already worked with Microsoft Dyamics CRM 4.0, you know that almost everything you do with JavaScript is considered a "hack" because you manipulate the DOM of the pages by yourself. Because of this I decided to give you some help with differents helper methods I developped at my day job.
I'm currently working on a big development project involving CRM.
Those methods seems a lot more complicated than they has to be because we can't really hide and show items in picklists (HTML select). In HTML if you want to hide an "option" of a select, you need to remove it ! So when you want to show it back again you need to readd it to the list, and in addition to that, it will be placed at the end, not where he was before... so I created methods to help people to really hide and show CRM picklist items only by their value.
Hide a picklist item in CRM 4.0
function HidePickListItem(listID, value)
{
var objList = document.getElementById(listID);
// If the list has never been saved, save it now
if (objList.SavedList == null)
{
var arrListe = new Array();
for (var i=0; i<objList.options.length; i++)
{
arrListe[i] = new Object();
arrListe[i].value = objList.options[i].value;
arrListe[i].Libelle = objList.options[i].text;
arrListe[i].Visible = true;
}
objList.SavedList = arrListe;
}
for (var i=0; i<objList.SavedList.length; i++)
if (objList.SavedList[i].value == value)
objList.SavedList[i].Visible = false;
for (var i=objList.options.length - 1; i>=0; i--)
if (objList.options[i].value == value)
objList.options.remove(i);
}
Show a picklist item that has been hidden in CRM 4.0
function ShowPickListItem(listID, value)
{
var objList = document.getElementById(listID);
if (objList.SavedList !=
null)
{
var selValue =
null;
var indexInsertion = 0;
for (
var i=0; i<objList.SavedList.length; i++)
if (objList.SavedList[i].value == value)
objList.SavedList[i].Visible =
true;
// Keep the selected value so we can reselect it after
if (objList.selectedIndex > -1)
selValue = objList.options[objList.selectedIndex].value;
// Remove all the items in the list
for (var i=objList.options.length - 1; i>=0; i--)
objList.options.remove(i);
// Add the items that must be visible
for (var i=0; i<objList.SavedList.length; i++)
{
if (objList.SavedList[i].Visible)
{
var oOption = document.createElement('option');
oOption.text = objList.SavedList[i].Libelle;
oOption.value = objList.SavedList[i].value;
objList.options.add(oOption);
}
}
// Reselect the item that was selected
for (var i=0; i<objList.options.length; i++)
if (objList.options[i].value == selValue)
objList.selectedIndex = i;
}
}