Search code examples
pluginsdynamics-crmdynamics-crm-2016

Dynamics CRM plugin -Pre Operation Update


I created a plug-in that calculates the pricing of the quote based on custom fields whenever update done on the quote entity.I executed the plugin on pre-opertaion update of the quote entity.I tried to debbug my plugin, it retrives null values.For example the value of the field "edm_CashAmount" contains 5000 but the code retrieved null. Kindly advise on the below. Thanks in advance.

namespace CRMPlugins.Plugins
{
    using System;
    using System.ServiceModel;
    using Microsoft.Xrm.Sdk;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Crm.Sdk.Messages;

    public class PrequoteUpdate : Plugin
    {

        public PrequoteUpdate()
            : base(typeof(PrequoteUpdate))
        {
            base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(20, "Update", "quote", new Action<LocalPluginContext>(ExecutePrequoteUpdate)));

        }

        protected void ExecutePrequoteUpdate(LocalPluginContext localContext)
        {
            if (localContext == null)
            {
                throw new ArgumentNullException("localContext");
            }

            Entity entity = null;

            if (localContext.PluginExecutionContext.InputParameters.Contains("Target") && localContext.PluginExecutionContext.InputParameters["Target"] is Entity)
            {
                entity = (Entity)localContext.PluginExecutionContext.InputParameters["Target"];
            }
            else
            {
                return;
            }
            decimal instotal = 0;
            Quote quote = entity.ToEntity<Quote>();
            using (GeneratedEntities orgContext = new GeneratedEntities(localContext.OrganizationService))
            {
                var installements = (from b in orgContext.edm_installementSet
                                     where b.GetAttributeValue<Guid>("edm_quote") == quote.QuoteId
                                     select b);

                foreach (var c in installements)
                {

                    if (c.edm_Amount != null)
                    {
                        instotal += (decimal)c.new_Decimal;

                    }
                }
            }
            decimal cash = 0;
            if (quote.edm_CashAmount != null)
            {
                cash = Convert.ToDecimal(quote.GetAttributeValue<Money>("edm_cashamount").Value);
            }
            decimal check = 0;
            if (quote.edm_CheckAmount != null)
            {
                check = Convert.ToDecimal(quote.GetAttributeValue<Money>("edm_checkamount").Value);
            }
            decimal credit = 0;
            if (quote.edm_CreditCardAmount != null)
            {
                credit = Convert.ToDecimal(quote.GetAttributeValue<Money>("edm_creditcardamount").Value);
            }
            decimal gift = 0;
            if (quote.new_GiftCard != null)
            {
                gift = Convert.ToDecimal(quote.GetAttributeValue<Money>("new_giftcard").Value);
            }
            decimal total = 0;
            if (quote.TotalLineItemAmount != null)
            {
                total = Convert.ToDecimal(quote.GetAttributeValue<Money>("totallineitemamount").Value);
            }

            decimal currentbalane = 0;
            if (quote.edm_CurrentBalane != null)
            {
                currentbalane = Convert.ToDecimal(quote.GetAttributeValue<Money>("edm_currentbalane").Value);
            }


            decimal DiscAmount = 0;
            if (quote.DiscountAmount != null)
            {
                DiscAmount = Convert.ToDecimal(quote.GetAttributeValue<Money>("discountamount").Value);

            }

            decimal totalafterdiscount = total - DiscAmount;
            decimal tax = (totalafterdiscount * 10) / 100;
            decimal totalwithvat = totalafterdiscount + tax;
            decimal paidamount = cash + check + credit + gift + instotal;
            decimal remainingamount = totalwithvat - paidamount;
            quote["edm_cashamount_1"] = new Money(tax);
            quote["edm_totaltax"] = new Money(tax);
            quote["edm_paidamount"] = new Money(paidamount);
            quote["edm_remainingamount"] = new Money(remainingamount);
            quote["edm_total"] = new Money(totalwithvat);
            quote["edm_totalinstallements"] = new Money(instotal);
            quote["edm_checkamount_1"] = new Money(totalwithvat);

        }
    }
}

Solution

  • for update plugins, if the value has not been changed then it will not appear in the target entity. Therefore you should create a pre entity image an include these values, or you can create a post entity image to see the state of the entity after the changes will be applied