locked
Plugin PostUpdate Context.Depth RRS feed

  • Question

  • Hi,

    after editing opportunityproducts there are some calculations in the current form, so I wrote a plugin registered as post update event.

    Here some parts of it:

            protected override void RegisterExecution()
            {
                AddExecution(PostUpdateOpportunityProduct, MessageName.Update, Conditions.PostExecute);
            }

             protected void PostUpdateOpportunityProduct()
            {

                    IOpportunityProduct opportunityProduct =
                        EntityFactory.Create<IOpportunityProduct>(Context.PostEntityImages["DefaultImage"] as DynamicEntity);
                    Guid oPGuid = opportunityProduct.OpportunityProductId.Value;
                    
                    if (Context.Depth < 2)
                    {
                        retrieveTest(oPGuid);
                    }
             }

            public void retrieveTest(Guid oPGuid)
            {
                DynamicEntity followup = new DynamicEntity();
                followup.Name = EntityName.opportunityproduct.ToString();

                CrmService service = GetCrmService();
                service.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;

                ColumnSet columnSet = new ColumnSet();
                columnSet.AddColumn("opportunityproductid");
                columnSet.AddColumn("quantity");

                TargetRetrieveDynamic target = new TargetRetrieveDynamic();
                target.EntityId = oPGuid;
                target.EntityName = EntityName.opportunityproduct.ToString();

                RetrieveRequest retrieve = new RetrieveRequest();
                retrieve.Target = target;
                retrieve.ColumnSet = columnSet;
                retrieve.ReturnDynamicEntities = true;

                RetrieveResponse response = (RetrieveResponse)service.Execute(retrieve);
                DynamicEntity retrievedEntity = (DynamicEntity)response.BusinessEntity;

                DynamicEntity entity = (DynamicEntity)response.BusinessEntity;

                //9 is a calculated value
                CrmDecimalProperty quantityProperty = new CrmDecimalProperty("quantity", new CrmDecimal(9));
                entity.Properties.Add(quantityProperty);
                service.Update(entity);
                return;
          }

    }

     

    but the Context.Depth condition does not work, so I have an infinite loop.

    Is there anybody woho can help me?

    Best regards, Eckart

    Thursday, September 9, 2010 7:07 AM

Answers

  • Thanks, I will check it asap abd let you know.
    • Marked as answer by Eegi Friday, September 10, 2010 6:34 AM
    Thursday, September 9, 2010 12:50 PM

