Tuesday, January 13, 2015

CRM 2013 Remove all the subgrid add button


Call it during On load (with certain condition) then all the sub grid add button in the form will be hidden.

Script:

var intervalId;

function makeReadOnly() {
    try {
        var subgridsLoaded = false;

        Xrm.Page.ui.controls.get().forEach(function (control, index) {
            if (control.setDisabled && Xrm.Page.ui.getFormType() != 3) {
                control.setDisabled(true);
            }
            else {
                // removeAddButtonFromSubGrid(control);
                // subgridsLoaded = true; 
            }
        });

        if ($("div[id$='_crmGridTD']").length > 0 && !subgridsLoaded) {
            intervalId = setInterval(function () {
                var subgridsArr = Xrm.Page.getControl(function (control, index) {
                    return control.getControlType() == 'subgrid';
                });
                subgridsArr.forEach(function (control, index) {
                    removeButtonsFromSubGrid(control);
                });
            }, 500);
        }
    }

    catch (e) {
        alert("makeReadOnly() Error: " + e.message);
    }
}

function removeButtonsFromSubGrid(subgridControl) {
    if (intervalId) {
        $('#' + subgridControl.getName() + '_addImageButton').css('display', 'none');
        $('#' + subgridControl.getName() + '_openAssociatedGridViewImageButton').css('display', 'none');
        clearInterval(intervalId);
    }
}

No comments:

Post a Comment