Tuesday 16 July 2013

Retrieving Subgrid Items count using Javascipt in MS CRM 2011/MS CRM 2013

Retrieving Subgrid Items count using Javascipt.

1.Get the subgrid  using "Name" of subgrid as given below.
















2.Write the Javascript function as given below:

function subgridItemCount() {
 
      // Get the Subgrid Control
    var grid = Xrm.Page.ui.controls.get('LineItems')._control;
    if (grid.get_innerControl() == null) {
        setTimeout(subgridItemCount, 1000);
        return false;
    }
    else if (grid.get_innerControl()._element.innerText.search("Loading") != -1) {
        setTimeout(subgridItemCount, 1000);
        return false;
    }
    var countRec = grid.get_innerControl().get_allRecordIds().length;
    if (countRec > 0) {
        alert(countRec);
 
    }
 
}


For MS CRM 2013:

function FindControl() {
    if (document.getElementById('subgridName')!= null) {
        var count=document.getElementById('subgridName').control.get_totalRecordCount();
        alert(count);
        }
        else {
            setTimeout("FindControl()", 1000);
        }
    }


3.Result




























Hope this helps,

Regards,

Yusuf

7 comments:

  1. Unfortunately, this will not work in CRM 2013. Do you have a current solution?

    ReplyDelete
  2. I would also be happy if there is a solution for CRM 2013

    ReplyDelete
  3. This code is works fine.Thanks for posting the blog clearly and neatly present it.

    ReplyDelete
  4. This solution can help you for MS CRM 2013.

    (function () {

    var TimeOut = 0;
    var subGrids = $('#new_myGrid'); //get your gird

    if (subGrids.length > 0 && subGrids[0].control.get_totalRecordCount() > -1) {
    setCridCounter(subGrids[0]);
    }
    else {
    var intId = setInterval(function () {
    subGrids = $('#' + EntityHash[EntityType]);
    if (subGrids.length > 0 && subGrids[0].control.get_totalRecordCount() > -1) {
    clearInterval(intId);
    setCridCounter(subGrids[0]);
    }
    else if (TimeOut > 5000)
    //If not completed by now, exit this interval after 5 sec.
    clearInterval(intId);
    TimeOut += 500;
    }, 500);
    }


    function setCridCounter(Sgrid) {
    var counter = Sgrid.control.get_totalRecordCount();
    alert(counter);
    }
    })();

    ReplyDelete
  5. Correction for the self-execution code above.

    (function () {

    var TimeOut = 0;
    var subGrids = $('#new_myGrid'); //get your gird

    if (subGrids.length > 0 && subGrids[0].control.get_totalRecordCount() > -1) {
    setCridCounter(subGrids[0]);
    }
    else {
    var intId = setInterval(function () {
    subGrids = $('#new_myGrid');
    if (subGrids.length > 0 && subGrids[0].control.get_totalRecordCount() > -1) {
    clearInterval(intId);
    setCridCounter(subGrids[0]);
    }
    else if (TimeOut > 5000)
    //If not completed by now, exit this interval after 5 sec.
    clearInterval(intId);
    TimeOut += 500;
    }, 500);
    }


    function setCridCounter(Sgrid) {
    var counter = Sgrid.control.get_totalRecordCount();
    alert(counter);
    }
    })();

    ReplyDelete
  6. Thanks for posting! :)

    ReplyDelete
  7. This is very Helpful Thanks again

    ReplyDelete