All replies

  • you can try to register plugin on preupdate and do the calculation and add your property in input propertybag. so no need of calling update method in your plugin

    Let us know


    Mahain : http://mahenderpal.wordpress.com
    Thursday, September 9, 2010 10:11 AM
  • Hi Mahender,

    in the pre event I don't have the new edited values to calculate. Or is there a possibility to identify them?

    Best regrads, Eckart

     

    Thursday, September 9, 2010 11:33 AM
  • Where are you getting/setting the Context variable as you should avoid class scoped variables in plugins (if this is what you are doing).


    MSCRM Bing'd - http://bingsoft.wordpress.com
    Thursday, September 9, 2010 11:36 AM
    Moderator
  • Hi,

    I solved the problem changing the code:

    using System;
    using System.Collections.Generic;
    using System.Text;
    using Infoman.Crm.Core.Platform.Logging;
    using Infoman.Crm.Core.Platform.Entities;
    using Infoman.Crm.Core.Platform.Plugins;
    using Microsoft.Crm.Sdk;
    using Microsoft.Crm.SdkTypeProxy;
    using Microsoft.Crm.Sdk.Query;

    namespace Draeger.Plugin.Crm.OpportunityProductDiscountCalculator
    {
        public class OpportunityProductManualDiscountCalculator : PluginBase
        {
            protected override void RegisterExecution()
            {
                AddExecution(PostUpdateOpportunityProduct, MessageName.Update, Conditions.PostExecute);
                AddExecution(PostUpdateOpportunityProduct, MessageName.Create, Conditions.PostExecute);
            }


            protected void PostUpdateOpportunityProduct()
            {
                try
                {
                    IOpportunityProduct opportunityProduct =
                        EntityFactory.Create<IOpportunityProduct>(Context.PostEntityImages["DefaultImage"] as DynamicEntity);
                    if (opportunityProduct == null)
                    {
                        Logger.WriteVerbose("No valid opportunityProduct found!");
                    }
                    else
                    {
                        Guid oPGuid = opportunityProduct.OpportunityProductId.Value;

                        retrieveManualDiscount(oPGuid, opportunityProduct);
                    }
                }
                catch (Exception e)
                {
                    Logger.WriteVerbose("some error! " + e.Message);
                }
            }

            public void retrieveManualDiscount(Guid oPGuid, IOpportunityProduct opportunityProduct)
            {
                DynamicEntity followup = new DynamicEntity();
                followup.Name = EntityName.opportunityproduct.ToString();

                ICrmService service = Context.CreateCrmService(true);

                ColumnSet columnSet = new ColumnSet();
                columnSet.AddColumn("manualdiscountamount");

                TargetRetrieveDynamic target = new TargetRetrieveDynamic();
                target.EntityId = oPGuid;
                target.EntityName = EntityName.opportunityproduct.ToString();

                RetrieveRequest retrieve = new RetrieveRequest();
                retrieve.Target = target;
                retrieve.ColumnSet = columnSet;
                retrieve.ReturnDynamicEntities = true;

                // Create a response reference and execute the retrieve request.
                RetrieveResponse response = (RetrieveResponse)service.Execute(retrieve);
                DynamicEntity entity = (DynamicEntity)response.BusinessEntity;
               
                decimal manualDiscountCmp = opportunityProduct.ManualDiscountAmount.Value;
                Logger.WriteVerbose("Old manualDiscount: " + manualDiscountCmp);
               
                decimal manualDiscount = PrepareAndCalculateManualDiscount(opportunityProduct);
                CrmMoneyProperty manualDiscountProperty = new CrmMoneyProperty("manualdiscountamount", new CrmMoney(manualDiscount));
                entity.Properties.Add(manualDiscountProperty);
                if (manualDiscount !=  manualDiscountCmp)
                {
                    Logger.WriteVerbose("New manualDiscount: " + manualDiscount);
                    service.Update(entity);
                }
                return;
            }

            public decimal PrepareAndCalculateManualDiscount(IOpportunityProduct opportunityProduct)
            {
                decimal pricePerUnit = opportunityProduct.Speed1PriceperUnit.Value;
                decimal quantity = opportunityProduct.Quantity.Value;
                decimal listPrice = opportunityProduct.PricePerUnit.Value;

                decimal manualDiscount = CalculateManualDiscount(pricePerUnit, quantity, listPrice);
                return manualDiscount;
            }

            public decimal CalculateManualDiscount(decimal pricePerUnit, decimal quantity, decimal listPrice)
            {
                return (listPrice - pricePerUnit) * quantity;
            }
        }
    }

    As you see I check the computed value before updating the service, so it will work only once.

    @Rett: I don't understand the question. Was ist related to the pre update event? That's what I still want to know: How can I check edited values in the pre update event.

    Also the problem with the Context.Depth is not solved for me but it doesnt matter in my case.

    Thanks all and I would be happy to get answers to my last two questions.

    Best regrads, Eckart

    Thursday, September 9, 2010 12:24 PM
  • you should get these values in input property bag
    Mahain : http://mahenderpal.wordpress.com
    Thursday, September 9, 2010 12:34 PM
  • Thanks, I will check it asap abd let you know.
    • Marked as answer by Eegi Friday, September 10, 2010 6:34 AM
    Thursday, September 9, 2010 12:50 PM
  • Then all you have to do is add your new/updated properties back into the input property bag's dynamic entity for it to be saved.
    MSCRM Bing'd - http://bingsoft.wordpress.com
    Thursday, September 9, 2010 1:13 PM
    Moderator