/// <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 ExecutePreReceivingTransactionDetailCreate(LocalPluginContext localContext) { if (localContext == null) { throw new ArgumentNullException("localContext"); } IPluginExecutionContext context = localContext.PluginExecutionContext; IOrganizationService service = localContext.OrganizationService; ITracingService trace = localContext.TracingService; Entity receivingTransactionDetail = (Entity)context.InputParameters["Target"]; string message = context.MessageName; string error = ""; try { ReceivingTransactionDetailHandler receivingTransactionDetailHandler = new ReceivingTransactionDetailHandler(service, trace); receivingTransactionDetailHandler.SetReceivingTransactionDetailFields(receivingTransactionDetail); } 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 ExecutePostReceivingTransactionDetailUpdate(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 receivingTransactionDetailEntity = (Entity)context.InputParameters["Target"]; if (!(context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)) { return; } if (receivingTransactionDetailEntity.LogicalName != "gsc_cmn_receivingtransactiondetail") { return; } if (context.Mode == 0) //Synchronous Plug-in { String message = context.MessageName; try { #region Pre-Images Guid preImageBaseModel = preImageEntity.GetAttributeValue <EntityReference>("gsc_basemodelid") != null ? preImageEntity.GetAttributeValue <EntityReference>("gsc_basemodelid").Id : Guid.Empty; Guid preImageProduct = 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 preImageModelYear = preImageEntity.Contains("gsc_modelyear") ? preImageEntity.GetAttributeValue <String>("gsc_modelyear") : String.Empty; Guid preImageColor = preImageEntity.GetAttributeValue <EntityReference>("gsc_vehiclecolorid") != null ? preImageEntity.GetAttributeValue <EntityReference>("gsc_vehiclecolorid").Id : Guid.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 postImageBaseModel = postImageEntity.GetAttributeValue <EntityReference>("gsc_basemodelid") != null ? postImageEntity.GetAttributeValue <EntityReference>("gsc_basemodelid").Id : Guid.Empty; Guid postImageProduct = 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 postImageModelYear = postImageEntity.Contains("gsc_modelyear") ? postImageEntity.GetAttributeValue <String>("gsc_modelyear") : String.Empty; Guid postImageColor = postImageEntity.GetAttributeValue <EntityReference>("gsc_vehiclecolorid") != null ? postImageEntity.GetAttributeValue <EntityReference>("gsc_vehiclecolorid").Id : Guid.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 ReceivingTransactionDetailHandler receivingTransactionDetailHandler = new ReceivingTransactionDetailHandler(service, trace); if (preImageCsNo != postImageCsNo || preImageVin != postImageVin || preImageProductionNo != postImageProductionNo || preImageEngineNo != postImageEngineNo) { receivingTransactionDetailHandler.CheckExistingInventoryRecord(postImageEntity); } if (preImageBaseModel != postImageBaseModel || preImageProduct != postImageProduct || preImageModelCode != postImageModelCode || preImageOptionCode != postImageOptionCode || preImageModelYear != postImageOptionCode || preImageColor != postImageColor || preImageCsNo != postImageCsNo || preImageVin != postImageVin || preImageProductionNo != postImageProductionNo || preImageEngineNo != postImageEngineNo) { receivingTransactionDetailHandler.CreateUpdateInventoryRecord(postImageEntity); } if (preImageProduct != postImageProduct) { receivingTransactionDetailHandler.SetDNPAmount(postImageEntity); } } catch (Exception ex) { throw new InvalidPluginExecutionException(ex.Message); // throw new InvalidPluginExecutionException(String.Concat("(Exception)\n", ex.Message, Environment.NewLine, ex.StackTrace)); } } }