Tuesday, 16 February 2016

20:09 - No comments

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

Introduction :

With CRM 2015 SP1, the client API was extended to allow attaching events to subgrid to manage the subgrid data operations.

The OnLoad event however executes on all of the following operations

1.Load of parent form
2.Save of parent form
3. Add a new record in sub grid
4. Remove a record from sub grid
5.Navigating to prev/next page in sub grid

when we register a method on the onload event, ts does not indicate in any way which of the above actually fired the onload event

Workaround to identifying the operation :

We cannot register an event when the record is added/removed from the sub grid. The only event available is onload so one way to identify if it was one of the add/remove operations that caused the grid to call the onload event. we can keep a check on the count of records in the grid.

we can register the onload event using the following code


   function onLoad() {

    var funtionName = "onLoad";

    try {

        //setting timeout beacuse subgid take some time to load after the form is loaded

        setTimeout(function () {

            //validating to check if the sub grid is present on the form

            if (Xrm.Page != null && Xrm.Page != undefined && Xrm.Page.getControl("contact_subgrid") != null && Xrm.Page.getControl("contact_subgrid") != undefined) {

                //stores the row count of subgrid on load event of CRM Form

                _rowCount = Xrm.Page.getControl("contact_subgrid").getGrid().getTotalRecordCount();

                //registering refreshform function onload event of subgrid

                Xrm.Page.getControl("contact_subgrid").addOnLoad(onGridLoad);

            }

        }, 5000);

    } catch (e) {

        Xrm.Utility.alertDialog(functionName + "Error: " + (e.message || e.description));

    }

}


The rowcount is defined as global variable and is updated here to store the count of  records in the grid when the form is loaded initially.



    function onGridLoad() {

    var functionName = " onGridLoad ";

    var currentRowCount = null;

    try {

        //setting timeout beacuse subgrid take some time to load after the form is loaded

        setTimeout(function () {

            //validating to check if the sub grid is present on the form

            if (Xrm.Page != null && Xrm.Page != undefined && Xrm.Page.getControl("contact_subgrid") != null &&                                   Xrm.Page.getControl("contact_subgrid") != undefined) {

                //stores the row count of subgrid on load event of CRM Form

                currentRowCount = Xrm.Page.getControl("contact_subgrid").getGrid().getTotalRecordCount();

                if (currentRowCount > _rowCount) {

                    //call the intended function which we want to call only when records are added to the grid

                    dosomething();

                    //set current row count to the global row count

                    _rowCount = currentRowCount;

                }

                else if (currentRowCount < _rowCount) {

                    //call the intended function which we want to call only when records are removed from the grid

                    dosomethingelse();

                    //set current row count to the global row count

                    _rowCount = currentRowCount;

                }

            }

        }, 2000);

    } catch (e) {

        Xrm.Utility.alertDialog(functionName + "Error: " + (e.message || e.description));

    }

}

0 comments:

Post a Comment