javascript - 如果您不先执行 alert(),则使用 Xrm.WebApi.createRecord 创建事件会失败
问题描述
我有一个使用 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');
它,它会跟随错误回调并给出消息“创建错误 - 发生意外错误”
我不知道为什么会这样,或者看到用户单击“确定”引起的短暂延迟应该使它起作用的任何原因。
解决方案
这可能是因为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);
});
推荐阅读
- python - 如何在 Azure 中的 python 中使用 Eventhub 和 Storage SDK
- angular - 在更改表单控制角度反应形式之前询问确认警报
- java - JavaDoc中的`.`和`#`有什么区别?
- r - 将 update* 调用发送到 Shiny 模块
- ibm-midrange - 在提交周期中调用 SQLRPG 时更正提交选项
- rust - 特征对象的静态数组
- python-3.7 - 如何在 Python 3.7.4 和 openssl 1.1.1 中强制使用 TLSv1.2?
- python - 如何在没有 Internet 访问权限且没有 root 访问权限的 RHEL 4 企业版上安装 Python 3.6?
- html - 内部div中的滚动条,而父级只有最大高度
- git - 我需要一种 100% 确定的方法来使我的 GitHub 分叉和克隆与原始版本相同吗?