You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Dynamics CRM 2016标签页失焦时批量字段校验的最优JS实现方法问询

实现Dynamics CRM 2016标签页失焦时的批量字段校验

Absolutely, using XRM/JavaScript is the optimal solution for your requirement. This approach avoids the clutter of attaching duplicate event handlers to each field, and centralizes the validation logic to only run when the user leaves your target tab—exactly what you're looking for.

Let's break down the step-by-step implementation:

1. 获取目标标签页的逻辑名称

首先打开Dynamics CRM 2016的表单编辑器,找到你要监控的标签页,打开它的属性面板,复制Name字段的值(这就是标签页的逻辑名称,比如tab_contactinfo),后续脚本需要用它来定位标签页。

2. 注册标签页状态变更事件

我们会在表单的OnLoad事件中绑定校验逻辑,确保表单加载完成后,标签页的状态变化能触发我们的校验函数。

表单OnLoad函数

将以下函数添加到你的Web资源中,然后绑定到表单的OnLoad事件(记得在事件设置里勾选“将执行上下文作为第一个参数传递”):

function formOnLoad(executionContext) {
    const formContext = executionContext.getFormContext();
    // 替换成你的目标标签页逻辑名称
    const targetTab = formContext.ui.tabs.get("tab_yourtargettab");
    
    // 给标签页绑定状态变更事件
    targetTab.addOnTabStateChange(validateTabFieldsOnBlur);
}

3. 编写校验逻辑

这个函数会在标签页状态变化时触发,我们先判断标签页是否正处于失焦状态(不再是激活状态),然后批量检查13个字段是否全部填写完成。

校验函数

function validateTabFieldsOnBlur(executionContext) {
    const formContext = executionContext.getFormContext();
    const targetTab = executionContext.getEventSource();
    
    // 仅在标签页失焦(不再激活)时执行校验
    if (targetTab.getActive()) {
        return; // 如果是用户切换进这个标签页,直接退出
    }
    
    // 替换成你需要检查的13个字段的逻辑名称
    const fieldsToValidate = [
        "new_field1", "new_field2", "new_field3",
        "new_field4", "new_field5", "new_field6",
        "new_field7", "new_field8", "new_field9",
        "new_field10", "new_field11", "new_field12", "new_field13"
    ];
    
    let allFieldsCompleted = true;
    
    // 遍历字段检查空值
    fieldsToValidate.forEach(fieldName => {
        const field = formContext.getAttribute(fieldName);
        if (!field) return; // 跳过表单上不存在的字段
        
        const fieldValue = field.getValue();
        const fieldType = field.getAttributeType();
        
        // 根据字段类型判断空值
        switch(fieldType) {
            case "string":
            case "memo":
                if (!fieldValue || fieldValue.trim() === "") {
                    allFieldsCompleted = false;
                }
                break;
            case "optionset":
            case "lookup":
            case "datetime":
            case "integer":
            case "decimal":
            case "money":
                if (fieldValue === null) {
                    allFieldsCompleted = false;
                }
                break;
            // 如果有布尔型等其他字段类型,可以在这里补充判断逻辑
            default:
                if (!fieldValue) {
                    allFieldsCompleted = false;
                }
        }
        
        if (!allFieldsCompleted) return; // 一旦发现空字段,提前终止循环
    });
    
    // 所有字段都填写完成时,弹出提示
    if (allFieldsCompleted) {
        Xrm.Utility.alertDialog("很棒!该标签页的所有字段都已填写完成。");
    }
}

关键注意事项

  • 字段逻辑名称准确性: 一定要确认你使用的是字段的逻辑名称(可在字段属性面板中查看),不要显示名称。
  • 字段类型适配: 脚本覆盖了常见的字段类型,如果你的13个字段包含特殊类型(如布尔型),可以在switch语句中补充对应的判断逻辑。
  • 测试场景: 记得测试切换标签页、折叠目标标签页、部分字段填写等场景,确保校验仅在标签页失焦时触发。

内容的提问来源于stack exchange,提问作者Damkulul

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:48:09