Dynamics CRM 2011 用Javascript 通过Odata 获取Entity

原文:http://stackoverflow.com/questions/7874562/crm-2011-getting-entity-with-javascript

 第一步:获取GUID

var customerItem =Xrm.Page.getAttribute("customerid").getValue();var customerID = customerItem[0].id;

 

 

第二步:获取oDataPath...

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

 

第三步:创建OData query
var filter ="/new_vehicleSet?"+"$select=new_vehicleId,new_Registration"+"&$filter=new_PrimaryDriver/Id eq (guid'"+ customerID +"')"+"&$orderby=new_LastAllocationDate desc"+"&$top=1";

 

 

第四步:获取数据

var retrieveRecordsReq =newXMLHttpRequest();
retrieveRecordsReq.open("GET", oDataPath + filter,true);
retrieveRecordsReq.setRequestHeader("Accept","application/json");
retrieveRecordsReq.setRequestHeader("Content-Type","application/json; charset=utf-8");
retrieveRecordsReq.onreadystatechange =function(){if(this.readyState ==4){if(this.status ==200){var retrievedRecords = JSON.parse(retrieveRecordsReq.responseText).d;if(retrievedRecords.results.length >0){var vehicle = retrievedRecords.results[0];SetLookup("new_replacedvehicle", vehicle.new_vehicleId, vehicle.new_Registration,"new_vehicle");}}}};
retrieveRecordsReq.send();

 

 第五步:对返回的Entity进行赋值操作

functionSetLookup(fieldName, idValue, textValue, typeValue){var value =newArray();
    value[0]=newObject();
    value[0].id = idValue;
    value[0].name = textValue;
    value[0].typename= typeValue;Xrm.Page.getAttribute(fieldName).setValue(value);}
 

 

posted @ 2013-09-24 15:34  Laser8000  阅读(321)  评论(0)    收藏  举报