Dynamics 365:识别快速创建表单来源字段并设置默认值
实现方案
方案1:通过URL参数传递来源标识
当从不同Lookup字段触发快速创建时,自定义打开URL并携带来源参数,在快速创建表单加载时读取参数并设置默认值:
- 操作步骤:
- 为主表单的两个Lookup字段自定义点击事件,替换默认的快速创建URL,添加自定义参数(如
source=lookupField1或source=lookupField2),同时带上主记录的ID、实体类型、名称等必要信息。 - 在快速创建表单的
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'); // 同上逻辑设置对应字段 } } - 确保主表单传递的URL参数完整,避免缺失关联记录信息。
- 为主表单的两个Lookup字段自定义点击事件,替换默认的快速创建URL,添加自定义参数(如
方案2:利用Unified Interface上下文识别来源
在Dynamics 365 Unified Interface环境中,可通过表单上下文获取来源表单的字段信息,判断快速创建的打开来源:
- 操作步骤:
- 在快速创建表单的
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); } }
navigation.getFormContext()API。 - 在快速创建表单的
方案3:隐藏字段传递来源标识
通过实体新增的隐藏字段临时存储来源信息,实现默认值设置:
- 操作步骤:
- 在目标实体中新增一个单行文本字段(如
sourceIdentifier),设置为表单隐藏状态。 - 主表单中,为两个Lookup字段的快速创建按钮添加点击脚本,先设置主表单上
sourceIdentifier的值(如lookup1或lookup2),再打开快速创建表单。 - 快速创建表单
OnLoad脚本中,读取上下文传递的sourceIdentifier值,根据值设置对应字段默认值,操作完成后清空该隐藏字段,避免影响后续记录创建。
- 在目标实体中新增一个单行文本字段(如
内容的提问来源于stack exchange,提问作者Kostas Karagiannis
相关产品推荐
相关产品推荐

