【问题标题】:function getControl("Subgrid").addOnLoad execute twice when parent record is first created首次创建父记录时,函数 getControl("Subgrid").addOnLoad 执行两次
【发布时间】:2021-12-07 22:49:46
【问题描述】:

你好我做了一个函数来检测我是否在我的子网格中添加或删除了一个新行并根据它执行一些 coce

通常它在现有父记录中可以正常工作,除非我创建一个新的父记录,然后按保存,然后在子网格中添加一行并且我的函数由于某种原因执行两次

有谁知道为什么我的代码只在我创建新的父记录时才执行两次,该函数甚至在 onLoad 中

我从这里获取了原理代码:(dsomething 函数触发了两次)

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

    }

}

【问题讨论】:

    标签: javascript dynamics-crm microsoft-dynamics powerapps dynamics-crm-online


    【解决方案1】:

    您的代码使用 Xrm.Page 对象,该对象在 Dynamics 365 CE Online/Dataverse 上已弃用。您应该使用新的 API;见Client API Reference - MS Docs。

    您的函数被调用了两次,因为onLoad 函数被系统调用了两次:第一次是在“创建”模式下打开表单时,第二次是保存表单上的数据时。保存数据后,不会重新加载表单页面,但仍会再次调用表单的onLoad 函数。这会导致您的脚本附加第二个 onLoad 处理程序。

    试试这个代码。它基于现代脚本 API。确保您的表单将表单上下文传递给 onLoad 函数:

    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 });
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-11
      • 2017-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-24
      相关资源
      最近更新 更多