D365/JavaScript问题:需要附加OnSave事件的帮助,不允许用户保存表单 [英] D365/JavaScript issue: Need help appending an OnSave event to not allow a user to save a form

查看:121
本文介绍了D365/JavaScript问题:需要附加OnSave事件的帮助,不允许用户保存表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个JavaScript OnSave事件,该事件通过机会"实体表单中的销售配额分配"实体网格进行解析,并在销售配额分配"网格的资源"字段中检查重复项.如果有重复项,则会出现警告消息.这是可行的,但是我希望能够附加OnSave事件,以在没有重复资源的情况下不允许用户保存表单.我该怎么办?

I've created a JavaScript OnSave event that parses through a "Sales Quota Distribution" entity grid within an "Opportunity" entity form, and checks for duplicates in the "Resource" field of the "Sales Quota Distribution" grid. When there is a duplicate, a warning message will appear. This is working, but I'd like to be able to append the OnSave event to not allow the user to save the form if there are no duplicate resources. How can I do this?

下面是我当前的代码:

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

任何帮助将不胜感激.谢谢!

Any help would be greatly appreciated. Thanks!

推荐答案

您需要的是

What you need is preventDefault (Client API reference) Few example links here,

JavaScript –阻止保存

取消保存基于异步操作结果的事件

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

Try below code, I have added just 2 lines var saveEvent = executionContext.getEventArgs();saveEvent.preventDefault();

假设函数GetTotalResourceCount(executionContext)在onSave Event上运行.

Assuming your function GetTotalResourceCount(executionContext) is running onSave Event.

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

这篇关于D365/JavaScript问题:需要附加OnSave事件的帮助,不允许用户保存表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