Hi All,
I am facing an issue with my plugin code. I am trying to update discount on opportunity product when they are created. This discount is coming from the pricelist field.
After updating the discount, I am also updating the dealer revenue field on opportunity which will be calculated using est. revenue. But I am getting an exception saying, "Opportunity product with ID " " does not exist. Below is my code. I get the exception in the highlighted line.
public void Execute(IServiceProvider serviceProvider)
{
try
{
TracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
ServiceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
Service = ServiceFactory.CreateOrganizationService(context.UserId);
if (context.InputParameters.Contains("Target") &&
context.InputParameters["Target"] is Entity && ((context.Depth <=4)))
{
Entity entity = (Entity)context.InputParameters["Target"];
Entity newEntity = new Entity("opportunityproduct");
Entity e = (Entity)context.PostEntityImages["POST_OPROD"];
Money PriceperUnit = null;
decimal quantity = 0;
decimal discount = 0;
if (entity.LogicalName != "opportunityproduct")
{
return;
}
if (context.MessageName == "Create" || context.MessageName=="Update")
{
string parentOpportunity = ((EntityReference)e.Attributes["opportunityid"]).Name;
Guid parentOppId = ((EntityReference)e.Attributes["opportunityid"]).Id;
EntityReference Oppty = ((EntityReference)e.Attributes["opportunityid"]);
var opp = new Entity("opportunity");
opp.Id = parentOppId;
quantity = (decimal)e.GetAttributeValue<decimal>("quantity");
//To retrieve the value of customer
Entity customer = myClass.GetEntity(Oppty, Service, new string[] { "customerid" });
EntityReference customerId = (EntityReference)customer.Attributes["customerid"];
//To retrive Client type of customer
String[] attributes = { "new_dealertype"};
Entity clienTypeEntity = myClass.GetEntity(customerId, Service, attributes);
OptionSetValue clientType = (OptionSetValue)(clienTypeEntity["new_dealertype"]);
//check if client type is dealer or ship to.//If yes then only we will retrieve the pricelist discount
if (!e.Attributes.Contains("new_percentagediscount"))
{
if ((clientType.Value == 100000000) || (clientType.Value == 100000001))
{
QueryExpression myQuery = new QueryExpression("opportunity");
ColumnSet columns1 = new ColumnSet("pricelevelid", "name", "customerid");
myQuery.ColumnSet = columns1;
myQuery.Criteria.AddCondition("name", ConditionOperator.Equal, parentOpportunity);
EntityCollection pricelistItems = Service.RetrieveMultiple(myQuery);
if (pricelistItems.Entities.Count > 0)
{
foreach (var a in pricelistItems.Entities)
{
if (a.Attributes.Contains("pricelevelid"))
{
var priceLst = a.GetAttributeValue<EntityReference>("pricelevelid");
QueryExpression query2 = new QueryExpression("pricelevel");
ColumnSet columns2 = new ColumnSet("new_basicdealerdiscount");
query2.ColumnSet = columns2;
query2.Criteria.AddCondition("pricelevelid", ConditionOperator.Equal, priceLst.Id);
EntityCollection discountItem = Service.RetrieveMultiple(query2);
if (discountItem.Entities.Count > 0)
{
foreach (var v in discountItem.Entities)
{
if (v.Attributes.Contains("new_basicdealerdiscount"))
{
newEntity["new_percentagediscount"] = Convert.ToDecimal(v.Attributes["new_basicdealerdiscount"]);
discount = Convert.ToDecimal(v.Attributes["new_basicdealerdiscount"]);
if (e.Attributes.Contains("priceperunit"))
{
PriceperUnit = (Money)e.GetAttributeValue<Money>("priceperunit");
myPluginOpportunityProductDiscount obj = new myPluginOpportunityProductDiscount();
newEntity["manualdiscountamount"] = obj.NewVolumeDiscountAmmount(discount, PriceperUnit, quantity);
//Fetch the updated est. revenue
String[] attributes1 = { "estimatedvalue", "opportunityid" };
Entity EstRevRef = myClass.GetEntity(Oppty, Service, attributes1);
//For dealer and ship to dealer revenue will be equal to est. revenue
Money estRev = (Money)EstRevRef["estimatedvalue"];
opp["new_dealerrevenue"] = estRev;
Service.Update(opp);
}
newEntity.Id = entity.Id;
Service.Update(newEntity);
I have tried to register my plugin as both synchronous as well as asynchronous, but the issue is same in both the cases. Please help me to correct it.
Thanks in advance !!