Tuesday, March 8, 2011

Hide Add New & Add Existing buttons on associated view based on the records in that view

I found one of the user crossing the question of "How to hide Add New & Add Existing button in the assocaited view based on the records in the view" i.e the user wants to hide display those buttons when there is no records present in the assocaited view.

The thread can be refered here.

crmGrid.InnerGrid.AllRecords.length / crmGrid.InnerGrid.NumberOfRecords property can help us in acheiving this.

By incorporating the above check in existing Dave Hawes script the requirement can be acheived. You can also modify the check based on your acheivement.

Below is the code snippet for your reference.

Javascript Code:


//To hide New & Add Existing buttons from form=============================
function HideAssociatedViewButtons(loadAreaId, buttonTitles)
{
var navElement = document.getElementById('nav_' + loadAreaId);
if (navElement != null)
{
navElement.onclick = function LoadAreaOverride()
{
// Call the original CRM method to launch the navigation link and create area iFrame
loadArea(loadAreaId);
HideViewButtons(document.getElementById(loadAreaId + 'Frame'), buttonTitles);
}
}
}


function HideViewButtons(Iframe, buttonTitles)
{
if (Iframe != null )
{
Iframe.onreadystatechange = function HideTitledButtons()
{
if (Iframe.readyState == 'complete')
{
var iFrame = frames[window.event.srcElement.id];
var liElements = iFrame.document.getElementsByTagName('li');

if (Iframe.contentWindow.document.all['crmGrid'].InnerGrid.AllRecords.length >0)
{
for (var j = 0; j < buttonTitles.length; j++)
{
for (var i = 0; i < liElements.length; i++)
{
if (liElements[i].getAttribute('title') == buttonTitles[j])
{
liElements[i].style.display = 'none';
break;
}
}
}
}
}
}
}
}


Keep Rocking !!!.

No comments:

Post a Comment