I have a scenario where I need to insert Opportunity line (Opportunity Product) to Opportunity via Javascript. I have gone through the Web API Entity reference and tried to insert Opportunity product via Javascript.
var entity = {};
entity["opportunityid@odata.bind"] = "/opportunities(34fgh654-sd45-ff44-dd44-4455ggfd4fcf)";
entity["productid@odata.bind"] = "/products(asda4556-c509-e811-a843-444fd445ghj4)";
var req = new XMLHttpRequest();
req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/opportunityproducts", true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.onreadystatechange = function() {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 204) {
var uri = this.getResponseHeader("OData-EntityId");
var regExp = /\(([^)]+)\)/;
var matches = regExp.exec(uri);
var newEntityId = matches[1];
} else {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send(JSON.stringify(entity));
The Above code is not working and it's not inserting any new line Item
I modify the above code by removing Product ID reference and it works. New Opportunity Line Item is inserting without any product reference.
var entity = {};
entity["opportunityid@odata.bind"] = "/opportunities(34fgh654-sd45-ff44-dd44-4455ggfd4fcf)";
var req = new XMLHttpRequest();
req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/opportunityproducts", true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.onreadystatechange = function() {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 204) {
var uri = this.getResponseHeader("OData-EntityId");
var regExp = /\(([^)]+)\)/;
var matches = regExp.exec(uri);
var newEntityId = matches[1];
} else {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send(JSON.stringify(entity));
Any idea on how to insert Opportunity Line Item with Product reference via Javascript. Am I missing any fields here to associate the product with Opportunity Line?
Any help is appreciated.
*This post is locked for comments
I have the same question (0)