/// <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 ExecutePostCommittedFirmOrderCreate(LocalPluginContext localContext) { if (localContext == null) { throw new ArgumentNullException("localContext"); } IPluginExecutionContext context = localContext.PluginExecutionContext; IOrganizationService service = localContext.OrganizationService; ITracingService trace = localContext.TracingService; Entity cfoEntity = (Entity)context.InputParameters["Target"]; if (!(context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)) { return; } if (cfoEntity.LogicalName != "gsc_sls_committedfirmorder") { return; } if (context.Mode == 0) //synchronous plugin { try { CommittedFirmOrderHandler cfoHandler = new CommittedFirmOrderHandler(service, trace); cfoHandler.SuggestCFOQuantity(cfoEntity); } catch (Exception ex) { //throw new InvalidPluginExecutionException(String.Concat("(Exception)\n", ex.Message, Environment.NewLine, ex.StackTrace, Environment.NewLine, error)); throw new InvalidPluginExecutionException(ex.Message); } } }
/// <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 }
public void NoFilter() { #region 1. Setup / Arrange var orgServiceMock = new Mock <IOrganizationService>(); var orgService = orgServiceMock.Object; var orgTracingMock = new Mock <ITracingService>(); var orgTracing = orgTracingMock.Object; #region Product Entity Collection var ProductCollection = new EntityCollection { EntityName = "product", Entities = { new Entity { Id = Guid.NewGuid(), LogicalName = "product", EntityState = EntityState.Created, Attributes = new AttributeCollection { { "gsc_sellprice", new Money(((Decimal)2500000)) }, { "gsc_modelcode", "123" }, { "gsc_optioncode", "456" } } } } }; #endregion #region Order CFO Quanity Entity Collection var CFOQuantityCollection = new EntityCollection() { EntityName = "gsc_sls_committedfirmorder", Entities = { new Entity { Id = new Guid(), LogicalName = "gsc_sls_committedfirmorder", Attributes = new AttributeCollection { { "gsc_vehiclebasemodelid", null }, { "gsc_productid", null }, { "gsc_vehiclecolorid", null }, { "gsc_dealerfilterid", null }, { "gsc_branchfilterid", null }, { "gsc_siteid", null }, } } } }; #endregion #region Order Planning Entity Collection var OrderPlanningCollection = new EntityCollection() { EntityName = "gsc_sls_orderplanning", Entities = { new Entity { Id = new Guid(), LogicalName = "gsc_sls_orderplanning", Attributes = new AttributeCollection { { "gsc_vehiclebasemodelid", new EntityReference("basemodel", new Guid("5ebda07e-0a26-e611-80d8-00155d010e2c")) { Name = "Mirage" } }, { "gsc_productid", new EntityReference(ProductCollection.EntityName, ProductCollection.Entities[0].Id) { Name = "Mirage GLX" } }, { "gsc_vehiclecolorid", new EntityReference("color", new Guid("5ebda07e-0a26-e611-80d8-00155d010e2c")) { Name = "Black" } }, { "gsc_dealerid", new EntityReference("dealer", new Guid("5ebda07e-0a26-e611-80d8-00155d010e2c")) { Name = "Citimotors" } }, { "gsc_branchid", new EntityReference("branch", new Guid("5ebda07e-0a26-e611-80d8-00155d010e2c")) { Name = "Branch 1" } }, { "gsc_siteid", new EntityReference("site", new Guid("5ebda07e-0a26-e611-80d8-00155d010e2c")) { Name = "Site 1" } }, { "gsc_orderpolicy", new OptionSetValue(100000001) } } } } }; #endregion #region Order Planning Details Entity Collection var OrderPlanningDetailCollection = new EntityCollection() { EntityName = "gsc_sls_orderplanningdetail", Entities = { new Entity { Id = new Guid(), LogicalName = "gsc_sls_orderplanningdetail", Attributes = new AttributeCollection { { "gsc_sls_orderplanningid", new EntityReference(OrderPlanningCollection.EntityName, OrderPlanningCollection.Entities[0].Id) }, { "gsc_beginninginventory", 5.00 }, { "gsc_retailaveragesales", 10.00 }, { "gsc_stockmonth", 0.5 } } } } }; #endregion orgServiceMock.Setup((service => service.RetrieveMultiple( It.Is <QueryExpression>(expression => expression.EntityName == OrderPlanningCollection.EntityName) ))).Returns(OrderPlanningCollection); 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 == OrderPlanningDetailCollection.EntityName) ))).Returns(OrderPlanningDetailCollection); #endregion #region 2. Call/Action var CFOHandler = new CommittedFirmOrderHandler(orgService, orgTracing); Entity cfoDetail = CFOHandler.SuggestCFOQuantity(CFOQuantityCollection.Entities[0]); #endregion #region 3. Verify Assert.AreEqual(OrderPlanningCollection.Entities[0].GetAttributeValue <EntityReference>("gsc_vehiclebasemodelid").Name, cfoDetail.GetAttributeValue <String>("gsc_basemodel")); Assert.AreEqual(OrderPlanningCollection.Entities[0].GetAttributeValue <EntityReference>("gsc_productid").Name, cfoDetail.GetAttributeValue <String>("gsc_modeldescription")); Assert.AreEqual(OrderPlanningCollection.Entities[0].GetAttributeValue <EntityReference>("gsc_vehiclecolorid").Name, cfoDetail.GetAttributeValue <String>("gsc_color")); Assert.AreEqual(OrderPlanningCollection.Entities[0].GetAttributeValue <EntityReference>("gsc_dealerid").Name, cfoDetail.GetAttributeValue <String>("gsc_dealer")); Assert.AreEqual(OrderPlanningCollection.Entities[0].GetAttributeValue <EntityReference>("gsc_branchid").Name, cfoDetail.GetAttributeValue <String>("gsc_branch")); Assert.AreEqual(OrderPlanningCollection.Entities[0].GetAttributeValue <EntityReference>("gsc_siteid").Name, cfoDetail.GetAttributeValue <String>("gsc_site")); Assert.AreEqual(ProductCollection.Entities[0].GetAttributeValue <String>("gsc_optioncode"), cfoDetail.GetAttributeValue <String>("gsc_optioncode")); Assert.AreEqual(ProductCollection.Entities[0].GetAttributeValue <String>("gsc_modelcode"), cfoDetail.GetAttributeValue <String>("gsc_modelcode")); Assert.AreEqual(ProductCollection.Entities[0].GetAttributeValue <Money>("gsc_sellprice"), cfoDetail.GetAttributeValue <Money>("gsc_unitprice")); Assert.AreEqual(0, cfoDetail.GetAttributeValue <Double>("gsc_cfoquantity")); Assert.AreEqual(10, cfoDetail.GetAttributeValue <Double>("gsc_suggestedcfo")); #endregion }