Xrm.Utility.openEntityForm的使用

本文详细介绍了如何使用Xrm.Utility.openEntityForm方法快速打开实体Form并直接设置默认值。通过实例展示了如何为查找类型、文本类型、日期类型和选项集类型字段赋值,并解释了URL字符串长度限制问题及解决方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

通常需要直接打开某一个form,并且有时需要在打开时直接设置默认值。
SDK中直接给提供了下面这个方法: Xrm.Utility.openEntityForm
这里写图片描述
结合我个人的实际例子再进行说明:

OpenOrder = function () {

    var parameters = {};
    // 为查找类型字段赋值
    parameters["new_servicerequset"] = Xrm.Page.data.entity.getId();
    parameters["new_servicerequsetname"] = Xrm.Page.getAttribute("new_requestname").getValue();

    // 为文本类型字段进行赋值
    parameters["new_workordername"] = Xrm.Page.getAttribute("new_requestname").getValue();

    // 为日期类型字段进行赋值  
    parameters["new_receiving_date"] = "2015-11-15 16:00:00.000";
    // 或者
    parameters["new_receiving_date"] = Xrm.Page.getAttribute("new_expected_date").getValue().toDateString();

    // 为选项集类型字段进行赋值     
    parameters["new_type"] = Xrm.Page.getAttribute("new_type").getValue();

    // 打开实体Form
    Xrm.Utility.openEntityForm("new_serviceworkorder", null, parameters);
}

Note:
因为最后会拼接成url字符串,所以赋值时不能直接赋值为object.必须转化为字符串。
例如对日期类型的赋值。

在调用这个方法时通常会出现下面这个错误。

这里写图片描述

觉得很奇怪,调查之后发现在我们调用 Xrm.Utility.openEntityForm(“new_serviceworkorder”, null, parameters);方法时,最后会把我们传递的参数都拼接成URL字符串,而通常浏览器会对URL的长度有最大限制。

解决方案可以参考下面这个链接:
http://www.cnblogs.com/luchaocai/p/3182510.html
这里写图片描述

微软官方给出的方案如下:

RESOLUTION
To resolve this problem, follow these steps:
Click Start, type Notepad in the Start Search box, right-click Notepad, and then click Run as administrator.

Note If you are prompted for an administrator password or for a confirmation, type the password, or click Continue.

On the File menu, click Open, type %windir%\System32\inetsrv\config\applicationHost.config in the File name box, and then click Open.
In the ApplicationHost.config file, locate the requestLimits tag.
Remove the maxQueryString property. Or, change the value to a value that matches the length of the query string that you are using. By default, the value of the maxQueryString property is 2048.

For example, modify the following code.

Save the ApplicationHost.config file.

