简体   繁体   English

CRM Odata和Javascript

[英]CRM Odata and Javascript

I am making my first CRM project - it's autoupdate field from some other entity. 我正在做我的第一个CRM项目-它是其他实体的自动更新字段。 I read a little and gave it a try. 我读了一点,然后尝试一下。 I have: 我有:

var Code = Xrm.Page.getAttribute("new_codeid").getValue();
var oDataPath = Xrm.Page.context.getServerUrl() + "/xrmservices/2011/organizationdata.svc" ;

var Query = "/new_codesSet?" +
    "$select=new_city" + 
    "&$filter=new_code eq '" + Code + "'" + 
    "&$top=1";

var Record_Request = new XMLHttpRequest();
Record_Request.open("GET", oDataPath + Query, true);    
Record_Request.setRequestHeader("Accept", "application/json");
Record_Request.setRequestHeader("Content-Type", "application/json; charset=utf-8");
Record_Request.onreadystatechange = function () 
{
    var Value = "";
    if (this.readyState == 4) 
    {
        if (this.status == 200) 
        {
            var RecordSet = JSON.parse(Record_Request.responseText).d;
            if(RecordSet.results.length > 0)
            {
                Value = RecordSet.new_city;
        }       
    }
}; 
Xrm.Page.getAttribute(address1_city).setValue(Value);

I get Unknown Error . 我收到Unknown Error How can I debug it? 我该如何调试? What I am doing wrong? 我做错了什么?

If attribute "new_codeid" is a lookup type (as I presume), .getValue() returns an array of objects. 如果属性“ new_codeid”是一种查找类型(如我所假定),则.getValue()返回对象数组。 (This is because a few lookup fields are capable of holding more than one lookup reference.) (这是因为一些查询字段可以容纳多个查询引用。)

So, in your filter you only need the id property of the first item in the array. 因此,在您的过滤器中,您只需要数组中第一项的id属性。 Modify the first line of your code to this: 将代码的第一行修改为此:

var Code = Xrm.Page.getAttribute("new_codeid").getValue()[0].id;

(Needless to say this only works when "new_codeid" actually has a value.) (不用说,这仅在“ new_codeid”实际上具有值时才有效。)

Best way to debug when using IE is to set your browser to allow script debugging, Options > Advanced > un-tick 'disable script debugging'. 使用IE进行调试的最佳方法是将浏览器设置为允许脚本调试,请选择选项>高级>取消选中“禁用脚本调试”。

in your Jscript use debugger; 在您的Jscript中使用调试器; to breakpoint your code, that'll allow you to jump into your code using visual studio for example. 断点代码,例如,可以使用Visual Studio跳入代码。

btw > just check in your second go you are using a variable called PostCodeId.Replace and assigning it to CodeId , what is PostCodeId 's value and are you attempting to overwrite CodeId with it? 顺便说一句>再次检查一下,您正在使用一个名为PostCodeId.Replace的变量并将其分配给CodeIdPostCodeId的值是什么,您是否正在尝试用它覆盖CodeId

I've taken the liberty of writing your code as follow: 我已经自由地编写您的代码,如下所示:

var CodeId = Xrm.Page.getAttribute("new_codeid").getValue();
debugger; //inseting a breakpoint as explained.
if (CodeId != null) {
    var serverURL = Xrm.Page.context.getClientUrl();
    var oDataSelect = serverURL +
            "/xrmservices/2011/OrganizationData.svc/new_codeSet?" +
            "$select=new_city" +
            "&$filter=new_code/Id eq guid'" + CodeId[0].id + "'" +
            "&$top=1"; //make sure this is correct by testing this build url in you browser

    $.ajax({
        type: "GET",
        contentType: "application/json; charset=utf-8",
        datatype: "json",
        url: oDataSelect,
        beforeSend: function (XMLHttpRequest) {
            XMLHttpRequest.setRequestHeader("Accept", "application/json");
        },
        success: function (data, textStatus, XmlHttpRequest) {
            ReturnCity(data); ;

        },
        error: function (XmlHttpRequest, textStatus, errorObject) {
            alert("OData Execution Error Occurred");
        }
    });

    function ReturnCity(data) {
        if (data.d != null) {                 
            var value = data.d.results[0].new_city;
            Xrm.Page.getAttribute("address1_city").setValue(value);
        }
    };
}

let me know how it goes. 让我知道事情的后续。

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

相关问题 CRM动态:oData字符串javascript - CRM dynamics: oData string javascript CRM女士(使用odata查询)中的Javascript无法正常工作? - Javascript in Ms CRM (with odata query) is not working? crm 2011 IFD无法使用OData和Javascript - crm 2011 IFD is not working using OData and Javascript 如何从CRM 2011 javascript odata函数返回数据? - How to return data from CRM 2011 javascript odata function? 使用JavaScript中的ODATA从Sharepoint页面在Dynamics CRM上运行查询 - Run Query on Dynamics CRM from Sharepoint Page using ODATA in JavaScript 如何在CRM 2011中使用Javascript和oData获取PartyList字段的值 - How to get value of PartyList field using Javascript and oData in CRM 2011 CRM OData DateTime时区问题 - CRM OData DateTime Timezone Issue 如何使用odata和Javascript获取CRM 2011中的用户预览时间 - How to Get user preview time in CRM 2011 from using odata ,Javascript 使用OData端点和CRM 2013中的javascript将多个资源关联到服务约会实体 - Associate multiple resources to service appointment entity using OData endpoint with javascript in CRM 2013 使用JavaScript和REST OData查询CRM 2011 AppointmentSet时如何测试d.data.results中存在哪些数据 - How to test what data is present in d.data.results when querying CRM 2011 AppointmentSet using JavaScript and REST OData
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM