locked
Dynamics CRM plugin - Pre operation update RRS feed

  • Question

  • Hello,
    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);

            }
        }
    }
    Tuesday, October 18, 2016 5:44 AM

All replies

  • Hi,

    Your target entity will only contain the properties which are updated when your plugin is firing. So if you are trying to get the value of edm_CashAmount  and that field was unchanged, it will return null. I suggest using a pre-entity image on your plugin to capture all field values.

    Regards,

    Adam

    Tuesday, October 18, 2016 10:31 AM