Saturday 23 November 2013

Attach OnClick event in Sub grid Records MS CRM 2013 Using JavaScript !!!

Sometimes we have requirements to attach click event to sub-grid. So we could read selected record data and do the stuffs.

Here is sample code to attach click event to sub grid :

function ReadSelectedSubGridRecords() {
    if (document.getElementById("SubGridName")) {
        var grid = document.getElementById("SubGridName").control;
        for (var rowNo = 0; rowNo < grid.get_selectedRecords().length; rowNo++)
            alert(grid.get_selectedRecords()[rowNo].Name);
    }
}

function Form_OnLoad() {
    if (document.getElementById("SubGridName")) {
        var grid = document.getElementById("SubGridName");
        // Google chrome
        if (grid.addEventListener) {
            grid.addEventListener('click', ReadSelectedSubGridRecords, false);
            // IE
        } else if (grid.attachEvent) {
            grid.attachEvent('onclick', ReadSelectedSubGridRecords);
        }

    }
    else {
        setTimeout("Form_OnLoad();", 2000);
    }
}

Attach Form_OnLoad function to form OnLoad event handler !!!

2 comments:

  1. Thanks for this helpful blog, the code work well but i need to get another cell value from the grid not the name, how to do it?
    Thanks

    ReplyDelete
  2. I tried to use your code to open the correct form on dblclick event in multiform scenario which worked fine but i noticed one issue. Issue is it wont allow me to close the form on single save and close event but it requires save and close twice. i tried to override the ondblclick event but that did not help. Any thoughts?

    ReplyDelete