locked
get total records count in the subgrid, crm 2016 RRS feed

  • Question

  • Hi,

    I have a subgrid in a CRM 2016 form.

    I want to check the count of records in the subgrid, and if it is more than one I should make a field readonly on the form.

    I tried to get rowsCount as below

    Xrm.Page.getControl("name").getGrid().getTotalRecordCount(), but it is returning -1.

    (I have added  Xrm.Page.getControl("name").addOnLoad(myFunction) at form load.

    And also refreshing subgrid on form load)

    I need to check the countofrecords onLoad of the form and Add/Remove of items on the subgrid.

    (My Subgrid is a custom subgrid, which gets data using custom fetchxml query)

    Need your suggestions.

    Thanks


    • Edited by balu05 Tuesday, April 19, 2016 10:19 AM
    Tuesday, April 19, 2016 10:04 AM

All replies

  • Hi,

    You can use below code. Replace "Activity" with your subgrid name.

    function RetrieveSubGridRecords(){

    if (document.getElementById("Activity")) {
     var grid = document.getElementById("Activity").control;
     var ids = grid.get_allRecordIds();
    if(ids.length>1)
    {
    //your logic;
    }
        }
        else {
            setTimeout("RetrieveSubGridRecords();", 2500);
        }

    }

    Hope this help.

    Thanks


    • Proposed as answer by Suresh Sorde Tuesday, April 19, 2016 1:00 PM
    • Edited by Suresh Sorde Tuesday, April 19, 2016 1:12 PM
    Tuesday, April 19, 2016 1:00 PM
  • Hi Suresh,

    Thanks for your reply!

    Is there any supported way, without using 'document.getElementById'.

    Tuesday, April 19, 2016 1:14 PM
  • Hi Balu,

    Please check suggestions given on below link.

    https://community.dynamics.com/crm/f/117/t/119866

    Hope this help.

    Thanks.



    Tuesday, April 19, 2016 1:35 PM
  • Hi Suresh,

    Using ODATA rest call we can get it.

    But what I am thinking is can't I get rows using

    Xrm.Page.getControl("name").getGrid().getTotalRecordCount()?

    Thanks

    Tuesday, April 19, 2016 1:52 PM
  • Hi,

    Please debug your code and check are you getting the getTotalRecordCount property for the subgrid or not.

    Hope this help.

    Thanks.

    Tuesday, April 19, 2016 2:08 PM
  • You can get number of rows in supported way using Xrm.Page.getControl("name").getGrid().getTotalRecordCount() by adding timeout before for the javascript code because subgrid takes some time to load after the form is loaded.

    If subgrid is not completly loaded with all records then it returns wrong number of records.

    Refer below blog for identifying event for records added or removed on sub grid.

    Identify the trigger for an On-load event for Sub-grid in Dynamics CRM

    HTH

    Sam


    Dynamics CRM MVP | Inogic | http://inogic.blogspot.com| news at inogic dot com

    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    Thursday, April 21, 2016 7:10 AM