public void VerifiedGreaterThanCounted() { #region 1. Setup / Arrange var orgServiceMock = new Mock <IOrganizationService>(); var orgService = orgServiceMock.Object; var orgTracingMock = new Mock <ITracingService>(); var orgTracing = orgTracingMock.Object; #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_countedqty", 1 }, { "gsc_verified", false } } } } }; #endregion #region Vehicle Count Breakdown EntityCollection var VehicleCounBreakdown = new EntityCollection { EntityName = "gsc_iv_vehiclecountbreakdown", Entities = { new Entity { Id = Guid.NewGuid(), LogicalName = "gsc_iv_vehiclecountbreakdown", EntityState = EntityState.Created, Attributes = new AttributeCollection { { "gsc_vehiclecountentrydetailid", new EntityReference(VehicleCountEntryDetail.EntityName, VehicleCountEntryDetail.Entities[0].Id) }, { "gsc_verified", true } } }, new Entity { Id = Guid.NewGuid(), LogicalName = "gsc_iv_vehiclecountbreakdown", EntityState = EntityState.Created, Attributes = new AttributeCollection { { "gsc_vehiclecountentrydetailid", new EntityReference(VehicleCountEntryDetail.EntityName, VehicleCountEntryDetail.Entities[0].Id) }, { "gsc_verified", true } } } } }; #endregion orgServiceMock.Setup((service => service.RetrieveMultiple( It.Is <QueryExpression>(expression => expression.EntityName == VehicleCounBreakdown.EntityName) ))).Returns(VehicleCounBreakdown); 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); #endregion #region 2. Call/Action var VehicleCountEntryDetailHandler = new VehicleCountEntryDetailHandler(orgService, orgTracing); VehicleCountEntryDetailHandler.ComputeVerifiedCountedQty(VehicleCountEntryDetail.Entities[0]); #endregion #region 3. Verify Assert.AreEqual(true, VehicleCountEntryDetail.Entities[0].GetAttributeValue <Boolean>("gsc_verified")); #endregion }
/// <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)); } } }