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

Dynamics 365:识别快速创建表单来源字段并设置默认值

实现方案

方案1:通过URL参数传递来源标识

当从不同Lookup字段触发快速创建时,自定义打开URL并携带来源参数,在快速创建表单加载时读取参数并设置默认值:

  • 操作步骤:
    1. 为主表单的两个Lookup字段自定义点击事件,替换默认的快速创建URL,添加自定义参数(如source=lookupField1或source=lookupField2),同时带上主记录的ID、实体类型、名称等必要信息。
    2. 在快速创建表单的OnLoad事件中添加以下JavaScript代码:
      function setDefaultFromSource(executionContext) {
          const formContext = executionContext.getFormContext();
          const urlParams = new URLSearchParams(window.location.search);
          const source = urlParams.get('source');
          
          if (source === 'lookupField1') {
              // 从第一个Lookup打开,设置对应关联字段默认值
              const parentId = urlParams.get('parentId');
              const parentType = urlParams.get('parentType');
              const parentName = urlParams.get('parentName');
              if (parentId && parentType) {
                  formContext.getAttribute('targetLookupField').setValue([{
                      id: parentId,
                      entityType: parentType,
                      name: parentName
                  }]);
              }
          } else if (source === 'lookupField2') {
              // 从第二个Lookup打开,设置另一字段默认值
              const otherParentId = urlParams.get('otherParentId');
              // 同上逻辑设置对应字段
          }
      }
      
    3. 确保主表单传递的URL参数完整,避免缺失关联记录信息。

方案2:利用Unified Interface上下文识别来源

在Dynamics 365 Unified Interface环境中,可通过表单上下文获取来源表单的字段信息,判断快速创建的打开来源:

  • 操作步骤:
    1. 在快速创建表单的OnLoad事件中添加脚本:
      function detectSourceFromContext(executionContext) {
          const formContext = executionContext.getFormContext();
          const sourceFormContext = formContext.context.navigation.getFormContext();
          
          if (!sourceFormContext) return;
          
          // 判断来源是哪个Lookup字段
          if (sourceFormContext.getAttribute('lookupField1')?.getValue()) {
              const lookupValue = sourceFormContext.getAttribute('lookupField1').getValue();
              formContext.getAttribute('targetField1').setValue(lookupValue);
          } else if (sourceFormContext.getAttribute('lookupField2')?.getValue()) {
              const lookupValue = sourceFormContext.getAttribute('lookupField2').getValue();
              formContext.getAttribute('targetField2').setValue(lookupValue);
          }
      }
      
    注意:该方法仅适用于Unified Interface环境,需验证当前系统版本支持navigation.getFormContext() API。

方案3:隐藏字段传递来源标识

通过实体新增的隐藏字段临时存储来源信息,实现默认值设置:

  • 操作步骤:
    1. 在目标实体中新增一个单行文本字段(如sourceIdentifier),设置为表单隐藏状态。
    2. 主表单中,为两个Lookup字段的快速创建按钮添加点击脚本,先设置主表单上sourceIdentifier的值(如lookup1或lookup2),再打开快速创建表单。
    3. 快速创建表单OnLoad脚本中,读取上下文传递的sourceIdentifier值,根据值设置对应字段默认值,操作完成后清空该隐藏字段,避免影响后续记录创建。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 20:55:21