zoukankan      html  css  js  c++  java
  • Dynamics 365-一:JS操作数据

    提示:知识点

    查询数据注意点

    ​ 在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{}
            }
        }
    }
    

    条件查询

    • 算术:eqnegtgeltle

      $filter=money lt 100
      
    • 逻辑:andornot

      $filter=money lt 100 and money lt 100
      $filter=not money lt 100
      
    • 模糊查询:containsendswithstartswith

      $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)
    
    
    到达胜利之前无法回头!
  • 相关阅读:
    OpenJudge计算概论-寻找山顶
    OpenJudge计算概论-配对碱基链
    OpenJudge计算概论-分配病房
    OpenJudge计算概论-计算鞍点
    OpenJudge计算概论-错误探测
    OpenJudge计算概论-文字排版
    OpenJudge计算概论-二维数组右上左下遍历
    OpenJudge-计算点的距离并排序
    OpenJudge计算概论-找最大数序列
    Openjudge计算概论-奇数单增序列
  • 原文地址:https://www.cnblogs.com/weiyongguang/p/14060776.html
Copyright © 2011-2022 走看看