标签:asc model util local comm uid lan 转换 value
function UTCToLocalTime(d) { var timeOffset = -((new Date()).getTimezoneOffset() / 60); d.setHours(d.getHours() + timeOffset); return d; } function ConvertStringToDatetime(dateString) { var reggie = /(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})Z/; var dateArray = reggie.exec(dateString); var dateObject = new Date( (+dateArray[1]), (+dateArray[2]) - 1, (+dateArray[3]), (+dateArray[4]), (+dateArray[5]), (+dateArray[6]) ); return dateObject; } function NameOnchangeHandler() { var clientURL = Xrm.Page.context.getClientUrl(); var req = new XMLHttpRequest() req.open("GET", encodeURI(clientURL + "/api/data/v8.0/ly_tests?$select=ly_name,ly_alternatekey,ly_bool,ly_datetime,ly_decimal,ly_float,_ly_lookup_value,ly_integer,ly_money,ly_multilinetext,ly_optionset,ly_singlelinetext&$orderby=ly_datetime asc&$filter=contains(ly_name,‘Update消息测试‘)&$count=true"), true); req.setRequestHeader("Accept", "application/json"); req.setRequestHeader("Content-Type", "application/json; charset=utf-8"); req.setRequestHeader("OData-MaxVersion", "4.0"); req.setRequestHeader("OData-Version", "4.0"); req.onreadystatechange = function () { if (this.readyState == 4) { req.onreadystatechange = null; if (this.status == 200) { var responseJSON = JSON.parse(this.responseText); Xrm.Utility.alertDialog("本次查询返回符合条件的记录条数:" + responseJSON["@odata.count"]); if (responseJSON.value != null && responseJSON.value.length >= 1) { for (var i = 0; i < responseJSON.value.length; i++) { Xrm.Utility.alertDialog("ly_name=" + responseJSON.value[i].ly_name);//单行文本 Xrm.Utility.alertDialog("ly_alternatekey=" + responseJSON.value[i].ly_alternatekey);//单行文本 Xrm.Utility.alertDialog("ly_bool=" + responseJSON.value[i].ly_bool);//两个选项 Xrm.Utility.alertDialog("ly_datetime=" + UTCToLocalTime(ConvertStringToDatetime(responseJSON.value[i].ly_datetime)));//日期和时间,需要将UTC时间转换为本地时间 Xrm.Utility.alertDialog("ly_decimal=" + responseJSON.value[i].ly_decimal);//十进制数 Xrm.Utility.alertDialog("ly_float=" + responseJSON.value[i].ly_float);//浮点数 Xrm.Utility.alertDialog("ly_lookup=" + responseJSON.value[i]._ly_lookup_value);//查找字段,只有记录的主键Guid,且前面加下划线,后面加_value Xrm.Utility.alertDialog("ly_integer=" + responseJSON.value[i].ly_integer);//整数 Xrm.Utility.alertDialog("ly_money=" + responseJSON.value[i].ly_money);//货币 Xrm.Utility.alertDialog("ly_multilinetext=" + responseJSON.value[i].ly_multilinetext);//多行文本 Xrm.Utility.alertDialog("ly_optionset=" + responseJSON.value[i].ly_optionset);//选项集 Xrm.Utility.alertDialog("ly_singlelinetext=" + responseJSON.value[i].ly_singlelinetext);//单行文本 Xrm.Utility.alertDialog("主键ly_testid=" + responseJSON.value[i].ly_testid);//主键 Xrm.Utility.alertDialog("币种_transactioncurrencyid_value=" + responseJSON.value[i]._transactioncurrencyid_value);//币种 } } } else { var error = JSON.parse(this.responseText).error; Xrm.Utility.alertDialog("查询罗勇测试实体记录出错." + error.message); } } }; req.send(); }
下面例子是根据主键查询一条记录:
function UTCToLocalTime(d) { var timeOffset = -((new Date()).getTimezoneOffset() / 60); d.setHours(d.getHours() + timeOffset); return d; } function ConvertStringToDatetime(dateString) { var reggie = /(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})Z/; var dateArray = reggie.exec(dateString); var dateObject = new Date( (+dateArray[1]), (+dateArray[2]) - 1, (+dateArray[3]), (+dateArray[4]), (+dateArray[5]), (+dateArray[6]) ); return dateObject; } function NameOnchangeHandler() { var clientURL = Xrm.Page.context.getClientUrl(); var req = new XMLHttpRequest() req.open("GET", encodeURI(clientURL + "/api/data/v8.0/ly_tests(278FF7E9-22B4-E511-80DA-000D3A400088)?$select=ly_name,ly_alternatekey,ly_bool,ly_datetime,ly_decimal,ly_float,_ly_lookup_value,ly_integer,ly_money,ly_multilinetext,ly_optionset,ly_singlelinetext"), true); req.setRequestHeader("Accept", "application/json"); req.setRequestHeader("Content-Type", "application/json; charset=utf-8"); req.setRequestHeader("OData-MaxVersion", "4.0"); req.setRequestHeader("OData-Version", "4.0"); req.onreadystatechange = function () { if (this.readyState == 4) { req.onreadystatechange = null; if (this.status == 200) { var responseJSON = JSON.parse(this.responseText); Xrm.Utility.alertDialog("ly_name=" + responseJSON.ly_name);//单行文本 Xrm.Utility.alertDialog("ly_alternatekey=" + responseJSON.ly_alternatekey);//单行文本 Xrm.Utility.alertDialog("ly_bool=" + responseJSON.ly_bool);//两个选项 Xrm.Utility.alertDialog("ly_datetime=" + UTCToLocalTime(ConvertStringToDatetime(responseJSON.ly_datetime)));//日期和时间,需要将UTC时间转换为本地时间 Xrm.Utility.alertDialog("ly_decimal=" + responseJSON.ly_decimal);//十进制数 Xrm.Utility.alertDialog("ly_float=" + responseJSON.ly_float);//浮点数 Xrm.Utility.alertDialog("ly_lookup=" + responseJSON._ly_lookup_value);//查找字段,只有记录的主键Guid Xrm.Utility.alertDialog("ly_integer=" + responseJSON.ly_integer);//整数 Xrm.Utility.alertDialog("ly_money=" + responseJSON.ly_money);//货币 Xrm.Utility.alertDialog("ly_multilinetext=" + responseJSON.ly_multilinetext);//多行文本 Xrm.Utility.alertDialog("ly_optionset=" + responseJSON.ly_optionset);//选项集 Xrm.Utility.alertDialog("ly_singlelinetext=" + responseJSON.ly_singlelinetext);//单行文本 Xrm.Utility.alertDialog("主键ly_testid=" + responseJSON.ly_testid);//主键 Xrm.Utility.alertDialog("币种_transactioncurrencyid_value=" + responseJSON._transactioncurrencyid_value);//币种 } else { var error = JSON.parse(this.responseText).error; Xrm.Utility.alertDialog("查询罗勇测试实体记录出错." + error.message); } } }; req.send(); }
如果是查询实体的查找字段对应实体记录的某个字段值还有类似如下更快捷方法:
从Dynamics 365 V9版本开始提供了新的API方便客户端编程调用,请优先使用,具体请参考官方文档:retrieveRecord (Client API reference) 和 retrieveMultipleRecords (Client API reference) 。
更多信息请参考官方文档:Query Data using the Web API 、 Retrieve an entity record using the Web API 和 Retrieve related entity records with a query 。
Dynamics CRM 2015/2016新特性之二十一:使用Web API查询记录
标签:asc model util local comm uid lan 转换 value
原文地址:https://www.cnblogs.com/luoyong0201/p/Dynamics_365_Web_API_Query_Data.html