0

我创建了一个 JavaScript OnSave 事件,该事件通过“机会”实体表单中的“销售配额分布”实体网格进行解析,并检查“销售配额分布”网格的“资源”字段中的重复项。当有重复时,将出现警告消息。这是可行的,但我希望能够附加 OnSave 事件,以在没有重复资源的情况下不允许用户保存表单。我怎样才能做到这一点?

以下是我当前的代码:

function GetTotalResourceCount(executionContext) {
    console.log("function started");
    var execContext = executionContext;
    var formContext = executionContext.getFormContext();
    var resourceyescount = 0;
    try {
        var gridCtx = formContext._gridControl;
        var grid = gridCtx.getGrid();
        var allRows = grid.getRows();
        //get rows - use the getControl method and pass the grid name.
        //var gridContext = formContext.getControl("s_qd");
        //        if (formContext.getGrid().getTotalRecordCount() == 0) {
        //           setTimeout(function () { GetTotalResourceCount(execContext); }, 2000);
        //           return;
        //       }
        var duplicatesFound = 0;
        //loop through rows and get the attribute collection
        allRows.forEach(function (row, rowIndex) {
            var thisRow = row.getData().entity;
            var thisRowId = thisRow.getId();
var thisResource = "";
var thisResourceName = "";
var thisResourceID = "";
            console.log("this row id=" + thisRowId);
            var thisAttributeColl = row.getData().entity.attributes;
            thisAttributeColl.forEach(function (thisAttribute, attrIndex) {
                var msg = "";
                if (thisAttribute.getName() == "new_resource") {
                    thisResource = thisAttribute.getValue();
thisResourceID = thisResource[0].id;
thisResourceName = thisResource[0].name;
                }
            });

            // Loop through every row and find one with
            // thatresource == thisResource &&
            // thatrow ID != thisRowId
            var allRows2 = formContext.getGrid().getRows();
            //loop through rows and get the attribute collection
            allRows2.forEach(function (row, rowIndex) {
                var thatRow = row.getData().entity;
                var thatRowId = thatRow.getId();
                var thatAttributeColl = row.getData().entity.attributes;
var thatResource = "";
var thatResourceName = "";
var thatResourceID = "";
                thatAttributeColl.forEach(function (thatAttribute, attrIndex) {
                    if (thatAttribute.getName() == "new_resource") {
                        thatResource = thatAttribute.getValue();
thatResourceID = thatResource[0].id;
thatResourceName = thatResource[0].name;
                        if (thatResourceID == thisResourceID && thatRowId != thisRowId) {
                            duplicatesFound++;
                            var msg = "Duplicate resource " + thatResource;
                        }
                    }
                });
            });
        });

        if (duplicatesFound > 0) {
            console.log("duplicate found");
            Xrm.Utility.alertDialog("WARNING: There are duplicate resources in the Sales Quota Distribution grid.");
        }
    } catch (err) {
        console.log('Error occurred :' + err)
    }
}

任何帮助将不胜感激。谢谢! 在此处输入图像描述

4

2 回答 2

0

您需要的是preventDefault (客户端 API 参考) 这里的几个示例链接,

Javascript – 防止保存

根据异步操作的结果取消保存事件

试试下面的代码,我只添加了 2 行 var saveEvent = executionContext.getEventArgs();saveEvent.preventDefault();

假设您的函数GetTotalResourceCount(executionContext)正在运行 onSave 事件。

function GetTotalResourceCount(executionContext) {
    console.log("function started");
    var execContext = executionContext;
    var formContext = executionContext.getFormContext();
    var resourceyescount = 0;
    try {
        var gridCtx = formContext._gridControl;
        var grid = gridCtx.getGrid();
        var allRows = grid.getRows();
        //get rows - use the getControl method and pass the grid name.
        //var gridContext = formContext.getControl("s_qd");
        //        if (formContext.getGrid().getTotalRecordCount() == 0) {
        //           setTimeout(function () { GetTotalResourceCount(execContext); }, 2000);
        //           return;
        //       }
        var duplicatesFound = 0;
        //loop through rows and get the attribute collection
        allRows.forEach(function (row, rowIndex) {
            var thisRow = row.getData().entity;
            var thisRowId = thisRow.getId();
var thisResource = "";
var thisResourceName = "";
var thisResourceID = "";
            console.log("this row id=" + thisRowId);
            var thisAttributeColl = row.getData().entity.attributes;
            thisAttributeColl.forEach(function (thisAttribute, attrIndex) {
                var msg = "";
                if (thisAttribute.getName() == "new_resource") {
                    thisResource = thisAttribute.getValue();
thisResourceID = thisResource[0].id;
thisResourceName = thisResource[0].name;
                }
            });

            // Loop through every row and find one with
            // thatresource == thisResource &&
            // thatrow ID != thisRowId
            var allRows2 = formContext.getGrid().getRows();
            //loop through rows and get the attribute collection
            allRows2.forEach(function (row, rowIndex) {
                var thatRow = row.getData().entity;
                var thatRowId = thatRow.getId();
                var thatAttributeColl = row.getData().entity.attributes;
var thatResource = "";
var thatResourceName = "";
var thatResourceID = "";
                thatAttributeColl.forEach(function (thatAttribute, attrIndex) {
                    if (thatAttribute.getName() == "new_resource") {
                        thatResource = thatAttribute.getValue();
thatResourceID = thatResource[0].id;
thatResourceName = thatResource[0].name;
                        if (thatResourceID == thisResourceID && thatRowId != thisRowId) {
                            duplicatesFound++;
                            var msg = "Duplicate resource " + thatResource;
                        }
                    }
                });
            });
        });

        if (duplicatesFound > 0) {
            console.log("duplicate found");
            Xrm.Utility.alertDialog("WARNING: There are duplicate resources in the Sales Quota Distribution grid.");
             var saveEvent = executionContext.getEventArgs();
             saveEvent.preventDefault();
        }
    } catch (err) {
        console.log('Error occurred :' + err)
    }
}
于 2020-03-26T07:54:54.610 回答
0

您还可以在其中一个控件上使用该setNotification方法。这将防止表单被保存。

从文档中:

在控件上设置错误通知将阻止表单保存。

就像:

formContext.getControl('your_attributename').setNotification('This will be displayed', 'optional id');
于 2020-03-26T08:02:28.890 回答