javascript - 一个 Javascript 中的 2 个 XMLHTTPRequest(Dynamics CRM)
问题描述
我目前正在尝试在动态 CRM Javascript 中运行 2 个单独的 XMLHTTPRequest,以从 2 个不同的实体中检索数据并根据检索到的内容运行代码。
出于安全原因,我已尽力尝试编辑一些名称,但前提是相同的。我的主要问题是第一次运行 XMLHTTPRequest(RA 横幅)工作正常,但第二次运行(状态横幅)返回的 Readystate 为 2 并停止。
function FormBanners(formContext) {
//Clear the existing banners
formContext.ui.clearFormNotification("Notif1");
formContext.ui.clearFormNotification("Notif2");
//Get the customer/rep
var customer = formContext.getAttribute("customerid").getValue();
var rep = formContext.getAttribute("representative").getValue();
var contact;
//use the rep if there is one else use the customer
if (rep != null) {
contact = rep;
}
else if (customer!= null) {
contact = customer;
}
//Get the account
var account = formContext.getAttribute("accountfield").getValue();
//As there is a requirement for 2 XMLHTTPRequests we have to queue them
var requestURLs = new Array();
//There will always be a customers or rep on the form
requestURLs.push(Xrm.Page.context.getClientUrl() + "/api/data/v9.1/contacts?$select=new_RA,new_SC,new_VC,new_PR&$filter=contactid eq " + contact[0].id + "", true);
//there may not be an account
if (account) {
requestURLs.push(Xrm.Page.context.getClientUrl() + "/api/data/v9.1/accounts?$select=_new_statusLookup_value&$filter=accountid eq " + account[0].id + "", true);
}
var current = 0;
function getURL(url) {
var req = new XMLHttpRequest();
req.open("GET", requestURLs[current]);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
req.onreadystatechange = function () {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {
var result = JSON.parse(req.response);
// Creation of the RA Banner
if (current == 0) {
var RA = result.value[0]["new_RA@OData.Community.Display.V1.FormattedValue"];
var SC = result.value[0]["new_SC@OData.Community.Display.V1.FormattedValue"];
var VC = result.value[0]["new_VC@OData.Community.Display.V1.FormattedValue"];
var PR = result.value[0]["new_PR@OData.Community.Display.V1.FormattedValue"];
var Notif = "";
//Only create a notification if any of the above contain "Yes"
if (RA == "Yes" || SC == "Yes" || VC == "Yes" || PR == "Yes") { Notif = "The Customer/Rep has:" }
if (RA == "Yes") { Notif = Notif + " RA,"; }
if (SC == "Yes") { Notif = Notif + " SC,"}
if (VC == "Yes") { Notif = Notif + " VC,"}
if (PR == "Yes") { Notif = Notif + " PR."}
if (Notif != "") {
formContext.ui.setFormNotification(Notif, "INFO", "Notif1");
}
}
//Creation of the Status Banner
else if (current == 1) {
status = results.value[i]["_new_statusLookup_value"];
if (status) {
if (status[0].name != "Open")
var Notif = "The status for the organisation on this case is " + status[0].name + ".";
formContext.ui.setFormNotification(Notif, "INFO", "Notif2");
}
}
++current;
if (current < requestURLs.length) {
getURL(requestURLs[current]);
}
} else {
Xrm.Utility.alertDialog(req.statusText);
}
}
}; req.send();
}
getURL(requestURLs[current]);
}
谁能帮我吗?
解决方案
所以在我的例子中,我犯了很多错误。接受 Arun 的建议,我将函数分离出来,这使得调试变得更加容易......相反,它在 XMLHTTP 请求上并没有失败,因为它运行良好,但 Firefox 在调试时崩溃了。
所以我的问题是我在第二部分:
- 将 i 的值用于结果(不再定义 i)
- 获取“_new_statusLookup_value”只会提供查找的 guid,而我想要“_new_statusLookup_value@OData.Community.Display.V1.FormattedValue”
- 让我们不要忽视这样一个事实,因为我已经复制并粘贴了此代码的许多迭代,所以我也使用“结果”而不是“结果”。
许多小错误..但它发生了!感谢您的帮助,只是去展示..拼写错误是我们的失败!
推荐阅读
- nunit - 在 Visual Studio 2019 上运行 Nunit 项目后,我没有看到任何测试运行
- java - 如何在 JButton 上使用 ActionListener 来更改 JPanel 容器内的 JLabel 组件的背景?
- java - GSON StackOverFlow 错误 - 使用带有继承的 Lombok 时的序列化
- php - 在 WooCommerce 中的产品 (Schema.org) 的结构化数据中添加 ean 代码 (gtin)
- javascript - 如何获得独立于设备的 UTC 时间?
- javascript - 文件不是通过序列化插入 javascript
- django - 在 transaction.atomic() 中创建期间未抛出 IntegrityError
- javascript - 无法使用本机反应(iOS)同时播放多个 youtube
- python - 传递给函数的参数不能按预期工作
- fortran - Fortran + BLAS:完全对称 rank-1 沿对角线更新/镜像矩阵块