/// <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 ExecutePreValidateQuoteAccessoryDelete(LocalPluginContext localContext) { if (localContext == null) { throw new ArgumentNullException("localContext"); } IPluginExecutionContext context = localContext.PluginExecutionContext; IOrganizationService service = localContext.OrganizationService; ITracingService trace = localContext.TracingService; var accessoryEntity = (EntityReference)context.InputParameters["Target"]; string message = context.MessageName; try { EntityCollection quoteAccessoryCollection = CommonHandler.RetrieveRecordsByOneValue("gsc_sls_quoteaccessory", "gsc_sls_quoteaccessoryid", accessoryEntity.Id, service, null, OrderType.Ascending, new[] { "gsc_quoteaccessorypn", "gsc_quoteid", "gsc_actualcost", "gsc_standard" }); QuoteAccessoryHandler quoteAccessoryHandler = new QuoteAccessoryHandler(service, trace); Entity quoteAccessory = quoteAccessoryCollection.Entities[0]; if (quoteAccessoryHandler.IsAccessoryStandard(quoteAccessory)) { throw new InvalidPluginExecutionException("Cannot remove default accessories."); } quoteAccessoryHandler.SetTotalAccessories(quoteAccessory, message); } catch (Exception ex) { throw new InvalidPluginExecutionException(ex.Message); } }
/// <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 ExecutePreQuoteAccessoryCreate(LocalPluginContext localContext) { if (localContext == null) { throw new ArgumentNullException("localContext"); } IPluginExecutionContext context = localContext.PluginExecutionContext; IOrganizationService service = localContext.OrganizationService; ITracingService trace = localContext.TracingService; Entity accessoryEntity = (Entity)context.InputParameters["Target"]; string message = context.MessageName; try { QuoteAccessoryHandler quoteAccessoryHandler = new QuoteAccessoryHandler(service, trace); Entity quoteAccessory = quoteAccessoryHandler.PopulateDetails(accessoryEntity, message); quoteAccessoryHandler.PopulateDetails(quoteAccessory, message); quoteAccessoryHandler.SetTotalAccessories(quoteAccessory, message); } catch (Exception ex) { throw new InvalidPluginExecutionException(String.Concat(ex.Message)); } }
/// <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 ExecutePostQuoteAccessoryUpdate(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 accessoryEntity = (Entity)context.InputParameters["Target"]; if (!(context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)) { return; } if (accessoryEntity.LogicalName != "gsc_sls_quoteaccessory") { return; } if (context.Mode == 0) //Synchronous Plug-in { string message = context.MessageName; try { QuoteAccessoryHandler quoteAccessoryHandler = new QuoteAccessoryHandler(service, trace); #region pre-Image Guid preImageItem = preImageEntity.GetAttributeValue <EntityReference>("gsc_productid") != null ? preImageEntity.GetAttributeValue <EntityReference>("gsc_productid").Id : Guid.Empty; var preImageFree = preImageEntity.Contains("gsc_free") ? preImageEntity.GetAttributeValue <Boolean>("gsc_free") : false; var preImageActualCost = preImageEntity.Contains("gsc_actualcost") ? preImageEntity.GetAttributeValue <Money>("gsc_actualcost").Value : 0; #endregion #region post-Image Guid postImageItem = postImageEntity.GetAttributeValue <EntityReference>("gsc_productid") != null ? postImageEntity.GetAttributeValue <EntityReference>("gsc_productid").Id : Guid.Empty; var postImageFree = postImageEntity.Contains("gsc_free") ? postImageEntity.GetAttributeValue <Boolean>("gsc_free") : false; var postImageActualCost = postImageEntity.Contains("gsc_actualcost") ? postImageEntity.GetAttributeValue <Money>("gsc_actualcost").Value : 0; #endregion if (preImageItem != postImageItem) { quoteAccessoryHandler.PopulateDetails(postImageEntity, message); quoteAccessoryHandler.SetTotalAccessories(postImageEntity, message); } if (preImageFree != postImageFree) { quoteAccessoryHandler.UpdateActualCost(postImageEntity); } if (preImageActualCost != postImageActualCost) { quoteAccessoryHandler.SetTotalAccessories(postImageEntity, message); } } catch (Exception ex) { throw new InvalidPluginExecutionException(String.Concat(ex.Message)); } } }