MS CRM 2011 在JScript中同步和异步使用REST Endpoint

简介:

在JScript中可以同步,也可以异步地使用REST Endpoint。以Retrieve为例,在下面的代码中,retrieveRecordsReq.open("GET", oDataPath + filter, false) 的第二个参数是false即为同步调用,如果为true即为异步调用。代码中其他部分同步与异步完全相同。

复制代码
if (typeof (ContactReadPaneLibrary) == "undefined") {
    ContactReadPaneLibrary = { __namespace: true };
}


ContactReadPaneLibrary = {
    Name: "ContactReadPaneLibrary",
    OnLoad: function () {
        if (parent.window.Xrm.Page.getAttribute("primarycontactid").getValue() != null && parent.window.Xrm.Page.getAttribute("primarycontactid").getValue().length > 0) {
            var primartycontact = parent.window.Xrm.Page.getAttribute("primarycontactid").getValue()[0];
            var contactid = primartycontact.id;
            var oDataPath = Xrm.Page.context.prependOrgName("/xrmservices/2011/organizationdata.svc");
            var filter = "/ContactSet(guid'" + contactid + "')?$select=Telephone1,FullName";
            var retrieveRecordsReq = new XMLHttpRequest();
            retrieveRecordsReq.open("GET", oDataPath + filter, false);
            retrieveRecordsReq.setRequestHeader("Accept", "application/json");
            retrieveRecordsReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
            retrieveRecordsReq.onreadystatechange = function () {
                ContactReadPaneLibrary.RetrieveContactCallBack(this);
            };
            retrieveRecordsReq.send();
        }
    },
    RetrieveContactCallBack: function (retrieveRecordsReq) {
        if (retrieveRecordsReq.readyState == 4 /* complete */) {
            if (retrieveRecordsReq.status == 200) {
                //Success
                //alert(retrieveRecordsReq.responseText);
                if (retrieveRecordsReq.responseText) {
                    var retrievedRecord = JSON.parse(retrieveRecordsReq.responseText).d;
                    document.getElementById("TextFullname").value = retrievedRecord.FullName;
                    document.getElementById("TextBusinessPhone").value = retrievedRecord.Telephone1;
                }
            }
            else {
                alert("Error : " + retrieveRecordsReq.status + ": " +
                    retrieveRecordsReq.statusText + ": " +
                    JSON.parse(retrieveRecordsReq.responseText).error.message.value);
            }
        }
    }
}
复制代码

 




本文转自JF Zhu博客园博客,原文链接:  http://www.cnblogs.com/jfzhu/archive/2013/02/12/2910622.html  ,如需转载请自行联系原作者







相关文章
|
3月前
|
供应链 中间件
SAP CRM 和 ERP 系统之间的主数据同步 - PRODUCT_R3_ADAPTER
SAP CRM 和 ERP 系统之间的主数据同步 - PRODUCT_R3_ADAPTER
|
中间件
SAP Cloud for Customer的产品主数据通过PI同步到CRM
SAP Cloud for Customer的产品主数据通过PI同步到CRM
SAP Cloud for Customer的产品主数据通过PI同步到CRM
使用SAP PI将CRM的Opportunity同步到C4C去
使用SAP PI将CRM的Opportunity同步到C4C去
使用SAP PI将CRM的Opportunity同步到C4C去
|
中间件
使用SAP PI和XIF Adapter将SAP CRM Opportunity同步到C4C系统
使用SAP PI和XIF Adapter将SAP CRM Opportunity同步到C4C系统
104 0
使用SAP PI和XIF Adapter将SAP CRM Opportunity同步到C4C系统
|
XML 中间件 Go
使用SAP CRM External Interface进行订单同步
使用SAP CRM External Interface进行订单同步
120 0
使用SAP CRM External Interface进行订单同步