This is my first bit of code written for dynamics crm.
I started with this sample of how to create an account. Worked like a charm.
https://szuyublog.wordpress.com/2016/02/22/microsoft-dynamics-crm-2016-web-api-create-an-entity-using-http-post-request/
Then I copied the working code and am trying to create a record in a different entity but of course it is not working.
Can someone tell me how to put error handling in so I know what the issue is.
I have psa installed and trying to create a record in msdyn_contractlinescheduleofvalue
I get all the values I want to insert from the salesorder/salesorderdetailid and then try to create the record
it never goes past this line of code req.onreadystatechange = function ()
Then it returns a message box that says [Object Object]
How do I learn what I am doing wrong?
Here is my code:
function getSalesOrderIdthenCreatecontractlinescheduleofvalue() {
if(Xrm.Page.ui.getFormType() !==1 ) {
debugger;
//get all the values needed to create record
var contractId = Xrm.Page.getAttribute('salesorderid').getValue()[0].id;
var contractline = Xrm.Page.data.entity.getId();
var invoicedate = new Date();
var invoicestatus = '192350000';
var linename = "test";
var transactionclassification = '192350003';
var transactiontypecode = '192350004';
var entityType = "msdyn_contractlinescheduleofvalue";
var clientURL = Xrm.Page.context.getClientUrl();
var clsv = {};
clsv["msdyn_contract"] = contractId;
clsv["msdyn_contractline"] = contractline;
clsv["msdyn_name"] = linename;
clsv["msdyn_invoicedate"] = invoicedate;
clsv["msdyn_invoicestatus"] = invoicestatus;
clsv["msdyn_transactionclassification"] = transactionclassification;
clsv["msdyn_transactiontypecode"] = transactiontypecode;
CreateEntity(clientURL, entityType, clsv);
}
}
function CreateEntity(clientURL, entityType, entityData) {
var req = new XMLHttpRequest();
req.open("POST", encodeURI(clientURL + "/api/data/v8.2/" + entityType) , true);
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.onreadystatechange = function () { ///never goes into this function
if (this.readyState == 4 /* complete */) {
req.onreadystatechange = null; //avoids memory leaks
if (this.status == 204) {
var entityUri = this.getResponseHeader("OData-EntityId");
alert(entityUri);
console.log("Created " + entityType + " with URI " + entityUri);
}
else {
var error = JSON.parse(this.response).error;
alert(error);
console.log(error.message);
}
}
};
req.send(JSON.stringify(entityData));
}