Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

170
Views
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));

    }

}

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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 });
    }
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!