コード例 #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 ExecutePostOrderChargeCreate(LocalPluginContext localContext)
        {
            if (localContext == null)
            {
                throw new ArgumentNullException("localContext");
            }

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

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

            try
            {
                SalesOrderChargeHandler salesOrderChargeHandler = new SalesOrderChargeHandler(service, trace);
                salesOrderChargeHandler.SetOrderTotalChargesAmount(salesOrderChargeEntity, 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);
            }
        }
コード例 #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 ExecutePreOrderChargeCreate(LocalPluginContext localContext)
        {
            if (localContext == null)
            {
                throw new ArgumentNullException("localContext");
            }

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

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

            try
            {
                SalesOrderChargeHandler salesOrderChargeHandler = new SalesOrderChargeHandler(service, trace);
                salesOrderChargeHandler.CheckifChargeExists(salesOrderChargeEntity);
                salesOrderChargeHandler.SetChargeDetails(salesOrderChargeEntity, message);
            }
            catch (Exception ex)
            {
                throw new InvalidPluginExecutionException(ex.Message);
            }
        }
コード例 #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 ExecutePreValidateOrderChargeDelete(LocalPluginContext localContext)
        {
            if (localContext == null)
            {
                throw new ArgumentNullException("localContext");
            }

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

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

            try
            {
                SalesOrderChargeHandler salesOrderChargeHandler = new SalesOrderChargeHandler(service, trace);

                #region Calling SetOrderTotalDiscountAmount method

                EntityCollection salesOrderChargeRecords = CommonHandler.RetrieveRecordsByOneValue("gsc_cmn_ordercharge", "gsc_cmn_orderchargeid", salesOrderChargeEntity.Id, service, null, OrderType.Ascending,
                                                                                                   new[] { "gsc_orderid", "gsc_amount", "gsc_actualcost" });

                if (salesOrderChargeRecords != null && salesOrderChargeRecords.Entities.Count > 0)
                {
                    Entity salesOrderCharge = salesOrderChargeRecords.Entities[0];
                    salesOrderChargeHandler.SetOrderTotalChargesAmount(salesOrderCharge, message);
                }

                #endregion
            }
            catch (Exception ex)
            {
                throw new InvalidPluginExecutionException(String.Concat("(Exception)\n", ex.Message, Environment.NewLine, ex.StackTrace, Environment.NewLine, error));
            }
        }
コード例 #4
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 ExecutePostOrderChargeUpdate(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 salesOrderChargeEntity = (Entity)context.InputParameters["Target"];

            if (salesOrderChargeEntity.LogicalName != "gsc_cmn_ordercharge")
            {
                return;
            }

            if (context.Mode == 0) //Synchronous Plugin
            {
                try
                {
                    #region Pre-images
                    var preImageSalesOrderId = preImageEntity.Contains("gsc_orderid")
                        ? preImageEntity.GetAttributeValue <EntityReference>("gsc_orderid").Id
                        : Guid.Empty;

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

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

                    var preImageFree = preImageEntity.GetAttributeValue <Boolean>("gsc_free");
                    #endregion

                    #region Post-images
                    var postImageSalesOrderId = postImageEntity.Contains("gsc_orderid")
                        ? postImageEntity.GetAttributeValue <EntityReference>("gsc_orderid").Id
                        : Guid.Empty;

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

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

                    var postImageFree = postImageEntity.GetAttributeValue <Boolean>("gsc_free");
                    #endregion

                    //Modified By : Jerome Anthony Gerero, Modified On : 9/21/2016
                    //Purpose : Call SetTotalCashOutlayAmount method from SalesOrderHandler on Total Charges Amount change

                    string message = context.MessageName;
                    SalesOrderChargeHandler salesOrderChargeHandler = new SalesOrderChargeHandler(service, trace);
                    SalesOrderHandler       salesOrderHandler       = new SalesOrderHandler(service, trace);

                    Guid salesOrderId = postImageEntity.GetAttributeValue <EntityReference>("gsc_orderid") != null
                        ? postImageEntity.GetAttributeValue <EntityReference>("gsc_orderid").Id
                        : Guid.Empty;

                    Entity salesOrder = service.Retrieve("salesorder", salesOrderId, new ColumnSet(true));

                    //Call functions on Charge Code change
                    if (preImageChargesCode != postImageChargesCode)
                    {
                        salesOrderChargeHandler.SetChargeDetails(postImageEntity, message);
                    }

                    //Call functions on Order ID, Charge Amount change
                    if (preImageSalesOrderId != postImageSalesOrderId || preImageSalesOrderChargeAmount != postImageSalesOrderChargeAmount || preImageFree != postImageFree)
                    {
                        salesOrderChargeHandler.SetOrderTotalChargesAmount(postImageEntity, message);
                        //salesOrderHandler.SetTotalCashOutlayAmount(salesOrder, message);
                    }

                    if (preImageFree != postImageFree)
                    {
                        salesOrderChargeHandler.FreeCharges(postImageEntity);
                    }
                }

                catch (Exception ex)
                {
                    //throw new InvalidPluginExecutionException(String.Concat("(Exception)\n", ex.Message, Environment.NewLine, ex.StackTrace, Environment.NewLine, error));
                    throw new InvalidPluginExecutionException(ex.Message);
                }
            }
        }
コード例 #5
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 Sales Order Charge EntityCollection
            var SalesOrderChargeCollection = new EntityCollection
            {
                EntityName = "gsc_cmn_ordercharge",
                Entities   =
                {
                    new Entity
                    {
                        Id          = Guid.NewGuid(),
                        LogicalName = "gsc_cmn_ordercharge",
                        EntityState = EntityState.Created,
                        Attributes  = new AttributeCollection
                        {
                            { "gsc_orderid", new EntityReference("salesorder", Guid.NewGuid())
                                {
                                    Name = "Sample Order"
                                } },
                            { "gsc_amount",  new Money((Decimal)320000.00) },
                            { "gsc_free",    false }
                        }
                    },

                    //Sales Order Charge records with charge amount value but with 'Free' checked
                    new Entity
                    {
                        Id          = Guid.NewGuid(),
                        LogicalName = "gsc_cmn_ordercharge",
                        EntityState = EntityState.Created,
                        Attributes  = new AttributeCollection
                        {
                            { "gsc_orderid", new EntityReference("salesorder", Guid.NewGuid())
                                {
                                    Name = "Sample Order"
                                } },
                            { "gsc_amount",  new Money((Decimal)80000.00) },
                            { "gsc_free",    true }
                        }
                    },

                    new Entity
                    {
                        Id          = Guid.NewGuid(),
                        LogicalName = "gsc_cmn_ordercharge",
                        EntityState = EntityState.Changed,
                        Attributes  = new AttributeCollection
                        {
                            { "gsc_orderid", new EntityReference("salesorder", Guid.NewGuid())
                                {
                                    Name = "Sample Order"
                                } },
                            { "gsc_amount",  new Money((Decimal)80000.00) },
                            { "gsc_free",    true }
                        }
                    }
                }
            };
            #endregion

            #region Sales Order EntityCollection
            var SalesOrderCollection = new EntityCollection
            {
                EntityName = "salesorder",
                Entities   =
                {
                    new Entity
                    {
                        Id          = SalesOrderChargeCollection.Entities[0].GetAttributeValue <EntityReference>("gsc_orderid").Id,
                        LogicalName = "salesorder",
                        EntityState = EntityState.Created,
                        Attributes  = new AttributeCollection
                        {
                            { "gsc_totalchargesamount", ""                    },
                            { "statecode",              new OptionSetValue(0) }
                        }
                    }
                }
            };
            #endregion

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

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

            #endregion

            #region 2. Call/Action

            var    SalesOrderChargeHandler = new SalesOrderChargeHandler(orgService, orgTracing);
            Entity salesorder = SalesOrderChargeHandler.SetOrderTotalChargesAmount(SalesOrderChargeCollection.Entities[0], "Create");
            #endregion

            #region 3. Verify
            Assert.AreEqual(SalesOrderChargeCollection.Entities[0].GetAttributeValue <Money>("gsc_amount").Value, SalesOrderCollection.Entities[0].GetAttributeValue <Money>("gsc_totalchargesamount").Value);
            #endregion
        }
コード例 #6
0
        public void SetNullChargeAmount()
        {
            #region 1. Setup / Arrange
            var orgServiceMock = new Mock <IOrganizationService>();
            var orgService     = orgServiceMock.Object;
            var orgTracingMock = new Mock <ITracingService>();
            var orgTracing     = orgTracingMock.Object;

            #region Sales Order Charge EntityCollection
            var SalesOrderChargeCollection = new EntityCollection
            {
                EntityName = "gsc_cmn_ordercharge",
                Entities   =
                {
                    new Entity
                    {
                        Id          = Guid.NewGuid(),
                        LogicalName = "gsc_cmn_ordercharge",
                        EntityState = EntityState.Created,
                        Attributes  = new AttributeCollection
                        {
                            { "gsc_description", String.Empty            },
                            { "gsc_chargesid",   new EntityReference("gsc_cmn_charges", Guid.Empty)
                                {
                                    Name = "Sample Order"
                                } },
                            { "gsc_chargetype",  String.Empty            },
                            { "gsc_amount",      new Money(Decimal.Zero) },
                            { "gsc_free",        false                   }
                        }
                    }
                }
            };
            #endregion

            #region Charges EntityCollection
            var ChargesCollection = new EntityCollection
            {
                EntityName = "gsc_cmn_charges",
                Entities   =
                {
                    new Entity
                    {
                        Id          = Guid.NewGuid(),
                        LogicalName = "gsc_cmn_charges",
                        EntityState = EntityState.Created,
                        Attributes  = new AttributeCollection
                        {
                            { "gsc_description",  "Delivery Charge"          },
                            { "gsc_chargeamount", new Money((Decimal)800.00) },
                            { "gsc_chargetype",   new OptionSetValue(0)      }
                        },
                        FormattedValues =
                        {
                            { "gsc_chargetype", "Delivery Charge"            }
                        }
                    }
                }
            };
            #endregion

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

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

            #endregion

            #region 2. Call/Action

            var    SalesOrderChargeHandler = new SalesOrderChargeHandler(orgService, orgTracing);
            Entity salesOrderCharge        = SalesOrderChargeHandler.SetChargeDetails(SalesOrderChargeCollection.Entities[0], "Create");

            #endregion

            #region 3. Verify
            Assert.AreEqual(SalesOrderChargeCollection.Entities[0].GetAttributeValue <Money>("gsc_amount").Value, Decimal.Zero);
            Assert.AreEqual(SalesOrderChargeCollection.Entities[0].GetAttributeValue <String>("gsc_description"), String.Empty);
            Assert.AreEqual(SalesOrderChargeCollection.Entities[0].GetAttributeValue <String>("gsc_chargetype"), String.Empty);
            #endregion
        }