I'm having trouble adding products to a quote programmatically i read all the documentation and all the similar threads, but after hundreds of tries, i continue not finding the right way to accomplish my purpose. This is my code:
using System;
using System.Linq;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
namespace ultima
{
public class InsertProdPlugin : IPlugin
{
private ITracingService _tracingService;
public void Execute(IServiceProvider serviceProvider)
{
_tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
_tracingService.Trace("Tracing: EXECUTE");
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = factory.CreateOrganizationService(context.UserId);
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
Entity entity = (Entity)context.InputParameters["Target"];
try
{
if (entity.LogicalName == "quote" && service != null)
{
ColumnSet attributes = new ColumnSet(true);
_tracingService.Trace("Error1");
Entity CurrentQuoteEntity;
_tracingService.Trace("Error2");
var id = context.PrimaryEntityId;
_tracingService.Trace("Error3:" + context.PrimaryEntityId);
CurrentQuoteEntity = service.Retrieve("quote", id, new ColumnSet("quoteid"));
_tracingService.Trace("Error4");
Guid quoteid = CurrentQuoteEntity.Id;
_tracingService.Trace("Error5:" + CurrentQuoteEntity.Id);
Entity newQuoteproductsEntity = new Entity("quotedetail");
_tracingService.Trace("Error6" + newQuoteproductsEntity);
QueryByAttribute query = new QueryByAttribute("product");
query.AddAttributeValue("productid", "019f6101-8e02-e811-a2c1-cba121a17201");
var product = service.RetrieveMultiple(query).Entities.FirstOrDefault();
if (product != null)
{
_tracingService.Trace("Error7:" + product.Id);
newQuoteproductsEntity["productid"] = product.Id;
newQuoteproductsEntity["uomid"] = "Primary Unit";
_tracingService.Trace("Error10" + newQuoteproductsEntity["uomid"]);
newQuoteproductsEntity["quoteid"] = CurrentQuoteEntity.Id;
_tracingService.Trace("Error11:" + CurrentQuoteEntity.Id + "\n Error12:" + newQuoteproductsEntity.Id);
service.Create(newQuoteproductsEntity);
_tracingService.Trace("Error13");
}
else
{
_tracingService.Trace("Product is empty!");
}
}
else
{
_tracingService.Trace("Not working at all!");
}
}
catch (Exception ex)
{
_tracingService.Trace(ex.InnerException.ToString());
throw;
}
}
}
}
}
I registered the plugin Post-operation (i also tried pre-operation and pre-validation) Asynchronous (also tried Synchronous) The Message is Update with primary entity quote, Secondary entity none and filtering attributes effectivefrom (when i update the effectivefrom date, the plugin fires).
I receive a business Process Error and this is the log:
Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=8.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: Unexpected exception from plug-in (Execute): ultima.InsertProdPlugin: System.NullReferenceException: Object reference not set to an instance of an object.Detail:
<OrganizationServiceFault xmlns:i="www.w3.org/.../XMLSchema-instance" xmlns="schemas.microsoft.com/.../Contracts">
<ActivityId>bd994089-8947-44bb-9990-b1cf5d53a8b3</ActivityId>
<ErrorCode>-2147220956</ErrorCode>
<ErrorDetails xmlns:d2p1="schemas.datacontract.org/.../System.Collections.Generic" />
<Message>Unexpected exception from plug-in (Execute): ultima.InsertProdPlugin: System.NullReferenceException: Object reference not set to an instance of an object.</Message>
<Timestamp>2018-02-12T11:02:52.2009169Z</Timestamp>
<ExceptionRetriable>false</ExceptionRetriable>
<ExceptionSource i:nil="true" />
<InnerFault i:nil="true" />
<OriginalException i:nil="true" />
<TraceText>
[Ultima4: ultima.InsertProdPlugin]
[f8059231-e40f-e811-a2c2-e45f139a85c9: ultima.InsertProdPlugin: Update of quote]
Tracing: EXECUTE
Error1
Error2
Error3:13733bc2-d90f-e811-a2c2-e45f139a85c9
Error4
Error5:13733bc2-d90f-e811-a2c2-e45f139a85c9
Error6Microsoft.Xrm.Sdk.Entity
Error7:019f6101-8e02-e811-a2c1-cba121a17201
Error10Primary Unit
Error11:13733bc2-d90f-e811-a2c2-e45f139a85c9
Error12:00000000-0000-0000-0000-000000000000
</TraceText>
</OrganizationServiceFault>
Thanks in advance.