/// <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 ExecutePostReceivingTransactionUpdate(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 receivingTransactionEntity = (Entity)context.InputParameters["Target"]; if (!(context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)) { return; } if (receivingTransactionEntity.LogicalName != "gsc_cmn_receivingtransaction") { return; } if (context.Mode == 0) //Synchronous Plug-in { string message = context.MessageName; try { #region Pre-images var preImageReceivingStatus = preImageEntity.Contains("gsc_receivingstatus") ? preImageEntity.GetAttributeValue<OptionSetValue>("gsc_receivingstatus").Value : 0; var preImageWBNo = preImageEntity.Contains("gsc_warrantybookletno") ? preImageEntity.GetAttributeValue<String>("gsc_warrantybookletno") : String.Empty; var preImagInvoiceNo = preImageEntity.Contains("gsc_invoiceno") ? preImageEntity.GetAttributeValue<String>("gsc_invoiceno") : String.Empty; #endregion #region Post-images var postImageReceivingStatus = postImageEntity.Contains("gsc_receivingstatus") ? postImageEntity.GetAttributeValue<OptionSetValue>("gsc_receivingstatus").Value : 0; var postImageWBNo = postImageEntity.Contains("gsc_warrantybookletno") ? postImageEntity.GetAttributeValue<String>("gsc_warrantybookletno") : String.Empty; var postImagInvoiceNo = postImageEntity.Contains("gsc_invoiceno") ? postImageEntity.GetAttributeValue<String>("gsc_invoiceno") : String.Empty; #endregion ReceivingTransactionHandler receivingTransactionHandler = new ReceivingTransactionHandler(service, trace); if (preImageWBNo != postImageWBNo) { receivingTransactionHandler.IsWBNoUnique(postImageEntity); receivingTransactionHandler.UpdateInventoryWBNo(postImageEntity); } if (preImagInvoiceNo != postImagInvoiceNo) { receivingTransactionHandler.ValidateInvoiceNo(postImageEntity); } if (preImageReceivingStatus != postImageReceivingStatus) { receivingTransactionHandler.UpdatePOItem(postImageEntity); receivingTransactionHandler.UpdateVPOSatus(postImageEntity); receivingTransactionHandler.SubtractinUnservedPO(postImageEntity); receivingTransactionHandler.InventoryMovementUponReceiving(postImageEntity); if (postImageReceivingStatus == 100000000) { if (receivingTransactionHandler.IsAllocatedVehicle(postImageEntity) == true) { postImageEntity["gsc_receivingstatus"] = new OptionSetValue(100000003); throw new InvalidPluginExecutionException("Vehicle is currently allocated, remove the allocation to cancel the record."); } receivingTransactionHandler.CancelReceivingTransaction(postImageEntity); } if (postImageReceivingStatus == 100000003 || postImageReceivingStatus == 100000004) { receivingTransactionHandler.UpdateVPOSatusinVPO(postImageEntity); } } } catch (Exception ex) { //if (ex.Message.Contains("Vehicle is currently allocated, remove the allocation to cancel the record.")) throw new InvalidPluginExecutionException(ex.Message); // else // throw new InvalidPluginExecutionException(String.Concat("(Exception)\n", ex.Message, Environment.NewLine, ex.StackTrace)); } } }
public void CancelReceivingTransaction() { #region 1. Setup / Arrange var orgServiceMock = new Mock <IOrganizationService>(); var orgService = orgServiceMock.Object; var orgTracingMock = new Mock <ITracingService>(); var orgTracing = orgTracingMock.Object; #region Purchase Order EntityCollection var PurchaseOrderCollection = new EntityCollection { EntityName = "gsc_cmn_purchaseorder", Entities = { new Entity { Id = Guid.NewGuid(), LogicalName = "gsc_cmn_purchaseorder", EntityState = EntityState.Created, Attributes = new AttributeCollection { { "gsc_vpostatus", new OptionSetValue(100000002) } } } } }; #endregion #region Receiving Transaction EntityCollection var ReceivingTransactionCollection = new EntityCollection { EntityName = "gsc_cmn_receivingtransaction", Entities = { new Entity { Id = Guid.NewGuid(), LogicalName = "gsc_cmn_receivingtransaction", EntityState = EntityState.Created, Attributes = new AttributeCollection { { "gsc_mmpcstatus", "Inactive" }, { "gsc_vpostatus", "In-Transit" }, { "gsc_purchaseorderid", PurchaseOrderCollection.Entities[0].Id } } } } }; #endregion #region Product Quantity EntityCollection var ProductQuantityCollection = new EntityCollection { EntityName = "gsc_iv_productquantity", Entities = { new Entity { Id = Guid.NewGuid(), LogicalName = "gsc_iv_productquantiy", EntityState = EntityState.Created, Attributes = new AttributeCollection { { "gsc_onhand", (Int32)2 }, { "gsc_available", (Int32)2 }, { "gsc_onorder", (Int32)11 } } } } }; #endregion #region Inventory EntityCollection var InventoryCollection = new EntityCollection { EntityName = "gsc_iv_inventory", Entities = { new Entity { Id = Guid.NewGuid(), LogicalName = "gsc_iv_inventory", EntityState = EntityState.Created, Attributes = new AttributeCollection { { "gsc_productquantityid", Guid.NewGuid() } } } } }; #endregion #region Receiving Transaction Detail EntityCollection var ReceivingTransactionDetailCollection = new EntityCollection { EntityName = "gsc_cmn_receivingtransactiondetail", Entities = { new Entity { Id = Guid.NewGuid(), LogicalName = "gsc_cmn_receivingtransactiondetail", EntityState = EntityState.Created, Attributes = new AttributeCollection { { "gsc_receivingtransactionid", ReceivingTransactionCollection.Entities[0].Id }, { "gsc_inventoryid", InventoryCollection.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 == ReceivingTransactionCollection.EntityName) ))).Returns(ReceivingTransactionCollection); orgServiceMock.Setup((service => service.RetrieveMultiple( It.Is <QueryExpression>(expression => expression.EntityName == InventoryCollection.EntityName) ))).Returns(InventoryCollection); orgServiceMock.Setup((service => service.RetrieveMultiple( It.Is <QueryExpression>(expression => expression.EntityName == ReceivingTransactionDetailCollection.EntityName) ))).Returns(ReceivingTransactionDetailCollection); #endregion #region 2. Call / Action var ReceivingTransactionHandler = new ReceivingTransactionHandler(orgService, orgTracing); Entity receivingTransaction = ReceivingTransactionHandler.CancelReceivingTransaction(ReceivingTransactionCollection.Entities[0]); #endregion #region 3. Verify Assert.AreEqual("", ""); #endregion }