Sunday, January 6, 2013

Dynamics CRM 2011 - Javascript - Getting selected records from subgrid

Getting the selected values from subgrid in form is not supported, which makes it little bit more tricky.
Lets say you need to get selected records from subgrid after the user got out of the subgrid, like after onchange event of some form attribute. In this case the focus moves out of the subgrid and all you'll get would be an empty list.

The trick to get the selected records is: Get the focus back on the subgrid before trying to get the values.
Anyway it is not supported as well.

My Sample code:

var subgridName = "testSubGrid"

function GetSelectedRecords() {
    var stringOfSelectedIds = "No records were selected";
    if (Xrm.Page.getControl(subgridName) != null) {
        document.getElementById(subgridName).getElementsByTagName('a')[0].focus();
    }
    //document.getElementById(subgridName).control.get_selectedIds()
    if (document.getElementById(subgridName) != null && document.getElementById(subgridName).control != null) {
        selectedRecods = document.getElementById(subgridName).control.get_selectedIds();
        if (selectedRecods != null && selectedRecods.length > 0) {
            stringOfSelectedIds = "Selceted records: " + selectedRecods.join();
        }
        alert(stringOfSelectedIds);
    }
}

GetSelectedRecords()

No comments:

Post a Comment