/// <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 ExecutePreValidateVehicleAdjustmentVarianceEntryDelete(LocalPluginContext localContext) { if (localContext == null) { throw new ArgumentNullException("localContext"); } IPluginExecutionContext context = localContext.PluginExecutionContext; IOrganizationService service = localContext.OrganizationService; ITracingService trace = localContext.TracingService; EntityReference vehicleAdjusmentVarianceEntryEntity = (EntityReference)context.InputParameters["Target"]; string message = context.MessageName; if (context.Depth > 1) { return; } try { EntityCollection vehicleAdjustmentVarianceEntryRecords = CommonHandler.RetrieveRecordsByOneValue("gsc_sls_vehicleadjustmentvarianceentry", "gsc_sls_vehicleadjustmentvarianceentryid", vehicleAdjusmentVarianceEntryEntity.Id, service, null, OrderType.Ascending, new[] { "gsc_adjustmentvariancestatus" }); VehicleAdjustmentVarianceEntryHandler vehicleAdjustmentVarianceEntryHandler = new VehicleAdjustmentVarianceEntryHandler(service, trace); vehicleAdjustmentVarianceEntryHandler.AdjustInventoryOnUnpostedDelete(vehicleAdjustmentVarianceEntryRecords.Entities[0]); } catch (Exception ex) { if (ex.Message.Contains("Unable to delete already posted Vehicle Adjustment/Variance Entry")) { throw new InvalidPluginExecutionException("Unable to delete already posted Vehicle Adjustment/Variance Entry"); } else { throw new InvalidPluginExecutionException(String.Concat("(Exception)\n", ex.Message, Environment.NewLine, ex.StackTrace)); } } }
/// <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 ExecutePostVehicleAdjustmentVarianceEntryUpdate(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 vehicleAdjustmentVarianceEntry = (Entity)context.InputParameters["Target"]; if (!(context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)) { return; } if (vehicleAdjustmentVarianceEntry.LogicalName != "gsc_sls_vehicleadjustmentvarianceentry") { return; } if (context.Mode == 0) //Synchronous Plug-in { string message = context.MessageName; try { #region Pre-images String preImageInventoryIdToAllocate = preImageEntity.GetAttributeValue <String>("gsc_inventoryidtoallocate") != null ? preImageEntity.GetAttributeValue <String>("gsc_inventoryidtoallocate") : String.Empty; var preImageAdjustmentIdToRemove = preImageEntity.Contains("gsc_adjustmentidtounallocate") ? preImageEntity.GetAttributeValue <string>("gsc_adjustmentidtounallocate") : String.Empty; var preImageAdjustmentStatus = preImageEntity.Contains("gsc_adjustmentvariancestatus") ? preImageEntity.GetAttributeValue <OptionSetValue>("gsc_adjustmentvariancestatus").Value : 0; #endregion #region Post-images String postImageInventoryIdToAllocate = postImageEntity.GetAttributeValue <String>("gsc_inventoryidtoallocate") != null ? postImageEntity.GetAttributeValue <String>("gsc_inventoryidtoallocate") : String.Empty; var postImageAdjustIdToRemove = postImageEntity.Contains("gsc_adjustmentidtounallocate") ? postImageEntity.GetAttributeValue <string>("gsc_adjustmentidtounallocate") : String.Empty; var postImageAdjustmentStatus = postImageEntity.Contains("gsc_adjustmentvariancestatus") ? postImageEntity.GetAttributeValue <OptionSetValue>("gsc_adjustmentvariancestatus").Value : 0; #endregion VehicleAdjustmentVarianceEntryHandler vehicleAdjustmentVarianceEntryHandler = new VehicleAdjustmentVarianceEntryHandler(service, trace); //Call function on Inventory Id To Allocate field value change if (preImageInventoryIdToAllocate != postImageInventoryIdToAllocate && !String.IsNullOrEmpty(postImageInventoryIdToAllocate)) { vehicleAdjustmentVarianceEntryHandler.CreateVehicleAdjustmentVarianceEntryDetailRecord(postImageEntity); } //Call function on Adjustment Variance Status change if (preImageAdjustmentStatus != postImageAdjustmentStatus) { //if posted if (postImageAdjustmentStatus == 100000001) { if (vehicleAdjustmentVarianceEntryHandler.RestrictPosting(postImageEntity) == true) { throw new InvalidPluginExecutionException("Cannot proceed with posting, one of the adjusted vehicle is in-transit site."); } } //Call function when record was cancelled if (postImageAdjustmentStatus == 100000002) { vehicleAdjustmentVarianceEntryHandler.CancelAdjustedVehicle(postImageEntity); } vehicleAdjustmentVarianceEntryHandler.PostVehicleAdjustmentVarianceEntry(postImageEntity); vehicleAdjustmentVarianceEntryHandler.UpdateStatus(postImageEntity); } //Call function on Adjustment Id to Unallocate field change if (preImageAdjustmentIdToRemove != postImageAdjustIdToRemove && postImageAdjustIdToRemove != String.Empty && postImageAdjustIdToRemove != "") { vehicleAdjustmentVarianceEntryHandler.RemoveAdjustedVehicle(postImageEntity); } } catch (Exception ex) { if (ex.Message.Contains("The inventory for entered vehicle is not available.")) { throw new InvalidPluginExecutionException("The inventory for entered vehicle is not available."); } else if (ex.Message.Contains("Cannot proceed with posting, one of the adjusted vehicle is in-transit site.")) { throw new InvalidPluginExecutionException("Cannot proceed with posting, one of the adjusted vehicle is in-transit site."); } else { //throw new InvalidPluginExecutionException(String.Concat("(Exception)\n", ex.Message, Environment.NewLine, ex.StackTrace, Environment.NewLine, error)); throw new InvalidPluginExecutionException(ex.Message); } } } }