I have a small method that creates an Incident record using 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);
}
}
This works as expected, however if I remove or comment out the alert('wibble');
it follows the error callback and gives the message 'Create error - An unexpected error occurred'
I don't not know why this is, or see any reason the short delay caused by the user clicking OK should make it work.
That could be because of Asynchronous behavior of Xrm.WebApi
methods. The promise object that returned by this call has to run in browser on its own convenience :)
Move the Xrm.WebApi.createRecord
line inside success callback of Xrm.WebApi.retrieveRecord
like below:
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);
});