/// <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 ExecutePreVehicleAdjustmentVarianceEntryDetailCreate(LocalPluginContext localContext) { if (localContext == null) { throw new ArgumentNullException("localContext"); } IPluginExecutionContext context = localContext.PluginExecutionContext; IOrganizationService service = localContext.OrganizationService; ITracingService trace = localContext.TracingService; Entity vehicleAdjustmentVarianceEntryDetailEntity = (Entity)context.InputParameters["Target"]; string message = context.MessageName; string error = ""; try { VehicleAdjustmentVarianceEntryDetailHandler vehicleAdjustmentVarianceEntryDetailHandler = new VehicleAdjustmentVarianceEntryDetailHandler(service, trace); vehicleAdjustmentVarianceEntryDetailHandler.ValidateImportRecord(vehicleAdjustmentVarianceEntryDetailEntity); vehicleAdjustmentVarianceEntryDetailHandler.CheckExistingInventoryRecord(vehicleAdjustmentVarianceEntryDetailEntity, message); } catch (Exception ex) { if (ex.Message.Contains("Please insert product information in Vehicle and Item Catalog")) { throw new InvalidPluginExecutionException(ex.Message); } else if (ex.Message.Contains("Record is already posted. Cannot allocate new vehicle.")) { throw new InvalidPluginExecutionException("Record is already posted. Cannot allocate new vehicle."); } else if (ex.Message.Contains("Vehicle already exists in Adjustment/Variance Entry.")) { throw new InvalidPluginExecutionException("Vehicle already exists in Adjustment/Variance Entry."); } else if (ex.Message.Contains("Vehicle already exists in inventory.")) { throw new InvalidPluginExecutionException("Vehicle already exists in inventory."); } else { 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 ExecutePreValidateVehicleAdjustmentVarianceEntryDetailDelete(LocalPluginContext localContext) { if (localContext == null) { throw new ArgumentNullException("localContext"); } IPluginExecutionContext context = localContext.PluginExecutionContext; IOrganizationService service = localContext.OrganizationService; ITracingService trace = localContext.TracingService; EntityReference vehicleAdjusmentVarianceEntryDetailEntity = (EntityReference)context.InputParameters["Target"]; string message = context.MessageName; if (context.Depth > 1) { return; } try { EntityCollection vehicleAdjustmentVarianceEntryDetailRecords = CommonHandler.RetrieveRecordsByOneValue("gsc_sls_adjustmentvariancedetail", "gsc_sls_adjustmentvariancedetailid", vehicleAdjusmentVarianceEntryDetailEntity.Id, service, null, OrderType.Ascending, new[] { "gsc_inventoryid", "gsc_vehicleadjustmentvarianceentryid" }); VehicleAdjustmentVarianceEntryDetailHandler vehicleAdjustmentVarianceEntryDetailHandler = new VehicleAdjustmentVarianceEntryDetailHandler(service, trace); vehicleAdjustmentVarianceEntryDetailHandler.AdjustInventoryRecordOnDelete(vehicleAdjustmentVarianceEntryDetailRecords.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 ExecutePostVehicleAdjustmentVarianceEntryDetailUpdate(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 vehicleAdjustmentVarianceEntryDetail = (Entity)context.InputParameters["Target"]; if (!(context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)) { return; } if (vehicleAdjustmentVarianceEntryDetail.LogicalName != "gsc_sls_adjustmentvariancedetail") { return; } if (context.Mode == 0) { String message = context.MessageName; try { #region Pre-Images Guid preImageProductId = preImageEntity.GetAttributeValue <EntityReference>("gsc_productid") != null ? preImageEntity.GetAttributeValue <EntityReference>("gsc_productid").Id : Guid.Empty; String preImageModelCode = preImageEntity.Contains("gsc_modelcode") ? preImageEntity.GetAttributeValue <String>("gsc_modelcode") : String.Empty; String preImageOptionCode = preImageEntity.Contains("gsc_optioncode") ? preImageEntity.GetAttributeValue <String>("gsc_optioncode") : String.Empty; String preImageCsNo = preImageEntity.Contains("gsc_csno") ? preImageEntity.GetAttributeValue <String>("gsc_csno") : String.Empty; String preImageVin = preImageEntity.Contains("gsc_vin") ? preImageEntity.GetAttributeValue <String>("gsc_vin") : String.Empty; String preImageProductionNo = preImageEntity.Contains("gsc_productionno") ? preImageEntity.GetAttributeValue <String>("gsc_productionno") : String.Empty; String preImageEngineNo = preImageEntity.Contains("gsc_engineno") ? preImageEntity.GetAttributeValue <String>("gsc_engineno") : String.Empty; #endregion #region Post-Images Guid postImageProductId = postImageEntity.GetAttributeValue <EntityReference>("gsc_productid") != null ? postImageEntity.GetAttributeValue <EntityReference>("gsc_productid").Id : Guid.Empty; String postImageModelCode = postImageEntity.Contains("gsc_modelcode") ? postImageEntity.GetAttributeValue <String>("gsc_modelcode") : String.Empty; String postImageOptionCode = postImageEntity.Contains("gsc_optioncode") ? postImageEntity.GetAttributeValue <String>("gsc_optioncode") : String.Empty; String postImageCsNo = postImageEntity.Contains("gsc_csno") ? postImageEntity.GetAttributeValue <String>("gsc_csno") : String.Empty; String postImageVin = postImageEntity.Contains("gsc_vin") ? postImageEntity.GetAttributeValue <String>("gsc_vin") : String.Empty; String postImageProductionNo = postImageEntity.Contains("gsc_productionno") ? postImageEntity.GetAttributeValue <String>("gsc_productionno") : String.Empty; String postImageEngineNo = postImageEntity.Contains("gsc_engineno") ? postImageEntity.GetAttributeValue <String>("gsc_engineno") : String.Empty; #endregion VehicleAdjustmentVarianceEntryDetailHandler vehicleAdjustmentVarianceEntryDetailHandler = new VehicleAdjustmentVarianceEntryDetailHandler(service, trace); if (preImageProductId != postImageProductId || preImageModelCode != postImageModelCode || preImageOptionCode != postImageOptionCode || preImageCsNo != postImageCsNo || preImageVin != postImageVin || preImageProductionNo != postImageProductionNo || preImageEngineNo != postImageEngineNo) { vehicleAdjustmentVarianceEntryDetailHandler.CheckExistingInventoryRecord(postImageEntity, message); } } catch (Exception ex) { if (ex.Message.Contains("Please insert product information in Vehicle and Item Catalog")) { throw new InvalidPluginExecutionException(ex.Message); } else if (ex.Message.Contains("Record is already posted. Cannot allocate new vehicle.")) { throw new InvalidPluginExecutionException("Record is already posted. Cannot allocate new vehicle."); } else if (ex.Message.Contains("Vehicle already exists in Adjustment/Variance Entry.")) { throw new InvalidPluginExecutionException("Vehicle already exists in Adjustment/Variance Entry."); } else if (ex.Message.Contains("Vehicle already exists in inventory.")) { throw new InvalidPluginExecutionException("Vehicle already exists in inventory."); } else { throw new InvalidPluginExecutionException(String.Concat("(Exception)\n", ex.Message, Environment.NewLine, ex.StackTrace, Environment.NewLine, ex.StackTrace)); } } } }