var url = window.location.href; // 当前页面地址
var arr = url.split("/");
var crm_url = arr[0] + "//" + arr[2]; // CRM 域名加端口
var req_url = crm_url + '/api/data/v8.2/{entityname}'; // {entityname} 实体 logical name,要复数形式
var req = new XMLHttpRequest();
var entity = {
'new_account_id@odata.bind':'/accounts(' + id + ')', // lookup 类型
'new_qty':10, // 数字
'type': 1 // 选项集
}; // 要创建的实体的数据
var data = JSON.stringify(entity); // 要发送到服务器的数据
req.open("POST", req_url , false);
req.setRequestHeader("Accept", req_url, "application/json");
req.setRequestHeader("Content-Type", "application/json; CHARSET=utf-8"); // 必要,CHARSET 可省略
req.setRequestHeader("OData-MaxVersion", "4.0"); // 可以省略此行
req.setRequestHeader("OData-Version", "4.0"); // 可以省略此行
req.onreadystatechange = function () {
if (this.readyState === 4) {
if (this.status === 204) {
//获取创建成功的实体的ID
var oDataEntityId = req.getResponseHeader("OData-EntityId");
var entityId = oDataEntityId.substring(oDataEntityId.indexOf("(") + 1, oDataEntityId.indexOf(")"));
return entityId;
}
else {
var error = JSON.parse(req.response).error;
alert(error.message);
}
}
};
req.send(JSON.stringify(data);
Microsoft Dynamics CRM 用 javascript 创建实体记录
最新推荐文章于 2021-12-31 12:23:17 发布