javascriptdynamics-crmmicrosoft-dynamicspowerappsdynamics-crm-online

function getControl("Subgrid").addOnLoad execute twice when parent record is first created


Hello I made a function that detect if i added or deleted a new row in my subgrid and execute some coce according to that

Normally it works fine in an existing parent record unless I create a new parent record, i press save, then i add a row in my subgrid and my function executes twice for some reason

does anyone know why my code execute twice only when i create a new parent record, the function is in the onLoad even

I took the principle code from here : (the dosomething function fire twice)

function onLoad() {

    var funtionName = "onLoad";

    try {

        setTimeout(function () {

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

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

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

            }

        }, 5000);

    } catch (e) {

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

    }

}

function onGridLoad() {

    var functionName = " onGridLoad ";

    var currentRowCount = null;

    try {

        setTimeout(function () {

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

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

                if (currentRowCount > _rowCount) {

                    dosomething();

                    _rowCount = currentRowCount;

                }

                else if (currentRowCount < _rowCount) {

                    dosomethingelse();

                    _rowCount = currentRowCount;

                }

            }

        }, 2000);

    } catch (e) {

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

    }

}


Solution

  • Your code uses the Xrm.Page object, which is deprecated on Dynamics 365 CE Online / Dataverse. You should use the new API; see Client API Reference - MS Docs.

    Your function is called twice, because the onLoad function gets called twice by the system: first when the form is opened in "Create" mode, second when the data on the form is saved. After the data has been saved, the form page is not reloaded, but nevertheless the form's onLoad function is called again. This causes your script to attach a second onLoad handler.

    Try this code. It is based on the modern script API. Make sure your form passes the form context to the onLoad function:

    function onLoad(c) {
        const formContext = c.getFormContext();
        const subGrid = formContext.getControl("subgrid");
        const contactSubGrid = formContext.getControl("contact_subgrid");
    
        if (subGrid === null || contactSubGrid === null)
            return;
    
        try {
            setTimeout(() => {
                let rowCount = subGrid.getGrid().getTotalRecordCount();
    
                subGrid.addOnLoad(controlContext => {
                    setTimeout(() => {
                        let currentRowCount = subGrid.getGrid().getTotalRecordCount();
    
                        try {
                            if (currentRowCount > rowCount) {
                                // do something
                            }
                            else if (currentRowCount < rowCount) {
                                // do something else
                            }
    
                            rowCount = currentRowCount;
                        }
                        catch (e) {
                            Xrm.Navigation.openErrorDialog({ message: e.message, errorCode: e.errorCode });
                        }
                    }, 2000);
                });
            }, 5000);
        }
        catch (e) {
            Xrm.Navigation.openErrorDialog({ message: e.message, errorCode: e.errorCode });
        }
    }