Dynamics Crm 2016 online:客户端记录创建阻止无法正常工作

时间:2016-10-20 07:45:57

标签: javascript dynamics-crm-2016

如果满足某些条件,我会尝试阻止创建约会,但是行Xrm.Page.context.getEventArgs().preventDefault();不起作用并且创建了记录。这是整个代码,调用是同步的:

var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.0/appointments?$select=activityid&$filter=<my filter>", false);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("Prefer", "odata.include-annotations=\"OData.Community.Display.V1.FormattedValue\"");
req.onreadystatechange = function () {
    if (this.readyState === 4) {
        req.onreadystatechange = null;
        if (this.status === 200) {
            var results = JSON.parse(this.response);
            if(results.value.length > 0)
            {
                Xrm.Utility.alertDialog('It is impossible to create the appointment.');
                Xrm.Page.context.getEventArgs().preventDefault();
            }
        }
        else {
            alert(this.statusText);
        }
    }
};
req.send();

1 个答案:

答案 0 :(得分:1)

您的代码应该在Xrm.Page.context.getEventArgs()错误输出,页面的上下文对象上没有用于事件参数的属性。

您应该在save上传递执行上下文,然后使用执行上下文的事件参数来停止保存。

表单自定义:

enter image description here

处理程序:

function onSave(executionContext) {
  if(results.value.length > 0)
  {
    Xrm.Utility.alertDialog('It is impossible to create the appointment.');
    executionContext.getEventArgs().preventDefault();
  }
}