I have a scenario where I need to insert Opportunity line (Opportunity Product) to Opportunity via C#. I have gone through the Web API Entity reference and tried to insert Opportunity product via C#.
var jsonBody = new Dictionary<string, object>()
{
{ "uomid@odata.bind", "/uoms(5b71260d-3248-e611-80d1-00155ddc4299)" },
{ "productid@odata.bind", "/products(9a9b2e45-d5d3-e811-80e5-00155d02682b)"},
{ "opportunityid@odata.bind", "/opportunities(eb166885-4fde-e911-a95a-00155d026b79)"},
{ "quantity", 1.0000000000 }
};
string httpUrl = "{my URL}/api/data/v8.2/opportunityproducts";
string httpMethod = "POST";
var url = new Uri(httpUrl);
var method = new HttpMethod(httpMethod);
var request = new HttpRequestMessage(method, url);
HttpClient client = new HttpClient(new HttpClientHandler() { Credentials = new NetworkCredential("***", "***", "**") });
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.Timeout = TimeSpan.FromMilliseconds(Timeout.Infinite);
string jsonItem = Newtonsoft.Json.JsonConvert.SerializeObject(jsonBody, Formatting.Indented);
request.Content = new StringContent(jsonItem.ToString(), Encoding.UTF8, "application/json");
var responseaa = client.SendAsync(request).Result;
string responseContent = responseaa.Content.ReadAsStringAsync().Result;
The Above code is not working and it's not inserting any new line Item.
I am getting BadRequest with message :
"message":"Unexpected exception from plug-in (Execute): XrmMasters.CalculateOpportunityProducts: System.NullReferenceException: Object reference not set to an instance of an object."
Any idea on how to insert Opportunity Line Item with Product reference via C#. Am I missing any fields here to associate the product with Opportunity Line?
Any help is appreciated.
*This post is locked for comments