如果您不先执行 alert(),则使用 Xrm.WebApi.createRecord 创建事件会失败
Creating Incident using Xrm.WebApi.createRecord fails if you don't do an alert() first
我有一个使用 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');
它会跟随错误回调并给出消息 'Create error - An unexpected error occurred'
我不知道这是为什么,也不知道用户单击“确定”造成的短暂延迟应该可以正常工作的任何原因。
这可能是因为 Xrm.WebApi
方法的异步行为。此调用返回的承诺对象必须在浏览器中 运行 方便:)
在 Xrm.WebApi.retrieveRecord
的成功回调中移动 Xrm.WebApi.createRecord
行,如下所示:
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);
});
我有一个使用 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');
它会跟随错误回调并给出消息 'Create error - An unexpected error occurred'
我不知道这是为什么,也不知道用户单击“确定”造成的短暂延迟应该可以正常工作的任何原因。
这可能是因为 Xrm.WebApi
方法的异步行为。此调用返回的承诺对象必须在浏览器中 运行 方便:)
在 Xrm.WebApi.retrieveRecord
的成功回调中移动 Xrm.WebApi.createRecord
行,如下所示:
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);
});