/// <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 ExecutePostOrderPlanningUpdate(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 orderPlanningeEntity = (Entity)context.InputParameters["Target"]; if (orderPlanningeEntity.LogicalName != "gsc_sls_orderplanning") { return; } if (context.Mode == 0) //Synchronous Plugin { try { OrderPlanningHandler orderPlanningHandler = new OrderPlanningHandler(service, trace); var preCreateDetail = preImageEntity.GetAttributeValue <Boolean>("gsc_createdetailforcurrentmonth"); var postCreateDetail = postImageEntity.GetAttributeValue <Boolean>("gsc_createdetailforcurrentmonth"); if (preCreateDetail != postCreateDetail) { orderPlanningHandler.CreateDetailsForThisMonth(postImageEntity); } } catch (Exception ex) { throw new InvalidPluginExecutionException(ex.Message); } } }
public void GeneratePrimaryName() { #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 = new Guid(), LogicalName = "gsc_sls_orderplanning", Attributes = new AttributeCollection { { "gsc_productid", new EntityReference("product", new Guid("5ebda07e-0a26-e611-80d8-00155d010e2c")) { Name = "Montero" } }, { "gsc_siteid", new EntityReference("site", new Guid("5ebda07e-0a26-e611-80d8-00155d010e2c")) { Name = "Evolander" } }, { "gsc_orderplanningpn", "" } } } } }; #endregion #endregion #region 2. Call/Action var OrderPlanningHandler = new OrderPlanningHandler(orgService, orgTracing); Entity CreatedOrderPlanning = OrderPlanningHandler.GeneratePrimaryName(OrderPlanningCollection.Entities[0]); #endregion #region 3. Verify Assert.AreEqual("Montero-Evolander", CreatedOrderPlanning.GetAttributeValue <String>("gsc_orderplanningpn")); #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 ExecutePostOrderPlanningCreate(LocalPluginContext localContext) { if (localContext == null) { throw new ArgumentNullException("localContext"); } IPluginExecutionContext context = localContext.PluginExecutionContext; IOrganizationService service = localContext.OrganizationService; ITracingService trace = localContext.TracingService; Entity orderPlanningEntity = (Entity)context.InputParameters["Target"]; if (!(context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)) { return; } if (orderPlanningEntity.LogicalName != "gsc_sls_orderplanning") { return; } if (context.Mode == 0) //synchronous plugin { try { OrderPlanningHandler orderPlanningHandler = new OrderPlanningHandler(service, trace); orderPlanningHandler.GeneratePrimaryName(orderPlanningEntity); } catch (Exception ex) { //throw new InvalidPluginExecutionException(String.Concat("(Exception)\n", ex.Message, Environment.NewLine, ex.StackTrace, Environment.NewLine, error)); throw new InvalidPluginExecutionException(ex.Message); } } }
public void CreateDetails() { #region 1. Setup / Arrange var orgServiceMock = new Mock <IOrganizationService>(); var orgService = orgServiceMock.Object; var orgTracingMock = new Mock <ITracingService>(); var orgTracing = orgTracingMock.Object; #region Invoice Entity Collection var InvoiceCollection = new EntityCollection() { EntityName = "invoice", Entities = { new Entity { Id = new Guid(), LogicalName = "invoice", Attributes = new AttributeCollection { { "gsc_salesinvoicestatus", new OptionSetValue(100000004) }, { "gsc_productid", new EntityReference("product", new Guid("5ebda07e-0a26-e611-80d8-00155d010e2c"))} } }, new Entity { Id = new Guid(), LogicalName = "invoice", Attributes = new AttributeCollection { { "gsc_salesinvoicestatus", new OptionSetValue(100000004) }, { "gsc_productid", new EntityReference("product", new Guid("5ebda07e-0a26-e611-80d8-00155d010e2c"))} } } } }; #endregion #region Sales Return Entity Collection var ReturnCollection = new EntityCollection() { EntityName = "gsc_sls_vehiclesalesreturn", Entities = { new Entity { Id = new Guid(), LogicalName = "gsc_sls_vehiclesalesreturn", Attributes = new AttributeCollection { { "gsc_vehiclesalesreturnstatus", new OptionSetValue(100000002) }, { "gsc_productid", new EntityReference("product", new Guid("5ebda07e-0a26-e611-80d8-00155d010e2c"))}, { "gsc_invoiceid", new EntityReference(InvoiceCollection.EntityName, InvoiceCollection.Entities[0].Id)} } } } }; #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_productid", new EntityReference("product", new Guid("5ebda07e-0a26-e611-80d8-00155d010e2c")) }, { "gsc_siteid", new EntityReference("site", new Guid("5ebda07e-0a26-e611-80d8-00155d010e2c")) }, { "gsc_createdetailforcurrentmonth", true }, { "gsc_retailperiodcoverage", 2 } }, FormattedValues = { { "gsc_retailperiodcoverage", "2" } } } } }; #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_year", 2016 }, { "gsc_month", 05 }, { "gsc_endinginventory", 10.00 }, } } } }; #endregion orgServiceMock.Setup((service => service.RetrieveMultiple( It.Is <QueryExpression>(expression => expression.EntityName == InvoiceCollection.EntityName) ))).Returns(InvoiceCollection); orgServiceMock.Setup((service => service.RetrieveMultiple( It.Is <QueryExpression>(expression => expression.EntityName == ReturnCollection.EntityName) ))).Returns(ReturnCollection); 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 == OrderPlanningDetailCollection.EntityName) ))).Returns(OrderPlanningDetailCollection); #endregion #region 2. Call/Action var OrderPlanningHandler = new OrderPlanningHandler(orgService, orgTracing); Entity Detail = OrderPlanningHandler.CreateDetailsForThisMonth(OrderPlanningCollection.Entities[0]); #endregion #region 3. Verify Assert.AreEqual(OrderPlanningCollection.Entities[0].Id, Detail.GetAttributeValue <EntityReference>("gsc_orderplanningid").Id); Assert.AreEqual("2016", Detail.GetAttributeValue <String>("gsc_year")); Assert.AreEqual("06", Detail.GetAttributeValue <String>("gsc_month")); Assert.AreEqual(0.0, Detail.GetAttributeValue <Double>("gsc_retailaveragesales")); Assert.AreEqual(OrderPlanningDetailCollection.Entities[0].GetAttributeValue <Double>("gsc_endinginventory"), Detail.GetAttributeValue <Double>("gsc_beginninginventory")); #endregion }