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

Model-Driven App中通过JS设置可编辑网格字段必填项报错求助

问题描述
  • 场景:Model-Driven App包含两个用户角色,需针对特定角色,将同一表单内多个可编辑网格的指定字段设为必填
  • 尝试方案及问题:
    • Power Automate流:无法适配表单实时字段控制场景
    • 业务规则:触发逻辑过快导致冲突,无法正常生效
  • 当前问题:采用Web资源JavaScript在表单OnLoad事件实现时,抛出错误 repairActivityGridEntity.getEntityMetadata is not a function
报错原因排查

getEntityMetadata() 是Xrm.Entity对象的专属方法,你获取的 repairActivityGridEntity 并非合法的实体对象——核心问题是可编辑网格属于子网格控件,不能通过常规实体对象方式直接获取,必须先定位子网格控件,再获取其数据源与实体元数据。

修正后的实现方案

核心逻辑

  1. 校验当前用户是否属于目标角色
  2. 遍历表单内的目标可编辑网格
  3. 针对每个网格,通过合法路径获取实体元数据,设置指定字段为必填

1. 用户角色校验函数

function checkUserRole(targetRoleName) {
    const currentUserRoles = Xrm.Utility.getGlobalContext().userSettings.roles;
    for (let i = 0; i < currentUserRoles.length; i++) {
        if (currentUserRoles[i].name === targetRoleName) {
            return true;
        }
    }
    return false;
}

2. 可编辑网格字段必填设置函数

function setEditableGridFieldsRequired(executionContext) {
    // 替换为你的目标角色名称
    const targetRole = "目标角色名称";
    if (!checkUserRole(targetRole)) return;

    // 定义需处理的网格及对应必填字段(逻辑名)
    const gridConfigs = [
        {
            gridName: "repairActivityGrid", // 子网格控件名称
            requiredFields: ["new_field1", "new_field2"]
        },
        {
            gridName: "secondEditableGrid", // 第二个可编辑网格控件名称
            requiredFields: ["new_field3"]
        }
    ];

    // 兼容新版统一界面,使用formContext替代Xrm.Page
    const formContext = executionContext.getFormContext();

    gridConfigs.forEach(config => {
        try {
            const gridControl = formContext.getControl(config.gridName);
            if (!gridControl || !gridControl.getGrid) {
                console.log(`未找到子网格控件:${config.gridName}`);
                return;
            }

            const grid = gridControl.getGrid();
            const dataSource = grid.getDataSource();

            // 正确获取实体元数据的方式
            dataSource.getEntityMetadata().then(entityMetadata => {
                config.requiredFields.forEach(fieldName => {
                    if (entityMetadata.attributes.get(fieldName)) {
                        gridControl.setColumnRequiredLevel(fieldName, "required");
                    } else {
                        console.log(`网格${config.gridName}中不存在字段:${fieldName}`);
                    }
                });
            }, error => {
                console.log(`获取实体元数据失败:${error.message}`);
            });
        } catch (e) {
            console.log(`处理网格${config.gridName}出错:${e.message}`);
        }
    });
}

3. 表单事件配置

  1. 将上述代码保存为类型为Script (JScript)的Web资源
  2. 在表单设计器中,为表单OnLoad事件添加该Web资源,调用setEditableGridFieldsRequired函数
  3. 勾选事件配置中的传递执行上下文选项
关键注意事项
  • 子网格控件名称需与表单设计器中配置的控件“名称”完全一致
  • 字段必须使用逻辑名,不能用显示名
  • 若仍使用旧版界面,可将formContext替换为Xrm.Page,但推荐优先使用新版统一界面的API

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 08:37:22