提示:知识点
查询数据注意点
在crm
的开发过程中,我们经常会用到在js
中查询数据,dynamics
提供了webapi
供我们使用,但在使用的时候不是直接通过实体名去进行查询的。
-
实体名以
s
结尾(复数形式),如tb_crms
,查询的时写tb_crmses
-
实体名以
y
结尾:tb_crmy
---tb_crmies
-
以
es
结尾:tb_crmes
---tb_crmeses
(不确定) -
其它的都加
s
,比如tb_user
--tb_users
-
提示:
webapi
查询时实体名只能是小写
课时一:JS操作数据
JSAPI请求头
xmlhttp.setRequestHeader("Content-Type", "application/json; charset=utf-8");
xmlhttp.setRequestHeader("Accept", "application/json");
xmlhttp.setRequestHeader("OData-MaxVersion", "4.0");
xmlhttp.setRequestHeader("OData-Version", "4.0");
// 加上成功返回201,省略成功返回204
xmlhttp.setRequestHeader("Prefer", "return-representation");
创建操作
提示:
- 日期类型:值为
Date
类型 - 查找类型:字段后追加
@odata.bind
,值为/目标实体名(当前查找字段值id)
,目标实体名使用复数形式
创建单条记录
function createEntity(){
var entity = {}
entity['new_name'] = 'dtcc' //文本
entity['new_gender'] = 1000 //选项集
entity['new_time'] = new Date() //日期
entity['new_search@odata.bind'] = '/new_tbs(id)' //查找
entity['new_bool'] = true
var req = new XMLHttpRequest()
req.open('post','url/accounts',false)
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.onreadystatechange = function(){
if(this.readyState == 4){
if(this.status == 204){}
else{}
}
}
req.send(JSON.stringify(entity))
}
查询操作
提示:
select
:显示记录中哪些字段expand
:指定此记录中查找类型
的字段,找出此字段对应的记录orderby
:排序filter
:筛选- 多个关键字
&
符号连接
查询全部
function getNameByPrimaryKey(){
var req = new XMLHttpRequest()
req.open('get','http://url/accounts',false)
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.onreadystatechange = function(){
if(this.readyState == 4){
if(this.status == 204){
var result = JSON.parse(this.responseText)
}
else{}
}
}
}
根据主键查询
function getDataByPrimaryKey(){
var req = new XMLHttpRequest()
req.open('get','http://url/accounts(id)?$select=name,accountid&$expand=territoryid($select=description)',false)
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.onreadystatechange = function(){
if(this.readyState == 4){
if(this.status == 204){
var result = JSON.parse(this.responseText)
}
else{}
}
}
}
根据主键查询单个字段值
function getNameByPrimaryKey(){
var req = new XMLHttpRequest()
req.open('get','http://url/accounts(id)/name',false)
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.onreadystatechange = function(){
if(this.readyState == 4){
if(this.status == 204){
var result = JSON.parse(this.responseText)
}
else{}
}
}
}
根据主键查询外键字段值
territoryid
为查找类型,可以理解为一个实体记录
function getNameByPrimaryKey(){
var req = new XMLHttpRequest()
req.open('get','http://url/accounts(id)/territoryid($select=description)',false)
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.onreadystatechange = function(){
if(this.readyState == 4){
if(this.status == 204){
var result = JSON.parse(this.responseText)
}
else{}
}
}
}
条件查询
-
算术:
eq
,ne
,gt
,ge
,lt
,le
$filter=money lt 100
-
逻辑:
and
,or
,not
$filter=money lt 100 and money lt 100 $filter=not money lt 100
-
模糊查询:
contains
,endswith
,startswith
$filter=contains(name,'nc')
function getNameByKey(){
var filter = '?$filter=contains(name,'nc')'
var req = new XMLHttpRequest()
req.open('get','http://url/accounts/'+filter,false)
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.onreadystatechange = function(){
if(this.readyState == 4){
if(this.status == 204){
var result = JSON.parse(this.responseText)
}
else{}
}
}
}
排序
'http://url/users?$select=name,revenue&$orderby=name asc,revenue desc&$filter=name eq 3'
TOP
'http://url/users?top=3'
COUNT
'http://url/users?count=true'
更新操作
更新单条记录
function createEntity(){
var entity = {}
entity['new_name'] = 'dtcc' //文本
entity['new_gender'] = 1000 //选项集
entity['new_time'] = new Date() //日期
entity['new_search@odata.bind'] = '/new_tbs(id)' //查找
entity['new_bool'] = true
var req = new XMLHttpRequest()
req.open('PATCH','url/accounts(Id)',false)
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
// 记录不存在则创建
req.setRequestHeader("If-Match", "*");
req.onreadystatechange = function(){
if(this.readyState == 4){
if(this.status == 204){}
else{}
}
}
req.send(JSON.stringify(entity))
}
更新记录的单个属性值
var entity = '{value:33}'
req.open('PUT','url/accounts(Id)/name',false)
req.send(JSON.stringify(entity))
删除操作
删除单条记录
req.open('delete','url/accounts(Id)',false)
清空记录属性值
req.open('delete','url/accounts(Id)/name',false)
HTTP.js
提示:
- 参数必须JSON格式化:
JSON.stringify(data)