【问题标题】:Dynamics CRM: JavaScript GET request is not retrieving records using Web.ApiDynamics CRM:JavaScript GET 请求未使用 Web.Api 检索记录
【发布时间】:2017-04-20 10:50:28
【问题描述】:

我在事件表单上编写了以下 JS 函数来检索合同行,但函数没有做任何事情。我已经验证了 Fetch Query 并返回结果。所以数据肯定是存在的。我已经调试过了,看起来“this.readyState == 4”是假的。

谁能告诉我我的代码有什么问题。我需要添加任何程序集吗?

谢谢

function Test() {

    var customerId = Xrm.Page.getAttribute("parentcustomer").getValue();
    if (customerId == null) {
        return;
    }

    var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>" +
        "<entity name='contractdetail'>" +
        "<attribute name='contractid' />" +
        "<attribute name='contractdetailid' />" +
        "<filter type='and'>" +
        "<condition attribute='statuscode' operator='in'>" +
        "<value>2</value>" +
        "<value>1</value>" +
        "</condition>" +
        "<condition attribute='customerid' operator='eq' value='" +
        customerId[0].id +
        "' />" +
        "</filter>" +           
        "</entity>" +
        "</fetch>";

    var uri = "/contractdetail?fetchXml=" + encodeURIComponent(fetchXml);
    var clientUrl = Xrm.Page.context.getClientUrl();
    var webAPIPath = "/api/data/v8.1";   
    uri = clientUrl + webAPIPath + uri;

    var request = new XMLHttpRequest();
    request.open("GET", encodeURI(uri), false);
    request.setRequestHeader("Accept", "application/json");
    request.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    request.setRequestHeader("OData-MaxVersion", "4.0");
    request.setRequestHeader("OData-Version", "4.0");
    request.onreadystatechange = function() {
        if (this.readyState == 4 /* complete */) {
            request.onreadystatechange = null;

            switch (this.status) {
                case 200: // Success with content returned in response body.
                case 204: // Success with no content returned in response body.
                    var data = JSON.parse(this.response);
                    if (data && data.value) {
                    for (var indexContractLine = 0; indexContractLine < data.value.length; indexContractLine++) {
                        alert(data.value[indexContractLine].contractdetailid);
                        //alert(data.value[indexContractLine]['@odata.etag']);
                    }
                }
                    break;
                default: // All other statuses are unexpected so are treated like errors.
                    var error;
                    try {
                        error = JSON.parse(request.response).error;
                    } catch (e) {
                        error = new Error("Unexpected Error");
                    }
                    alert(error);
                    break;
            }

            if (this.status == 200) {
                var data = JSON.parse(this.response);
                if (data && data.value) {
                    for (var indexContractLine = 0; indexContractLine < data.value.length; indexContractLine++) {
                        alert(data.value[indexContractLine].contractdetailid);
                        alert(data.value[indexContractLine]['@odata.etag']);
                    }
                } else {
                    var error = JSON.parse(this.response).error;
                    alert(error.message);
                }
            }
        };
        request.send();
    }
}   

【问题讨论】:

标签: javascript asp.net-web-api dynamics-crm dynamics-crm-2016 dynamics-365


【解决方案1】:

onreadystatechange 内部,将this 替换为request

这是直接从生产环境中提取的代码示例(注意:这是一个 POC,它并不意味着要复制粘贴到您的代码中)以显示它最终应该是什么样子:

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
    if (xhttp.readyState == 4) {
        if (xhttp.status == 200) {
            xhttp.onreadystatechange = null; // avoid memory leaks
            var data = JSON.parse(xhttp.response);
            onsuccess(data);
        }
        else {
            var error = JSON.parse(xhttp.response).error;
            onerror(error);
        }
    }
};

【讨论】:

  • 谢谢@Alex,我想我在这里遗漏了一些东西。代码现在完全按照你的建议,但它仍然没有达到这个条件'if(request.readyState == 4)'
  • 我只是在这里很傻。实际上,我们正在升级到 365,而我工作的组织仍在 v8.0 上。现在 readyState 为 4 但状态为 404。
【解决方案2】:

您可能需要安装 Jason Lattimer 的 CRM RESTBuilder 解决方案来进行进一步测试。

https://github.com/jlattimer/CRMRESTBuilder

它提供了一个 GUI 来创建 Web API 查询,然后您可以根据自己的喜好对其进行测试和修改。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-25
    • 2022-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多