我有一个使用Xrm.WebApi.createRecord创建事件记录的小方法
function createChangeRequest(emailData) {
var createRequestObj = null;
try {
//create CR object
createRequestObj = new Object();
createRequestObj.title = emailData.name;
createRequestObj.description = emailData.description;
var senderId = emailData.senderId.replace('{', '').replace('}', '');
var account = "";
if (emailData.senderEntity == 'contact') {
try {
//alert(senderId);
Xrm.WebApi.retrieveRecord("contact", senderId, "$select=fullname&$expand=parentcustomerid_account($select=accountid,name)")
.then(function (data) {
if (data.parentcustomerid_account.accountid != null) {
//alert(data.parentcustomerid_account.accountid);
account = data.parentcustomerid_account.accountid;
//set the lookup value
createRequestObj["customerid_account@odata.bind"] = "/accounts(" + account.toUpperCase() + ")";
}
},
function (error) {
Xrm.Utility.alertDialog(error.message);
});
} catch (e) {
Xrm.Utility.alertDialog(e.message);
}
//set the lookup value
createRequestObj["primarycontactid@odata.bind"] = "/contacts(" + senderId + ")";
}
else if (emailData.senderEntity == 'account') {
//set the lookup value
createRequestObj["customerid_account@odata.bind"] = "/accounts(" + senderId + ")";
}
alert('wibble');
Xrm.WebApi.createRecord("incident", createRequestObj).then(function (result) {
//get the guid of created record
var recordId = result.id;
//below code is used to open the created record
var windowOptions = {
openInNewWindow: false
};
//check if XRM.Utility is not null
if (Xrm.Utility != null) {
//open the entity record
Xrm.Utility.openEntityForm("incident", recordId, null, windowOptions);
}
},
function (error) {
Xrm.Utility.alertDialog('Create error - ' + error.message);
});
}
catch (e) {
Xrm.Utility.alertDialog('General Error - ' + e.message);
}
}
这可以按预期工作,但是如果我删除或注释掉alert('wibble');
,它会在错误回调后显示并显示消息“创建错误-发生意外错误”
我不知道这是为什么,或者不知道由用户单击“确定”引起的短暂延迟应该使它起作用的任何原因。
答案 0 :(得分:0)
这可能是因为Xrm.WebApi
方法的异步行为。调用返回的Promise对象必须在浏览器中方便地运行:)
按如下所示在Xrm.WebApi.createRecord
的成功回调中移动Xrm.WebApi.retrieveRecord
行:
Xrm.WebApi.retrieveRecord("contact", senderId, "$select=fullname&$expand=parentcustomerid_account($select=accountid,name)")
.then(function (data) {
if (data.parentcustomerid_account.accountid != null) {
//alert(data.parentcustomerid_account.accountid);
account = data.parentcustomerid_account.accountid;
//set the lookup value
createRequestObj["customerid_account@odata.bind"] = "/accounts(" + account.toUpperCase() + ")";
Xrm.WebApi.createRecord("incident", createRequestObj).then(function (result) {
//get the guid of created record
var recordId = result.id;
//below code is used to open the created record
var windowOptions = {
openInNewWindow: false
};
//check if XRM.Utility is not null
if (Xrm.Utility != null) {
//open the entity record
Xrm.Utility.openEntityForm("incident", recordId, null, windowOptions);
}
},
function (error) {
Xrm.Utility.alertDialog('Create error - ' + error.message);
});
}
},
function (error) {
Xrm.Utility.alertDialog(error.message);
});