检索多个记录OData java脚本Microsoft Dynamics CRM

时间:2013-03-25 05:41:52

标签: javascript dynamics-crm-2011

我使用以下java脚本代码按帐户ID检索联系人。我设置了警报消息调试。它没有输入成功的回叫消息功能。

结束时出现以下错误

  

检索时出错   “error”:{“lang”:“en-US”,“Value”:“位置20处的语法错误'\ ufffd'” }

我正在使用以下代码。

function retrieveMultiple(odataSetName, select, filter, successCallback) {
    var serverUrl = Xrm.Page.context.getServerUrl();
    var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
    var odataUri = serverUrl + ODATA_ENDPOINT + "/" + odataSetName + "?";

    alert("retrieveMultiple"+odataUri);

    if (select) {
        odataUri += "$select=" + select + "&";
        alert("select error="+odataUri);
    }

    if (filter) {
       odataUri += "$filter=" + filter;
       alert("filter error="+odataUri);
    }

    $.ajax({
       type: "GET",
       contentType: "application/json; charset=utf-8",
       datatype: "json",
       url: odataUri,
       beforeSend: function (XMLHttpRequest) {
           //Specifying this header ensures that the results will be returned as JSON.
           var x = XMLHttpRequest.setRequestHeader("Accept", "application/json");
           alert(" in Ajax :beforeSend:" + x );
       },
       success: function (data, textStatus, XmlHttpRequest) {
           alert("In success function outside success");

           if (successCallback) {
               alert("successCallback in if");

               if (data && data.d && data.d.results) {
                   alert("data && data.d && data.d.results"+data + data.d + data.d.results);
                   successCallback(data.d.results, textStatus, XmlHttpRequest);
                   alert("data.d.results, textStatus, XmlHttpRequest" + data.d.results + textStatus +       XmlHttpRequest);
                }
                else if (data && data.d) {
                     successCallback(data.d, textStatus, XmlHttpRequest);
                }
           else {
               successCallback(data, textStatus, XmlHttpRequest);
           }
       }
    },
    error: function (XmlHttpRequest, textStatus, errorThrown) {
        alert(" In erro function");

        if (XmlHttpRequest && XmlHttpRequest.responseText) {
           alert(" In error function If");
           alert("Error while retrieval ; Error – " + XmlHttpRequest.responseText);
        }
    }
});
}

function readRecordsOnSuccess(data, textStatus, XmlHttpRequest) {
    // Loop through the retrieved records
    for (var indx = 0; indx < data.length; indx++) {
         alert("Name – " + data[indx].name);
    }
}

function retrieveContactsByAccountId() {
    // Pass ‘Contact’ set name since we are reading Contacts
    var oDataSetName = "ContactSet";

    // Column names of ‘Contact’ (Pass * to read all columns)
    var columns = "FirstName";

    // Read Account Guid
    var accountId = Xrm.Page.data.entity.getId()

    // Prepare filter
    var filter = "AccountId/Id eq guid’" + accountId + "‘";

    alert("retrieveContactsByAccountId"+filter);

    retrieveMultiple(oDataSetName, columns, filter, readRecordsOnSuccess);
}

1 个答案:

答案 0 :(得分:1)

看起来像常见的错误类型;)请注意您传递的字符串:

var filter = "AccountId/Id eq guid’" + accountId + "‘";

你的撇号是常规different

ones

您需要定期使用'

var filter = "AccountId/Id eq guid'" + accountId + "'";