之前的博文只介绍了通过记录的primary key来查询单条记录或者单个属性值,本篇介绍多条记录的查询方法
var filter = "?$filter=name eq '123'"; var req = new XMLHttpRequest() req.open("get", Xrm.Page.context.getClientUrl() + "/api/data/v8.0/accounts/" + filter, false); 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) { if (this.status == 200) { var data=JSON.parse(this.responseText).value; } else { var data=this.responseText; } } }; req.send();结果如下图,一个object即为一条数据记录,filter的写法和odata时一致。
这里着重提下lookup字段的filter写法和原来的不大一样,这里lookup字段你的属性名为new_testid,在filter中的写法是_new_testid_value后面直接eq,另外个要注意的是guid上往往会带左右大括号{},这个得去掉,简单的说就是严格遵守odata version4.0的语法就对了。
var filter="?$filter=_new_testid_value eq "+Xrm.Page.getAttribute("new_testid").getValue()[0].id.replace('{', '').replace('}', '')