<think>我们正在讨论的是Xrm.Navigation.openConfirmDialog的回调处理方式。根据之前的讨论,我们知道这个方法返回一个Promise,因此可以使用.then()和.catch()来处理回调,或者使用async/await。 用户询问的是回调函数的使用方法,在JavaScript中,处理异步操作的传统方式是使用回调函数,但Xrm.Navigation.openConfirmDialog返回的是Promise,因此它本身并不直接接受一个回调函数作为参数。但是,我们可以通过Promise的.then()方法来模拟回调函数的风格。 因此,我们可以这样使用: 1. 使用.then()方法处理用户确认后的回调。 2. 使用.catch()处理错误。 示例代码: ```javascript Xrm.Navigation.openConfirmDialog({ text: "您确定要执行此操作吗?", title: "确认操作" }).then(function (result) { // 用户点击了确认或取消后,会进入这个回调函数 if (result.confirmed) { // 用户点击了确认 console.log("用户确认"); // 执行确认后的操作 } else { // 用户点击了取消 console.log("用户取消"); // 执行取消后的操作 } }).catch(function (error) { // 处理错误,例如弹窗无法打开等 console.error(error); }); ``` 另外,在之前的回答中,我们提到了async/await的方式,但用户这里明确问回调函数的使用,所以主要展示回调函数风格(即.then()方式)。 注意:在Dynamics 365中,这个API是异步的,所以不能使用同步的方式去等待结果,必须使用回调或者异步函数。 引用[1]是关于设置字段必填和显示的,与当前问题无关,因此不需要引用。 由于用户的问题是关于回调函数的使用,我们不需要引入同步调用的问题,所以只专注于回调处理。 现在,我们可以按照以下结构回答: 1. 说明openConfirmDialog返回Promise,因此使用.then()作为回调处理。 2. 给出示例代码。 3. 说明回调函数中的参数(result对象)的含义。 4. 错误处理。 同时,生成相关问题。 注意:我们之前已经讨论过异步和同步的问题,现在用户明确问回调函数,所以重点放在回调上。 生成的相关问题可以围绕Promise的回调处理、其他异步方法等。</think>### Xrm.Navigation.openConfirmDialog 回调函数使用方法详解 `Xrm.Navigation.openConfirmDialog` 采用 **Promise-based 回调机制**,通过 `.then()` 方法处理用户响应。以下是完整的使用方法: #### 1. 基础回调结构 ```javascript Xrm.Navigation.openConfirmDialog(confirmOptions) .then(successCallback) // 处理用户响应 .catch(errorCallback); // 处理异常 ``` #### 2. 完整示例代码 ```javascript // 1. 定义确认对话框参数 const confirmOptions = { text: "确定要删除此客户记录吗?", // 弹窗内容 title: "删除确认", // 弹窗标题 height: 200, // 弹窗高度(px) width: 450 // 弹窗宽度(px) }; // 2. 打开弹窗并处理回调 Xrm.Navigation.openConfirmDialog(confirmOptions) .then(function (response) { // 3. 处理用户响应 if (response.confirmed) { // 用户点击"确认" const recordId = Xrm.Page.data.entity.getId().replace(/[{}]/g, ""); deleteRecord(recordId); // 执行删除操作 } else { // 用户点击"取消" Xrm.Page.ui.setFormNotification("操作已取消", "INFO", "cancel_1"); // 引用[1][^1] } }) .catch(function (error) { // 4. 错误处理 console.error("弹窗打开失败:", error); Xrm.Page.ui.setFormNotification("系统错误,请重试", "ERROR", "error_1"); }); // 删除记录的函数示例 function deleteRecord(recordId) { Xrm.WebApi.deleteRecord("account", recordId) .then(function() { Xrm.Navigation.openAlertDialog({ text: "记录已删除" }); }); } ``` #### 3. 关键参数说明 | 参数 | 类型 | 说明 | 必填 | |------|------|------|------| | `response.confirmed` | Boolean | `true`=用户点击确认<br>`false`=用户点击取消 | ✓ | | `confirmOptions.text` | String | 弹窗显示的主文本内容 | ✓ | | `confirmOptions.title` | String | 弹窗标题栏文本 | ✕ | | `confirmOptions.height` | Number | 弹窗高度(像素) | ✕ | | `confirmOptions.width` | Number | 弹窗宽度(像素) | ✕ | #### 4. 回调执行流程 ```mermaid sequenceDiagram participant User participant D365 as Dynamics 365 participant Callback User->>D365: 触发操作(如点击按钮) D365->>D365: 调用 openConfirmDialog() D365-->>User: 显示确认弹窗 User->>D365: 点击"确认"或"取消" D365->>Callback: 执行.then(response) alt 用户确认 Callback->>Callback: response.confirmed=true Callback->>D365: 执行确认操作 else 用户取消 Callback->>Callback: response.confirmed=false Callback->>D365: 执行取消操作 end ``` #### 5. 最佳实践 1. **必填参数检查** 在回调中验证关键参数: ```javascript .then(function(response) { if (typeof response.confirmed === "undefined") { throw new Error("无效的弹窗响应"); } // ...其他逻辑 }) ``` 2. **UI状态更新** 根据结果更新界面元素: ```javascript if (response.confirmed) { Xrm.Page.getControl("status").setValue("已确认"); // 引用[1][^1] } ``` 3. **多语言支持** 使用资源文件实现本地化: ```javascript const text = RESX.get("DeleteConfirmation"); const title = RESX.get("WarningTitle"); ``` 4. **移动端适配** 调整弹窗尺寸适应小屏幕: ```javascript const options = Xrm.Utility.getGlobalContext().client.getClient() === "Mobile" ? { height: 150, width: 300 } : { height: 200, width: 450 }; ``` #### 6. 常见错误处理 ```javascript .catch(function(error) { // 弹窗被阻止 if (error.message.includes("blocked")) { Xrm.Navigation.openAlertDialog({ text: "请允许弹窗权限" }); } // 参数错误 else if (error.errorCode === -2147220991) { console.warn("无效参数:", error.message); } // 其他错误 else { console.error("错误代码:", error.errorCode); } }); ``` > **重要提示**:在回调函数中避免直接修改未保存的表单数据,应先检查表单状态: > ```javascript > if (Xrm.Page.data.entity.getIsDirty()) { > Xrm.Navigation.openAlertDialog("请先保存表单"); > return; > } > ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值