数据的增、删、改(jQuery.Ajax)

在crm的开发过程中,我们经常会用到在js中查询数据, dynamics提供了webapi供我们使用,但在使用的时候不是直接通过实体名去进行查询的。

ExecutionObj.getEventArgs().preventDefault();//阻止保存操作

 

实体名以s结尾,如 tb_crms,查询的时候就是tb_crmses,后面加es

  以y结尾:tb_crmy---tb_crmies

  以es结尾:tb_crmes---tb_crmeses

  其他的通通加s

  PS:webapi查询时实体名只能是小写,这个是同一实体中修改数据

function GetEntityName() {
                var name = Xrm.Page.data.entity.getEntityName();//获取名称
                //拆分名称最后两个单词
                var last_two = name[name.length - 2];//t
                var last_one = name[name.length - 1];//y
                var last = last_two + last_one;//ty
                if (last_one == "y") {
                    last_one = "ies";
                }
                else if (last_one == "s" || last == "es") {
                    last_one = "ses";
                }
                else {
                    last_one = last_one + "s";
                }
                //截取除最后一位字母的部分
                name = name.substring(0, name.length - 1);
                //拼接复数单词
                name = name + last_one;
                return name;
            }

 

添加(post)添加:无返回

function GetData() { 
  var name=GetEntityName();
  var localUrl = Xrm.Page.context.getClientUrl()+"/api/data/v9.0/"+name;
                    var jsondata = {};
                    jsondata.new_name = "A";
                    jsondata.new_client_name = "A";
                   //整型
                    jsondata.new_client_id = 12;
                    //两个选项
                    jsondata.new_client_sex = true;
                    //时间
                    var date=new Date();
                    jsondata.new_client_addtime =new Date(date.toLocaleDateString());
                    //浮点
                    jsondata.new_client_float = "32.5";
                    //选项集
                    jsondata.new_client_a = "100000001";
                    //货币
                    jsondata.new_client_money = "56";
                     //十进制
                    jsondata.new_client_cardnumber = "56123156";
                   //多行文本
                    jsondata.new_client_summary = "asdasdasdasasds";
                    //多选项集
                    jsondata.new_client_hobby="100000001,100000002,100000003"
                    $.ajax({
                        url:  localUrl,
                        type: "post",
                        dataType: "json",
                        contentType: "application/json; charset=utf-8",
                        data: JSON.stringify(jsondata),
                        success: function (data)
                        {
                            alert(data);
                        }, error: function (data) {
                            alert('Error!');
                        }
                    })
            }

删除(DELETE)返回:无返回

function GetDel() {
  var name=GetEntityName();
  var localUrl = Xrm.Page.context.getClientUrl()+"/api/data/v9.0/"+name;
                    var id =Xrm.Page.data.entity.getId().replace('{','').replace('}','');
                    $.ajax({
                        url: localUrl+"("+id+")",
                        type: "DELETE",
                        success: function (data)
                        {
                            alert('DelSuccess!');
                        },
                        error: function (data)
                        {
                            alert('Error!');
                        }
                    })
                }

修改(PATCH)返回:无返回

function UpdateInfo() { 
  var name=GetEntityName();
  var localUrl = Xrm.Page.context.getClientUrl()+"/api/data/v9.0/"+name;
                    var jsondata = {};
                    jsondata.new_user_name = Xrm.Page.getAttribute("new_client_name").getValue();
                    jsondata.new_user_id = Xrm.Page.getAttribute("new_client_id").getValue();
                    jsondata.new_user_sex = Xrm.Page.getAttribute("new_client_sex").getValue();
                    jsondata.new_user_addtime = Xrm.Page.getAttribute("new_client_addtime").getValue();
                    jsondata.new_user_float = Xrm.Page.getAttribute("new_client_float").getValue();
                    jsondata.new_user_a = Xrm.Page.getAttribute("new_client_a").getValue();
                    jsondata.new_user_money = Xrm.Page.getAttribute("new_client_money").getValue();
                    jsondata.new_user_cardnumber = Xrm.Page.getAttribute("new_client_cardnumber").getValue();
                if (Xrm.Page.getAttribute("new_client_summary").getValue() != null) {
                    jsondata.new_user_summary = Xrm.Page.getAttribute("new_client_summary").getValue();
                } else {
                    jsondata.new_user_summary = null;
                }
                    //绑定查找信息
                    //多选项集
                    var b = Xrm.Page.getAttribute("new_client_hobby").getValue();
                    var c = "";
                    if (b != null) {
                        for (var i = 0; i < b.length; i++) {
                            if (i == 0) {
                                c = b[i];
                            }
                            else {
                                c = c + "," + b[i];
                            }
                        }
                        jsondata.new_user_hobby = c;
                    } else {
                        jsondata.new_user_hobby = null;
                    }
                    //查找类型
                    if (Xrm.Page.getAttribute("new_client_select").getValue() != null) {
                        jsondata["new_user_select@odata.bind"] = "/new_test_entities(" + Xrm.Page.getAttribute("new_client_select").getValue()[0].id.replace('{', '').replace('}', '') + ")";
                    } else {
                        jsondata["new_user_select@odata.bind"] = null;
                    }
                    //获取选中实体id
                    var a = Xrm.Page.getAttribute("new_select_entity").getValue();
                    var id = a[0].id.replace('{', '').replace('}', '').toUpperCase();
                $.ajax({
                    url:url: localUrl+"("+id+")",
                    type: "PATCH",
                    data: JSON.stringify(jsondata),
                    dataType: "json",
                    contentType: "application/json; charset=utf-8",
                    success: function (data) {
                        alert('成功!');
                    },
                    error: function (data) {
                        alert('失败!');
                    }
                })
                }

