关注本人微信和易信公众号: 微软动态CRM专家罗勇 ,回复205或者20160314可方便获取本文,同时可以在第一间得到我发布的最新的博文信息,follow me!
先说更新记录的多个字段的值的方法,示例代码如下:
function ConvertStringToDatetime(dateString) { var reggie = /(d{4})-(d{2})-(d{2})T(d{2}):(d{2}):(d{2})Z/; var dateArray = reggie.exec(dateString); var dateObject = new Date( (+dateArray[1]), (+dateArray[2]) - 1, (+dateArray[3]), (+dateArray[4]), (+dateArray[5]), (+dateArray[6]) ); return dateObject; } function NameOnchangeHandler() { var clientURL = Xrm.Page.context.getClientUrl(); var req = new XMLHttpRequest(); req.open("PATCH", encodeURI(clientURL + "/api/data/v8.0/ly_tests(77A783FE-4EB3-E511-80D9-000D3A404231)"), true); 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 /* complete */) { req.onreadystatechange = null; if (this.status == 204) { Xrm.Utility.alertDialog("记录修改成功!") } else { var error = JSON.parse(this.response).error; Xrm.Utility.alertDialog("修改罗勇测试实体记录出错." + error.message); } } }; var test = {}; test.ly_name = "单行文本";//单行文本 test.ly_alternatekey = "13926492292";//单行文本 test.ly_bool = true;//两个选项 test.ly_datetime = ConvertStringToDatetime('2016-01-07T11:22:30Z');//日期和时间,需要将UTC时间转换为本地时间 test.ly_decimal = 10.01;//十进制数 test.ly_float = 10.02//浮点数 test["ly_Lookup@odata.bind"] = "/accounts(CE23165A-3AA3-E511-80C7-000D3A807EC7)"//查找字段,注意去/api/data/v8.0/$metadata页面查看关系的Partner属性的值 test.ly_integer = 10;//整数 test.ly_money = 10.03;//货币 test.ly_multilinetext = "多行文本";//多行文本 test.ly_optionset = 364750000;//选项集 test.ly_singlelinetext = "单行文本";//单行文本 req.send(JSON.stringify(test)); }
再说说更新一个字段的方法,这个有简便形式,注意http请求的动作是PUT了。
function NameOnchangeHandler() { var clientURL = Xrm.Page.context.getClientUrl(); var req = new XMLHttpRequest() req.open("PUT", encodeURI(clientURL + "/api/data/v8.0/ly_tests(77A783FE-4EB3-E511-80D9-000D3A404231)/ly_name"), true); 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 /* complete */) { req.onreadystatechange = null; if (this.status == 204) { Xrm.Utility.alertDialog("记录修改成功!") } else { var error = JSON.parse(this.response).error; Xrm.Utility.alertDialog("修改罗勇测试实体记录出错." + error.message); } } }; var updateValue = { "value": "来自罗勇的问候!" }; req.send(JSON.stringify(updateValue)); }
下面是将记录的字段值赋值为NULL (空值)的方法:
var clientURL = Xrm.Page.context.getClientUrl(); var req = new XMLHttpRequest() req.open("DELETE", encodeURI(clientURL + "/api/data/v8.0/ly_tests(77A783FE-4EB3-E511-80D9-000D3A404231)/ly_decimal"), true); 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 /* complete */) { req.onreadystatechange = null; if (this.status == 204) { Xrm.Utility.alertDialog("记录修改成功!") } else { var error = JSON.parse(this.response).error; Xrm.Utility.alertDialog("修改罗勇测试实体记录出错." + error.message); } } }; req.send();
下面是删除记录的示例代码:
var clientURL = Xrm.Page.context.getClientUrl(); var req = new XMLHttpRequest() req.open("DELETE", encodeURI(clientURL + "/api/data/v8.0/ly_tests(77A783FE-4EB3-E511-80D9-000D3A404231)"), true); 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 /* complete */) { req.onreadystatechange = null; if (this.status == 204) { Xrm.Utility.alertDialog("记录删除成功!"); } else if (this.status == 404) { Xrm.Utility.alertDialog("要删除的记录不存在!") } else { var error = JSON.parse(this.response).error; Xrm.Utility.alertDialog("修改罗勇测试实体记录出错." + error.message); } } }; req.send();
注意,对于可能包括小数的字段,比如十进制数,货币等,如果你赋予整数的话,比如赋予100,会导致出错。错误信息通常是:Incorrect attribute value type System.Int32 . 或者 The property provided was of type System.Int32, when the expected was of type System.Decimal 。我这里提供的解决办法很很简单,就是设置的值加上 0.00001 ,这个对你要设置的值大小没有影响,我查过数据库,但是又能让系统认为你传递过去的数据是正确的类型。示例是:{ "value": 200 + 0.00001 } 。
从Dynamics 365 V9版本开始提供了新的API来方便客户端编程,具体请参考官方文档:updateRecord (Client API reference) 和 deleteRecord (Client API reference) 。
更多信息请参考官方文档:Update and delete entities using the Web API 。