Friday, July 18, 2008

MSCRM 4 - Remove 'Add Existing xxxxx to this record' button

April 23, 2008 19:54 by davehawes

I've been working on a MS CRM 4 project and found that the custom entities that we added displayed an 'Add Existing xxxxx to this record' button when it was not required and created a lot of confusion amoung the users.

Microsoft have said they will make this optional 'In the next release' but that is not soon enough for me!

After working through various ideas on how to solve this with Ian Crowther and Steve Vallance we came up with the following fix. Thanks also to 'Dynamic Methods' for this post about hiding buttons: http://dmcrm.blogspot.com/2008/01/hiding-buttons-in-mscrm-40.html

This javascript should be added to the onLoad event of the entity where you are having the problem. You then need to call the function removeExistingButton passing in the name of the dataArea div tag and the title of the button you need to remove. (this can be found by using the IE Developer toolbar):

HideAssociatedViewButton('new_business_new_surveys', 'Add existing Survey to this record');
HideAssociatedViewButton('new_account_new_eventinvite', 'Add existing Event Invite to this record');

function HideAssociatedViewButton(loadAreaId, buttonTitle)
{
    var navElement = document.getElementById('nav_' + loadAreaId);
    if (navElement != null)
    {
        navElement.onclick = function LoadAreaOverride()
        {
            // Call the original method to launch the navigation link
            loadArea(loadAreaId);

            var associatedViewIFrame = document.getElementById(loadAreaId + 'Frame');
            if (associatedViewIFrame != null)
            {
                associatedViewIFrame.onreadystatechange = function HideTitledButton()
                {
                    if (associatedViewIFrame.readyState == 'complete')
                    {
                        var iFrame = frames[window.event.srcElement.id];
                        var liElements = iFrame.document.getElementsByTagName('li');
                        for (var i = 0; i < liElements.length; i++)
                        {
                            if (liElements[i].getAttribute('title') == buttonTitle)
                            {
                                liElements[i].style.display = 'none';
                                break;
                            }
                        }
                    }
                }
            }
        }
    }
}

No comments: