1.Onsave事件是窗体保存之前进行的操作,“PageData.getEventArgs().preventDefault();”该语句为阻止保存操作!但在这之前需要勾选上下文信息传递!PageData为传递的数据
2.在一个实体中修改另一个实体中的信息(两个实体是有关联的),获取lookup集合信息,将实体名称提取出来,a[0].entityType;-->为选中实体的名称。
获取上下文信息,最新写法get
function Add_contacts(PageData) {
var a = PageData.getFormContext();
var k = {};
k.num1 = a.getAttribute("new_num1").getValue();
k.num2 = a.getAttribute("new_num2").getValue();
k.sum = a.getAttribute("new_sum").getValue();
console.log(k);
}
点击按钮跳转至其他页面,并带有其他参数
function update_record() {
//跳转到跟进记录的页面,带客户id和联系人id到记录表
//跳转之前需判断两个值是否为空
if (Xrm.Page.data.entity.getId() == "" || Xrm.Page.getAttribute("new_client_contacts").getValue() == null) {
alert("请保存当前信息和选择一个联系人");
}
else {
let info = {};
info["new_record_clientname"] = Xrm.Page.data.entity.getId().replace(‘{‘, ‘‘).replace(‘}‘, ‘‘);//当前页面的id
info["new_record_clientnamename"] = Xrm.Page.getAttribute("new_name").getValue();//当前页面的名称,字段名称+name=名称
var a = Xrm.Page.getAttribute("new_client_contacts").getValue();//当前页面查找类型的集合
info["new_record_contacts"] = a[0].id.replace(‘{‘, ‘‘).replace(‘}‘, ‘‘).toUpperCase();//当前页面集合的取值
info["new_record_contactsname"]=a[0].name;//查找类型的
Xrm.Utility.openEntityForm("new_recording", null, info);跳转指定实体和带过去的参数。
}
}
修改的代码
function updatetime(PageData) {
debugger;
var time = Xrm.Page.getAttribute("new_record_updatetime").getValue();//获取当前填写日期信息
var nexttime = Xrm.Page.getAttribute("new_record_next_updatetime").getValue();
//获取用户表中时间信息
var a = Xrm.Page.getAttribute("new_record_clientname").getValue();
var id = a[0].id.replace(‘{‘, ‘‘).replace(‘}‘, ‘‘).toUpperCase();
var name = a[0].entityType;
var a = name[name.length - 2];//t
var b = name[name.length - 1];//y
var 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 localUrl = Xrm.Page.context.getClientUrl() + "/api/data/v9.0/" + name;
var t = "";
$.ajax({
url: localUrl + "(" + id + ")",
type: "get",
async: false,
dataType:"json",
success: function (data) {
t = data.new_client_updatetime;
}, error: function (data) {
alert(‘Error!‘);
}
})
var time12 = new Date(t);//客户时间
alert(time - time12);
if (time - time12<0) {
alert("有问题");
PageData.getEventArgs().preventDefault();
}
else {
if (nexttime != null) {//判断下一次更新时间是否为空
if (nexttime - time <= 0) {//下次时间小于所填时间
//添加中断
alert("更新时间应在下次更新时间之前!");
PageData.getEventArgs().preventDefault();
}
else {
//为空默认添加天数
var date = new Date();
var v = new Date(date.toLocaleDateString());
v.setDate(date.getDate() + 3);
var month = v.getMonth() + 1;
var day = v.getDate();
var mm = month;
var dd = day;
//单位数前面加0
if (mm <= 9) {
month = "0" + month;
}
if (dd <= 9) {
day = "0" + day;
}
var time1 = date.getFullYear() + "-" + month + "-" + day;
ClientUpdateTime(time);
ContactsUpdateTime(time);
Xrm.Page.getAttribute("new_record_next_updatetime").setValue(new Date(time1));
}
}
else {
//为空默认添加天数
var date = new Date();
var v = new Date(date.toLocaleDateString());
v.setDate(date.getDate() + 3);
var month = v.getMonth() + 1;
var day = v.getDate();
var mm = month;
var dd = day;
//单位数前面加0
if (mm <= 9) {
month = "0" + month;
}
if (dd <= 9) {
day = "0" + day;
}
var time1 = date.getFullYear() + "-" + month + "-" + day;
ClientUpdateTime(time);
ContactsUpdateTime(time);
Xrm.Page.getAttribute("new_record_next_updatetime").setValue(new Date(time1));
}
}
}
//修改客户实体信息
function ClientUpdateTime(time) {
//获取名称
debugger;
var a = Xrm.Page.getAttribute("new_record_clientname").getValue();
var id = a[0].id.replace(‘{‘, ‘‘).replace(‘}‘, ‘‘).toUpperCase();
var name = a[0].entityType;
var a = name[name.length - 2];//t
var b = name[name.length - 1];//y
var 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 localUrl = Xrm.Page.context.getClientUrl() + "/api/data/v9.0/" + name;
//获取id
var jsondata = {};
jsondata.new_client_updatetime = new Date(time);
$.ajax({
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(‘失败!‘);
}
})
}
//更新联系人时间信息
function ContactsUpdateTime(time) {
//获取名称
debugger;
var a = Xrm.Page.getAttribute("new_record_contacts").getValue();
var id = a[0].id.replace(‘{‘, ‘‘).replace(‘}‘, ‘‘).toUpperCase();
var name = a[0].entityType;
var a = name[name.length - 2];//t
var b = name[name.length - 1];//y
var 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 localUrl = Xrm.Page.context.getClientUrl() + "/api/data/v9.0/" + name;
//获取id
var jsondata = {};
jsondata.new_controls_updattime = new Date(time);
$.ajax({
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(‘失败!‘);
}
})
}
注意ajax的同步与异步,查询取值是get,类型必须加上dataType:"json",返回结果为json