/// <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 ExecutePostCommittedFirmOrderUpdate(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 cfoEntity = (Entity)context.InputParameters["Target"]; if (cfoEntity.LogicalName != "gsc_sls_committedfirmorder") { return; } if (context.Mode == 0) //Synchronous Plugin { try { CommittedFirmOrderHandler cfoHandler = new CommittedFirmOrderHandler(service, trace); var preBaseModelId = preImageEntity.GetAttributeValue <EntityReference>("gsc_vehiclebasemodelid") != null ? preImageEntity.GetAttributeValue <EntityReference>("gsc_vehiclebasemodelid").Id : Guid.Empty; var preProdId = preImageEntity.GetAttributeValue <EntityReference>("gsc_productid") != null ? preImageEntity.GetAttributeValue <EntityReference>("gsc_productid").Id : Guid.Empty; var preColorId = preImageEntity.GetAttributeValue <EntityReference>("gsc_vehiclecolorid") != null ? preImageEntity.GetAttributeValue <EntityReference>("gsc_vehiclecolorid").Id : Guid.Empty; var preDealerId = preImageEntity.GetAttributeValue <EntityReference>("gsc_dealerfilterid") != null ? preImageEntity.GetAttributeValue <EntityReference>("gsc_dealerfilterid").Id : Guid.Empty; var preBranchId = preImageEntity.GetAttributeValue <EntityReference>("gsc_branchfilterid") != null ? preImageEntity.GetAttributeValue <EntityReference>("gsc_branchfilterid").Id : Guid.Empty; var preSiteId = preImageEntity.GetAttributeValue <EntityReference>("gsc_siteid") != null ? preImageEntity.GetAttributeValue <EntityReference>("gsc_siteid").Id : Guid.Empty; var preGenerate = preImageEntity.GetAttributeValue <Boolean>("gsc_generatecfoquantity"); var postBaseModelId = postImageEntity.GetAttributeValue <EntityReference>("gsc_vehiclebasemodelid") != null ? postImageEntity.GetAttributeValue <EntityReference>("gsc_vehiclebasemodelid").Id : Guid.Empty; var postProdId = postImageEntity.GetAttributeValue <EntityReference>("gsc_productid") != null ? postImageEntity.GetAttributeValue <EntityReference>("gsc_productid").Id : Guid.Empty; var postColorId = postImageEntity.GetAttributeValue <EntityReference>("gsc_vehiclecolorid") != null ? postImageEntity.GetAttributeValue <EntityReference>("gsc_vehiclecolorid").Id : Guid.Empty; var postDealerId = postImageEntity.GetAttributeValue <EntityReference>("gsc_dealerfilterid") != null ? postImageEntity.GetAttributeValue <EntityReference>("gsc_dealerfilterid").Id : Guid.Empty; var postBranchId = postImageEntity.GetAttributeValue <EntityReference>("gsc_branchfilterid") != null ? postImageEntity.GetAttributeValue <EntityReference>("gsc_branchfilterid").Id : Guid.Empty; var postSiteId = postImageEntity.GetAttributeValue <EntityReference>("gsc_siteid") != null ? postImageEntity.GetAttributeValue <EntityReference>("gsc_siteid").Id : Guid.Empty; var postGenerate = postImageEntity.GetAttributeValue <Boolean>("gsc_generatecfoquantity"); if (preBaseModelId != postBaseModelId || preProdId != postProdId || preColorId != postColorId) { cfoHandler.DeleteSuggestedCFODetails(postImageEntity); cfoHandler.SuggestCFOQuantity(postImageEntity); } if (preGenerate != postGenerate) { cfoHandler.GenerateCFOQuantity(postImageEntity); } } catch (Exception ex) { throw new InvalidPluginExecutionException(ex.Message); } } }
public void GenerateCFOQuantity_Null() { #region 1. Setup / Arrange var orgServiceMock = new Mock <IOrganizationService>(); var orgService = orgServiceMock.Object; var orgTracingMock = new Mock <ITracingService>(); var orgTracing = orgTracingMock.Object; #region Order Planning Entity Collection var OrderPlanningCollection = new EntityCollection() { EntityName = "gsc_sls_orderplanning", Entities = { new Entity { Id = Guid.NewGuid(), LogicalName = "gsc_sls_orderplanning", Attributes = { { "gsc_ordercycle", new OptionSetValue(100000002) } }, FormattedValues = { { "gsc_ordercycle", "3" } } }, new Entity { Id = Guid.NewGuid(), LogicalName = "gsc_sls_orderplanning", Attributes = { { "gsc_ordercycle", new OptionSetValue(100000002) } }, FormattedValues = { { "gsc_ordercycle", "3" } } } } }; #endregion #region Suggested CFO Entity Collection var SuggestedCFOCollection = new EntityCollection() { EntityName = "gsc_sls_committedfirmorder", Entities = { new Entity { Id = new Guid(), LogicalName = "gsc_sls_committedfirmorder", Attributes = new AttributeCollection { { "gsc_generatecfoquantity", false }, } } } }; #endregion #region Suggested CFO Details Entity Collection var SuggestedCFODetailsCollection = new EntityCollection() { EntityName = "gsc_sls_committedfirmorderdetail", Entities = { new Entity { Id = new Guid(), LogicalName = "gsc_sls_committedfirmorderdetail", Attributes = new AttributeCollection { { "gsc_committedfirmorderid", new EntityReference(SuggestedCFOCollection.EntityName, SuggestedCFOCollection.Entities[0].Id) }, { "gsc_orderplanningid", new EntityReference(OrderPlanningCollection.EntityName, OrderPlanningCollection.Entities[0].Id) }, { "gsc_vehiclebasemodelid", new EntityReference("basemodel", new Guid("5ebda07e-0a26-e611-80d8-00155d010e2c")) { Name = "Mirage" } }, { "gsc_productid", new EntityReference("product", new Guid("5ebda07e-0a26-e611-80d8-00155d010e2c")) { Name = "Montero" } }, { "gsc_modelcode", "Model Code" }, { "gsc_optioncode", "Option Code" }, { "gsc_unitcost", new Money(180000) }, { "gsc_cfoquantity", 10 }, { "gsc_vehiclecolorid", new EntityReference("color", new Guid("5ebda07e-0a26-e611-80d8-00155d010e2c")) { Name = "Black" } }, { "gsc_dealer", "Citimotors" }, { "gsc_branch", "Branch 1" }, { "gsc_siteid", new EntityReference("site", new Guid("5ebda07e-0a26-e611-80d8-00155d010e2c")) { Name = "Site 1" } }, { "gsc_remakrs", "Remarks" }, { "gsc_statusreason", 100000000 }, } }, new Entity { Id = new Guid(), LogicalName = "gsc_sls_committedfirmorderdetail", Attributes = new AttributeCollection { { "gsc_committedfirmorderid", new EntityReference(SuggestedCFOCollection.EntityName, SuggestedCFOCollection.Entities[0].Id) }, { "gsc_orderplanningid", new EntityReference(OrderPlanningCollection.EntityName, OrderPlanningCollection.Entities[1].Id) }, { "gsc_vehiclebasemodelid", new EntityReference("basemodel", new Guid("5ebda07e-0a26-e611-80d8-00155d010e2c")) { Name = "Mirage" } }, { "gsc_productid", new EntityReference("product", new Guid("5ebda07e-0a26-e611-80d8-00155d010e2c")) { Name = "Montero" } }, { "gsc_modelcode", "Model Code" }, { "gsc_optioncode", "Option Code" }, { "gsc_unitcost", new Money(250000) }, { "gsc_cfoquantity", 10 }, { "gsc_vehiclecolorid", new EntityReference("color", new Guid("5ebda07e-0a26-e611-80d8-00155d010e2c")) { Name = "Black" } }, { "gsc_dealer", "Citimotors" }, { "gsc_branch", "Branch 1" }, { "gsc_siteid", new EntityReference("site", new Guid("5ebda07e-0a26-e611-80d8-00155d010e2c")) { Name = "Site 1" } }, { "gsc_remakrs", "Remarks" }, { "gsc_statusreason", 100000000 }, } } } }; #endregion orgServiceMock.Setup((service => service.RetrieveMultiple( It.Is <QueryExpression>(expression => expression.EntityName == SuggestedCFODetailsCollection.EntityName) ))).Returns(SuggestedCFODetailsCollection); orgServiceMock.Setup((service => service.RetrieveMultiple( It.Is <QueryExpression>(expression => expression.EntityName == OrderPlanningCollection.EntityName) ))).Returns(OrderPlanningCollection); #endregion #region 2. Call/Action var CFOHandler = new CommittedFirmOrderHandler(orgService, orgTracing); Entity cfoQuantityDetails = CFOHandler.GenerateCFOQuantity(SuggestedCFOCollection.Entities[0]); #endregion #region 3. Verify Assert.AreEqual(null, cfoQuantityDetails); #endregion }