/// <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 ExecutePostVehicleCountScheduleUpdate(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 vehicleCount          = (Entity)context.InputParameters["Target"];

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

            if (vehicleCount.LogicalName != "gsc_iv_vehiclecountschedule")
            {
                return;
            }

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

                try
                {
                    #region Pre-images
                    Guid preImageSite = preImageEntity.GetAttributeValue <EntityReference>("gsc_siteid") != null
                        ? preImageEntity.GetAttributeValue <EntityReference>("gsc_siteid").Id
                        : Guid.Empty;

                    Guid preImageBaseModel = preImageEntity.GetAttributeValue <EntityReference>("gsc_vehiclebasemodelid") != null
                        ? preImageEntity.GetAttributeValue <EntityReference>("gsc_vehiclebasemodelid").Id
                        : Guid.Empty;

                    Guid preImageProduct = preImageEntity.GetAttributeValue <EntityReference>("gsc_productid") != null
                        ? preImageEntity.GetAttributeValue <EntityReference>("gsc_productid").Id
                        : Guid.Empty;

                    Guid preImageColor = preImageEntity.GetAttributeValue <EntityReference>("gsc_vehiclecolorid") != null
                        ? preImageEntity.GetAttributeValue <EntityReference>("gsc_vehiclecolorid").Id
                        : Guid.Empty;

                    String preImageModel = preImageEntity.Contains("gsc_modelcode")
                        ? preImageEntity.GetAttributeValue <String>("gsc_modelcode")
                        : String.Empty;

                    String preImageOption = preImageEntity.Contains("gsc_optioncode")
                        ? preImageEntity.GetAttributeValue <String>("gsc_optioncode")
                        : String.Empty;

                    String preImageCopyFilterResult = preImageEntity.Contains("gsc_copyfilterresult")
                        ? preImageEntity.GetAttributeValue <String>("gsc_copyfilterresult")
                        : String.Empty;

                    String preImageCopyAllFilterResult = preImageEntity.Contains("gsc_copyallfilterresult")
                        ? preImageEntity.GetAttributeValue <String>("gsc_copyallfilterresult")
                        : String.Empty;

                    String preImageClearFilterResult = preImageEntity.Contains("gsc_clearfilterresult")
                        ? preImageEntity.GetAttributeValue <String>("gsc_clearfilterresult")
                        : String.Empty;

                    String preImageClearDetail = preImageEntity.Contains("gsc_cleardetail")
                        ? preImageEntity.GetAttributeValue <String>("gsc_cleardetail")
                        : String.Empty;

                    var preImageChangeStatus = preImageEntity.Contains("gsc_changestatus")
                        ? preImageEntity.GetAttributeValue <OptionSetValue>("gsc_changestatus").Value
                        : 0;

                    var preImageReuseCount = preImageEntity.Contains("gsc_reusecount")
                        ? preImageEntity.GetAttributeValue <String>("gsc_reusecount")
                        : String.Empty;
                    #endregion

                    #region Post-images
                    Guid postImageSite = postImageEntity.GetAttributeValue <EntityReference>("gsc_siteid") != null
                        ? postImageEntity.GetAttributeValue <EntityReference>("gsc_siteid").Id
                        : Guid.Empty;

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

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

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

                    String postImageModel = postImageEntity.Contains("gsc_modelcode")
                        ? postImageEntity.GetAttributeValue <String>("gsc_modelcode")
                        : String.Empty;

                    String postImageOption = postImageEntity.Contains("gsc_optioncode")
                        ? postImageEntity.GetAttributeValue <String>("gsc_optioncode")
                        : String.Empty;

                    String postImageCopyFilterResult = postImageEntity.Contains("gsc_copyfilterresult")
                        ? postImageEntity.GetAttributeValue <String>("gsc_copyfilterresult")
                        : String.Empty;

                    String postImageCopyAllFilterResult = postImageEntity.Contains("gsc_copyallfilterresult")
                        ? postImageEntity.GetAttributeValue <String>("gsc_copyallfilterresult")
                        : String.Empty;

                    String postImageClearFilterResult = postImageEntity.Contains("gsc_clearfilterresult")
                        ? postImageEntity.GetAttributeValue <String>("gsc_clearfilterresult")
                        : String.Empty;

                    String postImageClearDetail = postImageEntity.Contains("gsc_cleardetail")
                        ? postImageEntity.GetAttributeValue <String>("gsc_cleardetail")
                        : String.Empty;

                    var postImageChangeStatus = postImageEntity.Contains("gsc_changestatus")
                        ? postImageEntity.GetAttributeValue <OptionSetValue>("gsc_changestatus").Value
                        : 0;

                    var postImageReuseCount = postImageEntity.Contains("gsc_reusecount")
                        ? postImageEntity.GetAttributeValue <String>("gsc_reusecount")
                        : String.Empty;
                    #endregion

                    VehicleCountScheduleHandler vehicleCountHandler = new VehicleCountScheduleHandler(service, trace);

                    //Functions on Invoice No change
                    if (preImageSite != postImageSite || preImageBaseModel != postImageBaseModel || preImageProduct != postImageProduct ||
                        preImageColor != postImageColor || preImageModel != postImageModel || preImageOption != postImageOption)
                    {
                        vehicleCountHandler.ApplyFilter(postImageEntity);
                    }

                    if (preImageCopyFilterResult != postImageCopyFilterResult)
                    {
                        vehicleCountHandler.ReplicateFilterResult(postImageEntity);
                    }

                    if (preImageCopyAllFilterResult != postImageCopyAllFilterResult)
                    {
                        vehicleCountHandler.ReplicateAllFilterResult(postImageEntity);
                    }

                    if (preImageClearFilterResult != postImageClearFilterResult)
                    {
                        vehicleCountHandler.ClearFilterResults(postImageEntity);
                    }

                    if (preImageClearDetail != postImageClearDetail)
                    {
                        vehicleCountHandler.ClearVehicleForCounting(postImageEntity);
                    }

                    if (preImageChangeStatus != postImageChangeStatus)
                    {
                        if (postImageChangeStatus == 100000001)
                        {
                            vehicleCountHandler.CheckDuplicateStartedSchedule(postImageEntity);
                        }

                        else if (postImageChangeStatus == 100000000)
                        {
                            vehicleCountHandler.CancelCountSchedule(postImageEntity);
                        }
                    }

                    if (preImageReuseCount != postImageReuseCount)
                    {
                        vehicleCountHandler.ReuseCountSchedule(postImageEntity);
                    }
                }
                catch (Exception ex)
                {
                    throw new InvalidPluginExecutionException(String.Concat(ex.Message));
                }
            }
        }
        public void FilterResultEmpty()
        {
            #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_siteid", new EntityReference("site", new Guid("e0968b43-af44-e611-80da-00155d010e2c"))},
                          * {"gsc_vehiclebasemodelid", new EntityReference("basemodel", new Guid("e0968b43-af44-e611-80da-00155d010e2a"))},
                          * {"gsc_productid", new EntityReference("basemodel", new Guid("e0968b43-af44-e611-80da-00155d010e2b"))},
                          * {"gsc_vehiclecolorid", new EntityReference("color", Guid.NewGuid()) { Name = "Blue"}},
                          * {"gsc_modelcode", "0001"},
                          * {"gsc_optioncode", "002"}*/
                            { "gsc_siteid",             new EntityReference("site", new Guid("e0968b43-af44-e611-80da-00155d010e2c")) },
                            { "gsc_vehiclebasemodelid", null },
                            { "gsc_productid",          null },
                            { "gsc_colo",               "" },
                            { "gsc_modelcode",          "" },
                            { "gsc_optioncode",         "" }
                        }
                    }
                }
            };
            #endregion

            #region Product EntityCollection
            var ProductCollection = new EntityCollection
            {
                EntityName = "product",
                Entities   =
                {
                    new Entity
                    {
                        Id          = Guid.NewGuid(),
                        LogicalName = "product",
                        EntityState = EntityState.Created,
                        Attributes  = new AttributeCollection
                        {
                            { "gsc_modelcode",  "0001" },
                            { "gsc_optioncode", "0002" }
                        }
                    }
                }
            };
            #endregion

            #region Product Quantity EntityCollection
            var ProductQuantityCollection = new EntityCollection
            {
                EntityName = "gsc_iv_productquantity",
                Entities   =
                {
                    new Entity
                    {
                        Id          = Guid.NewGuid(),
                        LogicalName = "gsc_iv_productquantity",
                        EntityState = EntityState.Created,
                        Attributes  = new AttributeCollection
                        {
                            { "gsc_siteid",         new EntityReference("site",                       new Guid("e0968b43-af44-e611-80da-00155d010e2c")) },
                            { "gsc_vehiclemodelid", new EntityReference("basemodel",                  new Guid("e0968b43-af44-e611-80da-00155d010e2a")) },
                            { "gsc_vehiclecolorid", new EntityReference("vehiclecolor",               new Guid("e0968b43-af44-e611-80da-00155d010e1a")) },
                            { "gsc_productid",      new EntityReference(ProductCollection.EntityName, ProductCollection.Entities[0].Id)                 }
                        }
                    }
                }
            };
            #endregion

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

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

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

            #endregion

            #region 2. Call/Action
            var    VehicleCountScheduleHandler = new VehicleCountScheduleHandler(orgService, orgTracing);
            Entity filterResult = VehicleCountScheduleHandler.ApplyFilter(VehicleCountSchedule.Entities[0]);
            #endregion

            #region 3. Verify
            var ProductQuantityEntity  = ProductQuantityCollection.Entities[0];
            var InventorySchedulEntity = ProductCollection.Entities[0];
            Assert.AreEqual(ProductQuantityEntity.GetAttributeValue <EntityReference>("gsc_vehiclemodelid").Id, filterResult.GetAttributeValue <EntityReference>("gsc_vehiclebasemodelid").Id);
            Assert.AreEqual(ProductQuantityEntity.GetAttributeValue <EntityReference>("gsc_productid").Id, filterResult.GetAttributeValue <EntityReference>("gsc_productid").Id);
            Assert.AreEqual(ProductQuantityEntity.GetAttributeValue <EntityReference>("gsc_siteid").Id, filterResult.GetAttributeValue <EntityReference>("gsc_siteid").Id);
            Assert.AreEqual(ProductQuantityEntity.GetAttributeValue <EntityReference>("gsc_vehiclecolorid").Id, filterResult.GetAttributeValue <EntityReference>("gsc_vehiclecolorid").Id);
            Assert.AreEqual(InventorySchedulEntity.GetAttributeValue <String>("gsc_modelcode"), filterResult.GetAttributeValue <String>("gsc_modelcode"));
            Assert.AreEqual(InventorySchedulEntity.GetAttributeValue <String>("gsc_optioncode"), filterResult.GetAttributeValue <String>("gsc_optioncode"));
            #endregion
        }