Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

166
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda