zoukankan      html  css  js  c++  java
  • 数据的添加、修改、删除(原生)

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

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

      以y结尾:tb_crmy---tb_crmies

      以es结尾:tb_crmes---tb_crmeses

      其他的通通加s

      PSwebapi查询时实体名只能是小写,同一实体中操作

     

     

    添加

     var localUrl = Xrm.Page.context.getClientUrl() + "/api/data/v9.0/";

                function getXmlHttpRequestHH() { //获取响应请求头

                    var xmlHttp;

                    if (window.ActiveXObject) {

                        var aVersions = ["Msxml2.XMLHttp", "Msxml2.XMLHttp.5.0", "Msxml2.XMLHttp.4.0", "Msxml2.XMLHttp.3.0", "Microsoft.XMLHttp"];

                        for (var i = 0; i < aVersions.length; i++) {

                            try {

                                xmlHttp = new ActiveXObject(aVersions[i]);

                                break;

                            } catch (e) { } 1

                        }

                    } else if (window.XMLHttpRequest) {

                        xmlHttp = new XMLHttpRequest();

                    }

                    return xmlHttp;

                }

                function CreateEntity(entityname, jsondata) { //使用原生ajax添加信息

                    debugger;

                    var requestURL = localUrl + entityname;

                    var xmlhttp = getXmlHttpRequestHH();

                    if (xmlhttp == null) {

                        var obj = new Object();

                        obj.error = new Object();

                        obj.error.message = new Object();

                        obj.error.message.value = "您的浏览器不支持XmlHttpRequest,请更新或使用IE浏览器!";

                        return obj;

                    }

                    xmlhttp.open("POST", requestURL, false);

                    xmlhttp.setRequestHeader("Content-Type", "application/json; charset=utf-8");

                    xmlhttp.setRequestHeader("Accept", "application/json");

                    //Accept请求头用来告知(服务器)客户端可以处理的内容类型,这种内容类型用MIME类型来表示

                    xmlhttp.setRequestHeader("OData-MaxVersion", "4.0");

                    xmlhttp.setRequestHeader("OData-Version", "4.0");

                    xmlhttp.send(jsondata);

                    if (xmlhttp.readyState == 4) {

                        xmlhttp.onreadystatechange = null;

                        if (xmlhttp.status == 204) {

                            //return jsonObject[0];

                            var str = xmlhttp.getResponseHeader("OData-EntityId");

                            return str.substr(str.indexOf('(') + 1, 36);

                        }

                        else {

                            return JSON.parse(xmlhttp.response);

                        }

                    }

                }

                function GetData() //获取传入的参数,调用添加函数进行参数的添加

                {

                    //获取一个当前的实体名称,该实体名称必须为复数形式

                    let name = window.parent.Xrm.Page.data.entity.getEntityName();

                    let a = name[name.length - 2];//t

                    let b = name[name.length - 1];//y

                    let c = a + b;//ty

                    if (b == "y") {

                        b = "ies";

                    }

                    else if (b == "s" || c == "es") {

                        b = "ses";

                    }

                    else {

                        b = b + "s";

                    }

                    name = name.substring(0, name.length - 1);

                    name = name + b;

                    var jsondata = {};

                    jsondata.new_name = Xrm.Page.getAttribute("new_name").getValue();

                    jsondata.new_client_name = Xrm.Page.getAttribute("new_client_name").getValue();

                    jsondata.new_client_id = Xrm.Page.getAttribute("new_client_id").getValue();

                    jsondata.new_client_sex = Xrm.Page.getAttribute("new_client_sex").getValue();

                    jsondata.new_client_addtime = Xrm.Page.getAttribute("new_client_addtime").getValue();

                    jsondata.new_client_float = Xrm.Page.getAttribute("new_client_float").getValue();

                    jsondata.new_client_a = Xrm.Page.getAttribute("new_client_a").getValue();

                    jsondata.new_client_money = Xrm.Page.getAttribute("new_client_money").getValue();

                    jsondata.new_client_cardnumber = Xrm.Page.getAttribute("new_client_cardnumber").getValue();

                    jsondata.new_client_summary = Xrm.Page.getAttribute("new_client_summary").getValue();

                    CreateEntity(name, JSON.stringify(jsondata)); //调用方法

                }

     

    修改

     

               var localUrl = Xrm.Page.context.getClientUrl()+"/api/data/v9.0/"

                function getXmlHttpRequestHH() {

                    var xmlHttp;

                    if (window.ActiveXObject) {

                        var aVersions = ["Msxml2.XMLHttp", "Msxml2.XMLHttp.5.0", "Msxml2.XMLHttp.4.0", "Msxml2.XMLHttp.3.0", "Microsoft.XMLHttp"];

                        for (var i = 0; i < aVersions.length; i++) {

                            try {

                                xmlHttp = new ActiveXObject(aVersions[i]);

                                break;

                            } catch (e) { } 1

                        }

                    } else if (window.XMLHttpRequest) {

                        xmlHttp = new XMLHttpRequest();

                    }

                    return xmlHttp;

                }

                //更新记录的多个字段的值

                function UpdateMul(entityname, id, jsondata) {

                    debugger;

                    var requestURL = localUrl + entityname + "(" + id + ")";

                    var xmlhttp = getXmlHttpRequestHH();

                    if (xmlhttp == null) {

                        var obj = new Object();

                        obj.error = new Object();

                        obj.error.message = new Object();

                        obj.error.message.value = "您的浏览器不支持XmlHttpRequest,请更新或使用IE浏览器!";

                        return obj;

                    }

                    xmlhttp.open("PATCH", requestURL, false);

                    xmlhttp.setRequestHeader("Accept", "application/json");

                    xmlhttp.setRequestHeader("Content-Type", "application/json; charset=utf-8");

                    xmlhttp.setRequestHeader("OData-MaxVersion", "4.0");

                    xmlhttp.setRequestHeader("OData-Version", "4.0");

                    xmlhttp.send(jsondata);

     

                    if (xmlhttp.readyState == 4 /* complete */) {

                        if (xmlhttp.status == 204) {

                            //Xrm.Utility.alertDialog("记录修改成功!")

                        } else {

                            return JSON.parse(xmlhttp.response);

                        }

                    }

                }

                //获取修改信息

                function UpdateInfo(){

                    let name = window.parent.Xrm.Page.data.entity.getEntityName();

                    let a = name[name.length - 2];//t

                    let b = name[name.length - 1];//y

                    let c = a + b;//ty

                    if (b == "y") {

                        b = "ies";

                    }

                    else if (b == "s" || c == "es") {

                        b = "ses";

                    }

                    else {

                        b = b + "s";

                    }

                    name = name.substring(0, name.length - 1);

                    name = name + b;

                    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 d = Xrm.Page.getAttribute("new_client_hobby").getValue();

                    var e = "";

                    if (d != null) {

                        for (var i = 0; i < d.length; i++) {

                            if (i == 0) {

                                e = d[i];

                            }

                            else {

                                e = e + "," + d[i];

                            }

                        }

                        jsondata.new_user_hobby = e;

                    } else {

                        jsondata.new_user_hobby = null;

                    }

                    //查找保存为odata.bind形式【】里面为被链接实体,@odatabind必写,/后面为链接实体(实体id

                    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('}', '') + ")";

                    }

                    //获取当前实体id

                    var a = Xrm.Page.getAttribute("new_select_entity").getValue();

                    var id = a[0].id.replace('{', '').replace('}', '').toUpperCase();

                    //获取当前选中的名称

                    UpdateMul(name, id, JSON.stringify(jsondata));

                    }

    删除

     

     var localUrl = Xrm.Page.context.getClientUrl() + "/api/data/v9.0/" + name;

                function getXmlHttpRequestHH() {

                    var xmlHttp;

                    if (window.ActiveXObject) {

                        var aVersions = ["Msxml2.XMLHttp", "Msxml2.XMLHttp.5.0", "Msxml2.XMLHttp.4.0", "Msxml2.XMLHttp.3.0", "Microsoft.XMLHttp"];

                        for (var i = 0; i < aVersions.length; i++) {

                            try {

                                xmlHttp = new ActiveXObject(aVersions[i]);

                                break;

                            } catch (e) { }

                        }

                    } else if (window.XMLHttpRequest) {

                        xmlHttp = new XMLHttpRequest();

                    }

                    return xmlHttp;

                }

                function DeleteEntity(entityname, objectid) {

                    debugger; //js断点

                    var requestURL = localUrl + "entityname" + "(" + objectid + ")";

                    var xmlhttp = getXmlHttpRequestHH();

                    if (xmlhttp == null) {

                        var obj = new Object();

                        obj.error = new Object();

                        obj.error.message = new Object();

                        obj.error.message.value = "您的浏览器不支持XmlHttpRequest,请更新或使用IE浏览器!";

                        return obj;

                    }

                    xmlhttp.open("DELETE", requestURL, false);

                    xmlhttp.setRequestHeader("Accept", "application/json");

                    xmlhttp.setRequestHeader("Content-Type", "application/json; charset=utf-8");

                    xmlhttp.setRequestHeader("OData-MaxVersion", "4.0");

                    xmlhttp.setRequestHeader("OData-Version", "4.0");

                    xmlhttp.send();

                    if (xmlhttp.readyState == 4 /* complete */) {

                        xmlhttp.onreadystatechange = null;

                        if (xmlhttp.status == 204) {

                            //Xrm.Utility.alertDialog("记录删除成功!");

                        }

                        else if (xmlhttp.status == 404) {

                            return "要删除的记录不存在!";

                        }

                        else {

                            return JSON.parse(xmlhttp.response);

                        }

                    }

                }

                function GetDel() {

                    // Xrm.Page.data.entity.getId();为获取当前数据的id

                    var objectid = Xrm.Page.data.entity.getId().replace('{', '').replace('}', '');

                    let name = window.parent.Xrm.Page.data.entity.getEntityName();

                    let a = name[name.length - 2];//t

                    let b = name[name.length - 1];//y

                    let c = a + b;//ty

                    if (b == "y") {

                        b = "ies";

                    }

                    else if (b == "s" || c == "es") {

                        b = "ses";

                    }

                    else {

                        b = b + "s";

                    }

                    name = name.substring(0, name.length - 1);

                    name = name + b;

     

                    DeleteEntity(name, objectid);

                }

  • 相关阅读:
    【福利】乳摇动画初探
    碎裂效果尝试(clip-path篇)
    碎裂效果尝试(canvas篇)
    自己的HTML5 播放器
    纯CSS实现选项卡
    企鹅阿里实习生面试
    关于使用indexedDB的本地存储(3)
    关于使用indexedDB的本地存储(2)
    关于使用indexedDB的本地存储(1)
    关于ajax分段上传文件实例~
  • 原文地址:https://www.cnblogs.com/LanHai12/p/15257970.html
Copyright © 2011-2022 走看看