查(get)返回:查询的实体数据

function GetData(PageData) {
                debugger;
                 var id =Xrm.Page.data.entity.getId().replace('{','').replace('}','');
                $.ajax({
                    url: localUrl + "(" + id + ")",
                    type: "get", 
                    dataType: "json",
                    success: function (data) {
                       console.log(data)
                    }, error: function (data) {
                        alert('Error!');
                    }
                })
}

补充:

jsondata["字段名称@odata.bind"] = "/实体名称复数形式(" + 实体id + ")";--》查找类型赋值

时间类型赋值

var add_time = new Date();

add_goodsinfo.new_buytime = new Date(add_time.toLocaleDateString());-->转两次

写了一个方:url:为参数地址,EntityData:为传入数据,Manipulation:操作类型需要大写,Async:同步还是异步

//获取当前的实体名称,返回一个复数的名称

function GetEntityName() {
                var name = Xrm.Page.data.entity.getEntityName();//获取名称
                //拆分名称最后两个单词
                var last_two = name[name.length - 2];//t
                var last_one = name[name.length - 1];//y
                var last = last_two + last_one;//ty
                if (last_one == "y") {
                    last_one = "ies";
                }
                else if (last_one == "s" || last == "es") {
                    last_one = "ses";
                }
                else {
                    last_one = last_one + "s";
                }
                //截取除最后一位字母的部分
                name = name.substring(0, name.length - 1);
                //拼接复数单词
                name = name + last_one;
                return name;
            }

//获取当前时间字符串

function GetTimeNow() {
                var date = new Date();
                return date.toLocaleDateString();
            }

//添加、修改、删除、查询、的集合方法

          //获取当前实体名称的复数形式
          function GetEntityName() {
                var name = Xrm.Page.data.entity.getEntityName();//获取名称
                //拆分名称最后两个单词
                var last_two = name[name.length - 2];//t
                var last_one = name[name.length - 1];//y
                var last = last_two + last_one;//ty
                if (last_one == "y") {
                    last_one = "ies";
                }
                else if (last_one == "s" || last == "es") {
                    last_one = "ses";
                }
                else {
                    last_one = last_one + "s";
                }
                //截取除最后一位字母的部分
                name = name.substring(0, name.length - 1);
                //拼接复数单词
                name = name + last_one;
                return name;
            }
            //增、删、改、查集合方法
            function DataManipulation(Url, EntityData, RequestMode, Async) {
                //获取当前url
                return new Promise(
                    function (t_callback,f_callback) {
                        $.ajax({
                            url: Url,
                            timeout: 10000,//超时时间
                            type: RequestMode,
                            async: Async,
                            headers: {
                                "Content-Type": "application/json; charset=utf-8",
                                "Accept": "application/json",
                                "OData-MaxVersion": "4.0",
                                "OData-Version": "4.0"
                            },
                            data: EntityData === null ? "" : JSON.stringify(EntityData),
                            dataType: "json",
                            contentType: "application/json; charset=utf-8",
                            success:(data)=> {
                                t_callback(data);
                            }, error:(data)=> {
                                f_callback(data)
                            }
                        })
                    }
                )
            } 
            //调用方法
            var RequestMode = "get";
            var EntityId=Xrm.Page.data.entity.getId().replace('{','').replace('}','');
            var Url=Xrm.Page.context.getClientUrl()+"/api/data/v9.0/"+GetEntityName()+"("+EntityId+")";
            var ReturnDate = "";//全体名称
            DataManipulation(Url, null, RequestMode, true).then((data)=> {
                //返回查询数据
                if (RequestMode == "get") {
                    ReturnDate = data;
                }
                else {
                    ReturnDate = true;
                }
            }).catch((data)=> {
                    ReturnDate = false;
                }
            );

 

原文地址:https://www.cnblogs.com/LanHai12/p/15257968.html