Ejemplo n.º 1
0
        /// <summary>
        /// Executes the plug-in.
        /// </summary>
        /// <param name="localContext">The <see cref="LocalPluginContext"/> which contains the
        /// <see cref="IPluginExecutionContext"/>,
        /// <see cref="IOrganizationService"/>
        /// and <see cref="ITracingService"/>
        /// </param>
        /// <remarks>
        /// For improved performance, Microsoft Dynamics CRM caches plug-in instances.
        /// The plug-in's Execute method should be written to be stateless as the constructor
        /// is not called for every invocation of the plug-in. Also, multiple system threads
        /// could execute the plug-in at the same time. All per invocation state information
        /// is stored in the context. This means that you should not use global variables in plug-ins.
        /// </remarks>
        protected void ExecutePreQuoteChargeCreate(LocalPluginContext localContext)
        {
            if (localContext == null)
            {
                throw new ArgumentNullException("localContext");
            }

            IPluginExecutionContext context = localContext.PluginExecutionContext;
            IOrganizationService    service = localContext.OrganizationService;
            ITracingService         trace   = localContext.TracingService;
            Entity quoteChargeEntity        = (Entity)context.InputParameters["Target"];

            string message = context.MessageName;

            try
            {
                QuoteChargeHandler quoteChargeHandler = new QuoteChargeHandler(service, trace);
                quoteChargeHandler.CheckifChargeExists(quoteChargeEntity);
                quoteChargeHandler.ReplicateChargeAmount(quoteChargeEntity, message);
            }
            catch (Exception ex)
            {
                throw new InvalidPluginExecutionException(ex.Message);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Executes the plug-in.
        /// </summary>
        /// <param name="localContext">The <see cref="LocalPluginContext"/> which contains the
        /// <see cref="IPluginExecutionContext"/>,
        /// <see cref="IOrganizationService"/>
        /// and <see cref="ITracingService"/>
        /// </param>
        /// <remarks>
        /// For improved performance, Microsoft Dynamics CRM caches plug-in instances.
        /// The plug-in's Execute method should be written to be stateless as the constructor
        /// is not called for every invocation of the plug-in. Also, multiple system threads
        /// could execute the plug-in at the same time. All per invocation state information
        /// is stored in the context. This means that you should not use global variables in plug-ins.
        /// </remarks>
        protected void ExecutePostQuoteChargeCreate(LocalPluginContext localContext)
        {
            if (localContext == null)
            {
                throw new ArgumentNullException("localContext");
            }

            IPluginExecutionContext context = localContext.PluginExecutionContext;
            IOrganizationService    service = localContext.OrganizationService;
            ITracingService         trace   = localContext.TracingService;
            Entity quoteChargeEntity        = (Entity)context.InputParameters["Target"];

            string message = context.MessageName;
            string error   = "";

            try
            {
                QuoteChargeHandler quoteChargeHandler = new QuoteChargeHandler(service, trace);
                quoteChargeHandler.SetQuoteTotalChargesAmount(quoteChargeEntity, message);
            }
            catch (Exception ex)
            {
                //throw new InvalidPluginExecutionException(String.Concat("(Exception)\n", ex.Message, Environment.NewLine, ex.StackTrace, Environment.NewLine, error));
                throw new InvalidPluginExecutionException(ex.Message);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Executes the plug-in.
        /// </summary>
        /// <param name="localContext">The <see cref="LocalPluginContext"/> which contains the
        /// <see cref="IPluginExecutionContext"/>,
        /// <see cref="IOrganizationService"/>
        /// and <see cref="ITracingService"/>
        /// </param>
        /// <remarks>
        /// For improved performance, Microsoft Dynamics CRM caches plug-in instances.
        /// The plug-in's Execute method should be written to be stateless as the constructor
        /// is not called for every invocation of the plug-in. Also, multiple system threads
        /// could execute the plug-in at the same time. All per invocation state information
        /// is stored in the context. This means that you should not use global variables in plug-ins.
        /// </remarks>
        protected void ExecutePreValidateQuoteChargeDelete(LocalPluginContext localContext)
        {
            if (localContext == null)
            {
                throw new ArgumentNullException("localContext");
            }

            IPluginExecutionContext context = localContext.PluginExecutionContext;
            IOrganizationService    service = localContext.OrganizationService;
            ITracingService         trace   = localContext.TracingService;
            var    quoteChargeEntity        = (EntityReference)context.InputParameters["Target"];
            string message = context.MessageName;
            string error   = "";

            if (context.Depth > 1)
            {
                return;
            }

            try
            {
                QuoteChargeHandler quoteChargeHandler = new QuoteChargeHandler(service, trace);

                #region Calling SetQuoteTotalChargesAmount method

                EntityCollection quoteChargeRecords = CommonHandler.RetrieveRecordsByOneValue("gsc_cmn_quotecharge", "gsc_cmn_quotechargeid", quoteChargeEntity.Id, service, null, OrderType.Ascending,
                                                                                              new[] { "gsc_quoteid", "gsc_chargeamount", "gsc_chargetype", "gsc_actualcost" });

                if (quoteChargeRecords != null && quoteChargeRecords.Entities.Count > 0)
                {
                    Entity quoteCharge = quoteChargeRecords.Entities[0];
                    quoteChargeHandler.SetQuoteTotalChargesAmount(quoteCharge, message);
                }

                #endregion
            }
            catch (Exception ex)
            {
                throw new InvalidPluginExecutionException(String.Concat("(Exception)\n", ex.Message, Environment.NewLine, ex.StackTrace, Environment.NewLine, error));
            }
        }
Ejemplo n.º 4
0
        public void FreeFieldCheckedUnitTest()
        {
            #region 1. Setup / Arrange
            var orgServiceMock = new Mock <IOrganizationService>();
            var orgService     = orgServiceMock.Object;
            var orgTracingMock = new Mock <ITracingService>();
            var orgTracing     = orgTracingMock.Object;

            #region Quote Charge EntityCollection
            var QuoteChargeCollection = new EntityCollection
            {
                EntityName = "gsc_cmn_quotecharge",
                Entities   =
                {
                    new Entity
                    {
                        Id          = Guid.NewGuid(),
                        LogicalName = "gsc_cmn_quotecharge",
                        EntityState = EntityState.Created,
                        Attributes  = new AttributeCollection
                        {
                            { "gsc_quoteid",      new EntityReference("quote", Guid.NewGuid())
                                {
                                    Name = "Sample Quote"
                                } },
                            { "gsc_chargeamount", new Money((Decimal)320000.00) },
                            { "gsc_free",         false }
                        }
                    },

                    //Applied Charges records with charge amount value but with 'Free' checked
                    new Entity
                    {
                        Id          = Guid.NewGuid(),
                        LogicalName = "gsc_cmn_quotecharge",
                        EntityState = EntityState.Created,
                        Attributes  = new AttributeCollection
                        {
                            { "gsc_quoteid",      new EntityReference("quote", Guid.NewGuid())
                                {
                                    Name = "Sample Quote"
                                } },
                            { "gsc_chargeamount", new Money((Decimal)80000.00) },
                            { "gsc_free",         true }
                        }
                    },

                    new Entity
                    {
                        Id          = Guid.NewGuid(),
                        LogicalName = "gsc_cmn_appliedcharges",
                        EntityState = EntityState.Changed,
                        Attributes  = new AttributeCollection
                        {
                            { "gsc_quoteid",      new EntityReference("quote", Guid.NewGuid())
                                {
                                    Name = "Sample Quote"
                                } },
                            { "gsc_chargeamount", new Money((Decimal)80000.00) },
                            { "gsc_free",         true }
                        }
                    }
                }
            };
            #endregion

            #region Quote EntityCollection
            var QuoteCollection = new EntityCollection
            {
                EntityName = "quote",
                Entities   =
                {
                    new Entity
                    {
                        Id          = QuoteChargeCollection.Entities[0].GetAttributeValue <EntityReference>("gsc_quoteid").Id,
                        LogicalName = "quote",
                        EntityState = EntityState.Created,
                        Attributes  = new AttributeCollection
                        {
                            { "gsc_totalchargesamount", ""                    },
                            { "statecode",              new OptionSetValue(0) }
                        }
                    }
                }
            };
            #endregion

            orgServiceMock.Setup((service => service.RetrieveMultiple(
                                      It.Is <QueryExpression>(expression => expression.EntityName == QuoteChargeCollection.EntityName)
                                      ))).Returns(QuoteChargeCollection);

            orgServiceMock.Setup((service => service.RetrieveMultiple(
                                      It.Is <QueryExpression>(expression => expression.EntityName == QuoteCollection.EntityName)
                                      ))).Returns(QuoteCollection);

            #endregion

            #region 2. Call/Action

            var    QuoteChargeHandler = new QuoteChargeHandler(orgService, orgTracing);
            Entity quote = QuoteChargeHandler.SetQuoteTotalChargesAmount(QuoteChargeCollection.Entities[0], "Create");
            #endregion

            #region 3. Verify
            Assert.AreEqual(QuoteChargeCollection.Entities[0].GetAttributeValue <Money>("gsc_chargeamount").Value, QuoteCollection.Entities[0].GetAttributeValue <Money>("gsc_totalchargesamount").Value);
            #endregion
        }
Ejemplo n.º 5
0
        public void ReplicateChargeAmountOnSave()
        {
            #region 1. Setup / Arrange
            var orgServiceMock = new Mock <IOrganizationService>();
            var orgService     = orgServiceMock.Object;
            var orgTracingMock = new Mock <ITracingService>();
            var orgTracing     = orgTracingMock.Object;

            #region Charge EntityCollection
            var ChargeCollection = new EntityCollection
            {
                EntityName = "gsc_cmn_charges",
                Entities   =
                {
                    new Entity
                    {
                        Id          = Guid.NewGuid(),
                        LogicalName = "gsc_cmn_charges",
                        EntityState = EntityState.Created,
                        Attributes  = new AttributeCollection
                        {
                            { "gsc_description",  "Description"         },
                            { "gsc_chargeamount", new Money(20000)      },
                            { "gsc_chargetype",   new OptionSetValue(1) }
                        }
                    }
                }
            };
            #endregion

            #region QuoteCharge Entity
            var QuoteEntity = new Entity()
            {
                Id          = Guid.NewGuid(),
                LogicalName = "quote",
                Attributes  =
                {
                }
            };
            #endregion

            #region QuoteCharge Entity
            var QuoteChargeEntity = new Entity()
            {
                Id          = Guid.NewGuid(),
                LogicalName = "gsc_cmn_quotecharge",
                Attributes  =
                {
                    { "gsc_quoteid",      new EntityReference(QuoteEntity.LogicalName,     QuoteEntity.Id)                  },
                    { "gsc_chargesid",    new EntityReference(ChargeCollection.EntityName, ChargeCollection.Entities[0].Id) },
                    { "gsc_description",  "" },
                    { "gsc_chargeamount", new Money() },
                    { "gsc_chargetype",   new OptionSetValue() }
                }
            };
            #endregion

            orgServiceMock.Setup((service => service.RetrieveMultiple(
                                      It.Is <QueryExpression>(expression => expression.EntityName == ChargeCollection.EntityName)
                                      ))).Returns(ChargeCollection);

            orgServiceMock.Setup(service => service.Retrieve(
                                     It.IsAny <string>(),
                                     It.IsAny <Guid>(),
                                     It.IsAny <ColumnSet>())).Returns(QuoteChargeEntity);

            orgServiceMock.Setup((service => service.Update(It.Is <Entity>(entity => entity.LogicalName == QuoteChargeEntity.LogicalName)))).Callback <Entity>(s => QuoteChargeEntity = s);

            #endregion

            #region 2. Call/Action

            var    QuoteChargeHandler = new QuoteChargeHandler(orgService, orgTracing);
            Entity quote = QuoteChargeHandler.ReplicateChargeAmount(QuoteChargeEntity, "Create");
            #endregion

            #region 3. Verify
            Assert.AreEqual(ChargeCollection.Entities[0]["gsc_description"], QuoteChargeEntity["gsc_description"]);
            Assert.AreEqual(ChargeCollection.Entities[0].GetAttributeValue <Money>("gsc_chargeamount").Value, QuoteChargeEntity.GetAttributeValue <Money>("gsc_chargeamount").Value);
            Assert.AreEqual(ChargeCollection.Entities[0].GetAttributeValue <OptionSetValue>("gsc_chargetype").Value, QuoteChargeEntity.GetAttributeValue <OptionSetValue>("gsc_chargetype").Value);
            #endregion
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Executes the plug-in.
        /// </summary>
        /// <param name="localContext">The <see cref="LocalPluginContext"/> which contains the
        /// <see cref="IPluginExecutionContext"/>,
        /// <see cref="IOrganizationService"/>
        /// and <see cref="ITracingService"/>
        /// </param>
        /// <remarks>
        /// For improved performance, Microsoft Dynamics CRM caches plug-in instances.
        /// The plug-in's Execute method should be written to be stateless as the constructor
        /// is not called for every invocation of the plug-in. Also, multiple system threads
        /// could execute the plug-in at the same time. All per invocation state information
        /// is stored in the context. This means that you should not use global variables in plug-ins.
        /// </remarks>
        protected void ExecutePostQuoteChargeUpdate(LocalPluginContext localContext)
        {
            if (localContext == null)
            {
                throw new ArgumentNullException("localContext");
            }

            IPluginExecutionContext context = localContext.PluginExecutionContext;

            Entity preImageEntity  = (context.PreEntityImages != null && context.PreEntityImages.Contains(this.preImageAlias)) ? context.PreEntityImages[this.preImageAlias] : null;
            Entity postImageEntity = (context.PostEntityImages != null && context.PostEntityImages.Contains(this.postImageAlias)) ? context.PostEntityImages[this.postImageAlias] : null;

            IOrganizationService service = localContext.OrganizationService;
            ITracingService      trace   = localContext.TracingService;

            if (!(context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity))
            {
                return;
            }

            Entity quoteChargeEntity = (Entity)context.InputParameters["Target"];

            if (quoteChargeEntity.LogicalName != "gsc_cmn_quotecharge")
            {
                return;
            }

            if (context.Mode == 0) //Synchronous Plugin
            {
                try
                {
                    QuoteChargeHandler quoteChargeHandler = new QuoteChargeHandler(service, trace);

                    string message = context.MessageName;

                    var preImageChargeId = preImageEntity.Contains("gsc_chargesid")
                        ? preImageEntity.GetAttributeValue <EntityReference>("gsc_chargesid").Id
                        : Guid.Empty;

                    var postImageChargeId = postImageEntity.Contains("gsc_chargesid")
                        ? postImageEntity.GetAttributeValue <EntityReference>("gsc_chargesid").Id
                        : Guid.Empty;

                    var preActualCost = preImageEntity.Contains("gsc_actualcost")
                        ? preImageEntity.GetAttributeValue <Money>("gsc_actualcost").Value
                        : Decimal.Zero;

                    var postActualCost = postImageEntity.Contains("gsc_actualcost")
                        ? postImageEntity.GetAttributeValue <Money>("gsc_actualcost").Value
                        : Decimal.Zero;

                    var preImageFree  = preImageEntity.GetAttributeValue <Boolean>("gsc_free");
                    var postImageFree = postImageEntity.GetAttributeValue <Boolean>("gsc_free");

                    if (preImageChargeId != postImageChargeId)
                    {
                        quoteChargeHandler.ReplicateChargeAmount(postImageEntity, message);
                        quoteChargeHandler.SetQuoteTotalChargesAmount(postImageEntity, message);
                    }

                    if (preImageFree != postImageFree)
                    {
                        quoteChargeHandler.FreeCharges(postImageEntity);
                        quoteChargeHandler.SetQuoteTotalChargesAmount(postImageEntity, message);
                    }

                    if (preActualCost != postActualCost)
                    {
                        quoteChargeHandler.SetQuoteTotalChargesAmount(postImageEntity, message);
                    }
                }

                catch (Exception ex)
                {
                    //throw new InvalidPluginExecutionException(String.Concat("(Exception)\n", ex.Message, Environment.NewLine, ex.StackTrace, Environment.NewLine, error));
                    throw new InvalidPluginExecutionException(ex.Message);
                }
            }
        }