/// <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 ExecutePreValidateInventoryCreate(LocalPluginContext localContext) { if (localContext == null) { throw new ArgumentNullException("localContext"); } IPluginExecutionContext context = localContext.PluginExecutionContext; IOrganizationService service = localContext.OrganizationService; ITracingService trace = localContext.TracingService; Entity inventoryEntity = (Entity)context.InputParameters["Target"]; string message = context.MessageName; string error = ""; try { InventoryMovementHandler inventoryMovementHandler = new InventoryMovementHandler(service, trace); inventoryMovementHandler.UpdateInventoryFields(inventoryEntity, message); } catch (Exception ex) { throw new InvalidPluginExecutionException(String.Concat("(Exception)\n", ex.Message, Environment.NewLine, ex.StackTrace, Environment.NewLine, error)); } }
/// <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 ExecutePostInventoryUpdate(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; string message = context.MessageName; if (!(context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)) { return; } Entity quoteDiscountEntity = (Entity)context.InputParameters["Target"]; if (quoteDiscountEntity.LogicalName != "gsc_iv_inventory") { return; } if (context.Mode == 0) //Synchronous Plugin { try { var preImageProductQuantityId = preImageEntity.Contains("gsc_productquantityid") ? preImageEntity.GetAttributeValue <EntityReference>("gsc_productquantityid").Id : Guid.Empty; var postImageProductQuantityId = postImageEntity.Contains("gsc_productquantityid") ? postImageEntity.GetAttributeValue <EntityReference>("gsc_productquantityid").Id : Guid.Empty; InventoryMovementHandler inventoryMovementHandler = new InventoryMovementHandler(service, trace); if (preImageProductQuantityId != postImageProductQuantityId) { inventoryMovementHandler.UpdateInventoryFields(postImageEntity, message); } } catch (Exception ex) { //throw new InvalidPluginExecutionException(String.Concat("(Exception)\n", ex.Message, Environment.NewLine, ex.StackTrace, Environment.NewLine, error)); throw new InvalidPluginExecutionException(ex.Message); } } }
//Created By: Raphael Herrera, Created On: 8/31/2016 /*Purpose: Perform BL for receiving Vehicle In-Transit Transfer records * Registration Details: * Event/Message: * Post/Update: gsc_intransitstatus * Primary Entity: gsc_iv_vehicleintransittransferreceiving */ public void CancelTransfer(Entity vehicleTransferReceiving) { _tracingService.Trace("Started Cancel Transfer Method..."); if (vehicleTransferReceiving.GetAttributeValue <OptionSetValue>("gsc_intransitstatus").Value != 100000000) { throw new InvalidPluginExecutionException("Only records with Shipped status can be canceled."); } var inTransitTransferId = vehicleTransferReceiving.Contains("gsc_intransittransferid") ? vehicleTransferReceiving.GetAttributeValue <EntityReference>("gsc_intransittransferid").Id : Guid.Empty; //Retrieve In-Transit Transfer of In-Transit Transfer Receiving EntityCollection inTransitTransferCollection = CommonHandler.RetrieveRecordsByOneValue("gsc_iv_vehicleintransittransfer", "gsc_iv_vehicleintransittransferid", inTransitTransferId, _organizationService, null, OrderType.Ascending, new[] { "gsc_sourcesiteid", "gsc_viasiteid" }); _tracingService.Trace("In-Transit Transfer Records Retrieved: " + inTransitTransferCollection.Entities.Count); if (inTransitTransferCollection.Entities.Count > 0) { Entity inTransitTransferEntity = inTransitTransferCollection.Entities[0]; //Retrieve Allocated Vehicles of In-Transit Transfer EntityCollection transferDetailsCollection = CommonHandler.RetrieveRecordsByOneValue("gsc_iv_vehicleintransittransferdetail", "gsc_vehicleintransittransferid", inTransitTransferEntity.Id, _organizationService, null, OrderType.Ascending, new[] { "gsc_sourcesiteid", "gsc_viasiteid", "gsc_inventoryid" }); _tracingService.Trace("In Transit Transfer Detail Records Retrieved: " + transferDetailsCollection.Entities.Count); if (transferDetailsCollection.Entities.Count > 0) { foreach (Entity transferDetailsEntity in transferDetailsCollection.Entities) { var sourceSiteId = transferDetailsEntity.Contains("gsc_sourcesiteid") ? transferDetailsEntity.GetAttributeValue <EntityReference>("gsc_sourcesiteid").Id : Guid.Empty; var inventoryId = transferDetailsEntity.Contains("gsc_inventoryid") ? transferDetailsEntity.GetAttributeValue <EntityReference>("gsc_inventoryid").Id : Guid.Empty; //Retrieve Inventory of Allocated Vehicle EntityCollection inventoryCollection = CommonHandler.RetrieveRecordsByOneValue("gsc_iv_inventory", "gsc_iv_inventoryid", inventoryId, _organizationService, null, OrderType.Ascending, new[] { "gsc_status", "gsc_productquantityid" }); _tracingService.Trace("Inventory Records Retrieved: " + inventoryCollection.Entities.Count); if (inventoryCollection.Entities.Count > 0) { Entity inventoryEntity = inventoryCollection.Entities[0]; var viaProdQuantityId = inventoryEntity.Contains("gsc_productquantityid") ? inventoryEntity.GetAttributeValue <EntityReference>("gsc_productquantityid").Id : Guid.Empty; if (inventoryEntity.GetAttributeValue <OptionSetValue>("gsc_status").Value == 100000001)//status == allocated { throw new InvalidPluginExecutionException("Vehicle in this transaction is still allocated. Please remove allocation in Sales Order before cancelling."); } //Retrieve Product Quantity of Via Site EntityCollection viaQuantityCollection = CommonHandler.RetrieveRecordsByOneValue("gsc_iv_productquantity", "gsc_iv_productquantityid", viaProdQuantityId, _organizationService, null, OrderType.Ascending, new[] { "gsc_available", "gsc_onhand", "gsc_productid", "gsc_allocated" }); _tracingService.Trace("Via Site Product Quantity Records Retrieved: " + viaQuantityCollection.Entities.Count); if (viaQuantityCollection.Entities.Count > 0) { Entity viaQuantityEntity = viaQuantityCollection.Entities[0]; var productId = viaQuantityEntity.Contains("gsc_productid") ? viaQuantityEntity.GetAttributeValue <EntityReference>("gsc_productid").Id : Guid.Empty; //Retrieve Product Quantity of Source Site var sourceConditionList = new List <ConditionExpression> { new ConditionExpression("gsc_siteid", ConditionOperator.Equal, sourceSiteId), new ConditionExpression("gsc_productid", ConditionOperator.Equal, productId) }; EntityCollection sourceQuantityCollection = CommonHandler.RetrieveRecordsByConditions("gsc_iv_productquantity", sourceConditionList, _organizationService, null, OrderType.Ascending, new[] { "gsc_onhand", "gsc_available", "gsc_allocated" }); _tracingService.Trace("Source Site Product Quantity Records Retrieved: " + sourceQuantityCollection.Entities.Count); if (sourceQuantityCollection.Entities.Count > 0) { #region BL for Cancellation of Vehicle In-Transit Transfer Receiving Entity sourceQuantityEntity = sourceQuantityCollection.Entities[0]; InventoryMovementHandler inventoryHandler = new InventoryMovementHandler(_organizationService, _tracingService); // Update Inventory. Status = Available inventoryEntity["gsc_productquantityid"] = new EntityReference(sourceQuantityEntity.LogicalName, sourceQuantityEntity.Id); inventoryEntity["gsc_status"] = new OptionSetValue(100000000); inventoryHandler.UpdateInventoryFields(inventoryEntity, "Update"); _tracingService.Trace("Updated Inventory Status..."); // Update Product Quantity of Via Site inventoryHandler.UpdateProductQuantityDirectly(viaQuantityEntity, -1, -1, -1, 0, 0, 0, 0, 0);//Included value in allocated for recount _tracingService.Trace("Updated Via Site Product Quantity..."); //Update Product Quantity of Source Site inventoryHandler.UpdateProductQuantityDirectly(sourceQuantityEntity, 1, 1, 1, 0, 0, 0, 0, 0);//Included value in allocated for recount _tracingService.Trace("Updated Source Site Product Quantity..."); // Delete Allocated Vehicle record _organizationService.Delete(transferDetailsEntity.LogicalName, transferDetailsEntity.Id); _tracingService.Trace("Deleted Transfer Details..."); } else { throw new InvalidPluginExecutionException("Product Quantity of source site not found..."); } } else { throw new InvalidPluginExecutionException("Product Quantity of via site not found..."); } } else { throw new InvalidPluginExecutionException("No Inventory Records Found..."); } } // Update Vehicle In-Transit Transfer Status to Picked inTransitTransferEntity["gsc_intransittransferstatus"] = new OptionSetValue(100000000); _organizationService.Update(inTransitTransferEntity); //Clear vehicle in-transit transfer lookup field vehicleTransferReceiving["gsc_intransittransferid"] = null; _organizationService.Update(vehicleTransferReceiving); _tracingService.Trace("Updated Vehicle In-Transit Transfer status to cancelled..."); #endregion } } else { throw new InvalidPluginExecutionException("No In Transit Transfer Records Found..."); } _tracingService.Trace("Ending CancelTransfer Method..."); }
//Created By: Raphael Herrera, Created On: 8/31/2016 /*Purpose: Perform BL for receiving Vehicle In-Transit Transfer records * Registration Details: * Event/Message: * Post/Update: gsc_intransitstatus * Primary Entity: gsc_iv_vehicleintransittransferreceiving */ public void ReceiveTransfer(Entity vehicleTransferReceiving) { _tracingService.Trace("Started ReceiveTransfer Method..."); var inTransitTransferId = vehicleTransferReceiving.Contains("gsc_intransittransferid") ? vehicleTransferReceiving.GetAttributeValue <EntityReference>("gsc_intransittransferid").Id : Guid.Empty; //Retrieve Vehicle In-Transit Transfer EntityCollection inTransitCollection = CommonHandler.RetrieveRecordsByOneValue("gsc_iv_vehicleintransittransfer", "gsc_iv_vehicleintransittransferid", inTransitTransferId, _organizationService, null, OrderType.Ascending, new[] { "gsc_intransittransferstatus" }); _tracingService.Trace("Vehicle In-Transit Transfer records retrieved: " + inTransitCollection.Entities.Count); if (inTransitCollection.Entities.Count > 0) { Entity vehicleInTransit = inTransitCollection.Entities[0]; EntityCollection receivingDetailsCollection = CommonHandler.RetrieveRecordsByOneValue("gsc_iv_vehicleintransitreceivingdetail", "gsc_intransitreceivingid", vehicleTransferReceiving.Id, _organizationService, null, OrderType.Ascending, new[] { "gsc_inventoryid", "gsc_destinationsiteid" }); _tracingService.Trace("In Transit Transfer Details records retrieved: " + receivingDetailsCollection.Entities.Count); if (receivingDetailsCollection.Entities.Count > 0) { foreach (Entity receivingDetailsEntity in receivingDetailsCollection.Entities) { var inventoryId = receivingDetailsEntity.Contains("gsc_inventoryid") ? receivingDetailsEntity.GetAttributeValue <EntityReference>("gsc_inventoryid").Id : Guid.Empty; //Retrieve inventory EntityCollection inventoryCollection = CommonHandler.RetrieveRecordsByOneValue("gsc_iv_inventory", "gsc_iv_inventoryid", inventoryId, _organizationService, null, OrderType.Ascending, new[] { "gsc_status", "gsc_productquantityid", "gsc_color", "gsc_productid" }); _tracingService.Trace("Inventory records retrieved: " + inventoryCollection.Entities.Count); if (inventoryCollection.Entities.Count > 0) { Entity inventory = inventoryCollection.Entities[0]; var productQuantityId = inventory.Contains("gsc_productquantityid") ? inventory.GetAttributeValue <EntityReference>("gsc_productquantityid").Id : Guid.Empty; //Retrieve product quantity of Via Site EntityCollection viaQuantityCollection = CommonHandler.RetrieveRecordsByOneValue("gsc_iv_productquantity", "gsc_iv_productquantityid", productQuantityId, _organizationService, null, OrderType.Ascending, new[] { "gsc_available", "gsc_onhand", "gsc_allocated", "gsc_productid", "gsc_vehiclemodelid", "gsc_siteid", "gsc_vehiclecolorid" }); _tracingService.Trace("Via Site ProductQuantity records retrieved: " + viaQuantityCollection.Entities.Count); if (viaQuantityCollection.Entities.Count > 0) { Entity viaProdQuantity = viaQuantityCollection.Entities[0]; //Retrieve Product Quantity of Destination Site var destinationSite = vehicleTransferReceiving.Contains("gsc_destinationsiteid") ? vehicleTransferReceiving.GetAttributeValue <EntityReference>("gsc_destinationsiteid").Id : Guid.Empty; var productId = viaProdQuantity.Contains("gsc_productid") ? viaProdQuantity.GetAttributeValue <EntityReference>("gsc_productid").Id : Guid.Empty; InventoryMovementHandler inventoryHandler = new InventoryMovementHandler(_organizationService, _tracingService); Entity vehicleColor = inventoryHandler.GetVehicleColorReference(inventory); var destinationConditionList = new List <ConditionExpression> { new ConditionExpression("gsc_siteid", ConditionOperator.Equal, destinationSite), new ConditionExpression("gsc_productid", ConditionOperator.Equal, productId), new ConditionExpression("gsc_vehiclecolorid", ConditionOperator.Equal, vehicleColor.Id) }; EntityCollection destinationQuantityCollection = CommonHandler.RetrieveRecordsByConditions("gsc_iv_productquantity", destinationConditionList, _organizationService, null, OrderType.Ascending, new[] { "gsc_onhand", "gsc_available", "gsc_allocated", "gsc_vehiclemodelid", "gsc_productid", "gsc_vehiclecolorid" }); Entity destinationQuantityEntity = new Entity("gsc_iv_productquantity"); _tracingService.Trace("Destination ProductQuantity records retrieved: " + destinationQuantityCollection.Entities.Count); if (destinationQuantityCollection.Entities.Count == 0) { Guid destinationQuantityId = inventoryHandler.CreateProductQuantity(receivingDetailsEntity.GetAttributeValue <EntityReference>("gsc_destinationsiteid"), productId, new EntityReference("gsc_cmn_vehiclecolor", vehicleColor.Id)); EntityCollection destinationCollection = CommonHandler.RetrieveRecordsByOneValue("gsc_iv_productquantity", "gsc_iv_productquantityid", destinationQuantityId, _organizationService, null, OrderType.Ascending, new[] { "gsc_onhand", "gsc_available", "gsc_productid", "gsc_vehiclecolorid", "gsc_vehiclemodelid" }); destinationQuantityEntity = destinationCollection.Entities[0]; _tracingService.Trace("Created Product Quantity..."); } else { destinationQuantityEntity = destinationQuantityCollection.Entities[0]; } #region BL for Receiving Vehicle In-Transit Transfer Record Int32 viaAvailable = viaProdQuantity.GetAttributeValue <Int32>("gsc_available"); Int32 viaOnHand = viaProdQuantity.GetAttributeValue <Int32>("gsc_onhand"); Int32 destinationAvailable = destinationQuantityEntity.GetAttributeValue <Int32>("gsc_available"); Int32 destinationOnHand = destinationQuantityEntity.GetAttributeValue <Int32>("gsc_onhand"); //Update Inventory Product Quantity inventory["gsc_productquantityid"] = new EntityReference(destinationQuantityEntity.LogicalName, destinationQuantityEntity.Id); inventoryHandler.UpdateInventoryFields(inventory, "Update"); _tracingService.Trace("Updated Inventory Status..."); //Update Product Quantity of Via Site inventoryHandler.UpdateProductQuantityDirectly(viaProdQuantity, -1, -1, -1, 0, 0, 0, 0, 0);//Included value in allocated for recount _tracingService.Trace("Updated Via Site Product Quantity..."); Entity branchEntity = GetBranchEntity(vehicleTransferReceiving); Guid viaSiteId = viaProdQuantity.GetAttributeValue <EntityReference>("gsc_siteid").Id; inventoryHandler.CreateInventoryHistory("Vehicle In Transit Transfer Receiving", branchEntity.GetAttributeValue <string>("accountnumber"), branchEntity.GetAttributeValue <string>("name"), vehicleTransferReceiving.GetAttributeValue <string>("gsc_vehicleintransittransferreceivingpn"), DateTime.UtcNow, 1, 1, viaProdQuantity.GetAttributeValue <Int32>("gsc_onhand"), destinationSite, viaSiteId, viaSiteId, inventory, viaProdQuantity, true, false); _tracingService.Trace("Created inventory history log..."); //Update Product Quantity of Destination Site inventoryHandler.UpdateProductQuantityDirectly(destinationQuantityEntity, 1, 1, 1, 0, 0, 0, 0, 0);//Included value in allocated for recount _tracingService.Trace("Updated Destination Site Product Quantity..."); inventoryHandler.CreateInventoryHistory("Vehicle In Transit Transfer Receiving", branchEntity.GetAttributeValue <string>("accountnumber"), branchEntity.GetAttributeValue <string>("name"), vehicleTransferReceiving.GetAttributeValue <string>("gsc_vehicleintransittransferreceivingpn"), DateTime.UtcNow, 1, 1, destinationQuantityEntity.GetAttributeValue <Int32>("gsc_onhand"), destinationSite, viaSiteId, destinationSite, inventory, destinationQuantityEntity, true, true); _tracingService.Trace("Created inventory history log..."); //Update Vehicle In-Transit Transfer. Status = Received vehicleInTransit["gsc_intransittransferstatus"] = new OptionSetValue(100000002); _organizationService.Update(vehicleInTransit); _tracingService.Trace("Updated Vehicle In-Transit Transfer..."); #endregion } else { throw new InvalidPluginExecutionException("No Via Site Found..."); } } else { throw new InvalidPluginExecutionException("No Inventory Record Found..."); } } } else { throw new InvalidPluginExecutionException("No Receiving Details Found..."); } } _tracingService.Trace("Ending ReceiveTransfer Method..."); }