简体   繁体   English

2 个 XMLHTTPRequests 在一个 Javascript (Dynamics CRM)

[英]2 XMLHTTPRequests in one Javascript (Dynamics CRM)

I am currently trying to run 2 separate XMLHTTPRequests in dynamics CRM Javascript to retrieve data from 2 different entities and run code depending on what is retrieved..我目前正在尝试在动态 CRM Javascript 中运行 2 个单独的 XMLHTTPRequest,以从 2 个不同的实体中检索数据并根据检索到的内容运行代码。

I've done my best to try and edit some names for security reasons, but the premise is the same.出于安全原因,我已尽力尝试编辑一些名称,但前提是相同的。 My main problem is that the first run of the XMLHTTPRequest (the RA Banner) works fine, but then the second run (the Status Banner) the Readystate that is returned is 2 and stopping.我的主要问题是第一次运行 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]);
}

Can anyone help me out?谁能帮我吗?

So in my example I had made many mistakes.所以在我的例子中,我犯了很多错误。 Taking Arun's advice I separated the function out, which made debugging far easier.. it instead was not failing on the XMLHTTP Request because that worked fine, but firefox was crashing when debugging.听从 Arun 的建议,我将 function 分开,这使得调试变得更加容易.. 它并没有在 XMLHTTP 请求上失败,因为它工作正常,但是 firefox 在调试时崩溃了。

So my issues were that I was in the second part:所以我的问题是我在第二部分:

  • Used the value of i for results (i was no longer defined)将 i 的值用于结果(不再定义 i)
  • Getting the "_new_statusLookup_value" would only provide the guid of the lookup I would instead want "_new_statusLookup_value@OData.Community.Display.V1.FormattedValue"获取“_new_statusLookup_value”只会提供查找的 guid,而我想要“_new_statusLookup_value@OData.Community.Display.V1.FormattedValue”
  • Lets not ignore the fact that because I had copied and pasted many iterations of this code that I was also using "results" instead of "result".让我们不要忽视这样一个事实,因为我已经复制并粘贴了此代码的许多迭代,所以我也使用“结果”而不是“结果”。

Many little mistakes.. but it happens, Thanks for the help.许多小错误..但它发生了,感谢您的帮助。 just goes to show.. typos are our downfall!只是去展示..拼写错误是我们的失败!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM