コード例 #1
0
        public void ComputeVarianceAndUpdateStatus()
        {
            #region 1. Setup / Arrange
            var orgServiceMock = new Mock <IOrganizationService>();
            var orgService     = orgServiceMock.Object;
            var orgTracingMock = new Mock <ITracingService>();
            var orgTracing     = orgTracingMock.Object;


            #region VehicleCountSchedule EntityCollection
            var VehicleCountSchedule = new EntityCollection
            {
                EntityName = "gsc_iv_vehiclecountschedule",
                Entities   =
                {
                    new Entity
                    {
                        Id          = Guid.NewGuid(),
                        LogicalName = "gsc_iv_vehiclecountschedule",
                        EntityState = EntityState.Created,
                        Attributes  = new AttributeCollection
                        {
                            { "gsc_description",  "Put desc here."              },
                            { "gsc_status",       new OptionSetValue(100000000) },
                            { "gsc_documentdate", DateTime.Today                }
                        }
                    }
                }
            };
            #endregion

            #region VehicleCountEntry EntityCollection
            var VehicleCountEntry = new EntityCollection
            {
                EntityName = "gsc_iv_vehiclecountentry",
                Entities   =
                {
                    new Entity
                    {
                        Id          = Guid.NewGuid(),
                        LogicalName = "gsc_iv_vehiclecountentry",
                        EntityState = EntityState.Created,
                        Attributes  = new AttributeCollection
                        {
                            { "gsc_vehiclecountscheduleid", new EntityReference(VehicleCountSchedule.EntityName, VehicleCountSchedule.Entities[0].Id) },
                            { "gsc_status",                 new OptionSetValue(100000000) }
                        }
                    }
                }
            };
            #endregion

            #region VehicleCountEntryDetail EntityCollection
            var VehicleCountEntryDetail = new EntityCollection
            {
                EntityName = "gsc_iv_vehiclecountentrydetails",
                Entities   =
                {
                    new Entity
                    {
                        Id          = Guid.NewGuid(),
                        LogicalName = "gsc_iv_vehiclecountentrydetails",
                        EntityState = EntityState.Created,
                        Attributes  = new AttributeCollection
                        {
                            { "gsc_vehiclecountentryid", new EntityReference(VehicleCountEntry.EntityName, VehicleCountEntry.Entities[0].Id) },
                            { "gsc_onhandqty",                                                       50 },
                            { "gsc_countedqty",                                                      39 },
                            { "gsc_varianceqty", 0 }
                        }
                    }
                }
            };
            #endregion


            orgServiceMock.Setup(service => service.Retrieve(
                                     It.IsAny <string>(),
                                     It.IsAny <Guid>(),
                                     It.IsAny <ColumnSet>())).Returns(VehicleCountEntryDetail.Entities[0]);

            orgServiceMock.Setup((service => service.Update(It.Is <Entity>(entity => entity.LogicalName == VehicleCountEntryDetail.EntityName)))).Callback <Entity>(s => VehicleCountEntryDetail.Entities[0] = s);

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

            orgServiceMock.Setup((service => service.Update(It.Is <Entity>(entity => entity.LogicalName == VehicleCountEntry.EntityName)))).Callback <Entity>(s => VehicleCountEntry.Entities[0] = s);

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

            orgServiceMock.Setup((service => service.Update(It.Is <Entity>(entity => entity.LogicalName == VehicleCountSchedule.EntityName)))).Callback <Entity>(s => VehicleCountSchedule.Entities[0] = s);

            #endregion

            #region 2. Call/Action

            var VehicleCountEntryDetailHandler = new VehicleCountEntryDetailHandler(orgService, orgTracing);
            VehicleCountEntryDetailHandler.ComputeVariance(VehicleCountEntryDetail.Entities[0]);
            VehicleCountEntryDetailHandler.UpdateStatus(VehicleCountEntryDetail.Entities[0]);

            #endregion

            #region 3. Verify
            Assert.AreEqual(11, VehicleCountEntryDetail.Entities[0].GetAttributeValue <Int32>("gsc_varianceqty"));
            Assert.AreEqual(100000001, VehicleCountEntry.Entities[0].GetAttributeValue <OptionSetValue>("gsc_status").Value);
            Assert.AreEqual(100000002, VehicleCountSchedule.Entities[0].GetAttributeValue <OptionSetValue>("gsc_status").Value);


            #endregion
        }
コード例 #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 ExecutePostVehicleCountEntryDetailsUpdate(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;
            Entity countEntryDetail      = (Entity)context.InputParameters["Target"];

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

            if (countEntryDetail.LogicalName != "gsc_iv_vehiclecountentrydetails")
            {
                return;
            }

            if (context.Mode == 0) //Synchronous Plug-in
            {
                string message = context.MessageName;

                try
                {
                    VehicleCountEntryDetailHandler vehicleCountHandler = new VehicleCountEntryDetailHandler(service, trace);

                    var preImageCountedQty = preImageEntity.Contains("gsc_countedqty")
                        ? preImageEntity.GetAttributeValue <Int32>("gsc_countedqty")
                        : 0;
                    var preImageBreakdownCount = preImageEntity.Contains("gsc_breakdowncount")
                        ? preImageEntity.GetAttributeValue <String>("gsc_breakdowncount")
                        : String.Empty;

                    var postImageCountedQty = postImageEntity.Contains("gsc_countedqty")
                        ? postImageEntity.GetAttributeValue <Int32>("gsc_countedqty")
                        : 0;
                    var postImageBreakdownCount = postImageEntity.Contains("gsc_breakdowncount")
                        ? postImageEntity.GetAttributeValue <String>("gsc_breakdowncount")
                        : String.Empty;

                    if (preImageCountedQty != postImageCountedQty)
                    {
                        vehicleCountHandler.ComputeVariance(postImageEntity);
                        vehicleCountHandler.UpdateStatus(postImageEntity);

                        if (postImageEntity.Contains("gsc_verifiedcountedqty") && postImageEntity.GetAttributeValue <Int32>("gsc_verifiedcountedqty") != 0)
                        {
                            vehicleCountHandler.ComputeVerifiedCountedQty(postImageEntity);
                        }
                    }

                    if (preImageBreakdownCount != postImageBreakdownCount)
                    {
                        vehicleCountHandler.ComputeVerifiedCountedQty(postImageEntity);
                    }
                }
                catch (Exception ex)
                {
                    throw new InvalidPluginExecutionException(String.Concat(ex.Message));
                }
            }
        }