/// <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 ExecutePreQuoteDiscountCreate(LocalPluginContext localContext) { if (localContext == null) { throw new ArgumentNullException("localContext"); } IPluginExecutionContext context = localContext.PluginExecutionContext; IOrganizationService service = localContext.OrganizationService; ITracingService trace = localContext.TracingService; Entity quoteDiscountEntity = (Entity)context.InputParameters["Target"]; string message = context.MessageName; try { QuoteDiscountHandler quoteDiscountHandler = new QuoteDiscountHandler(service, trace); quoteDiscountHandler.CheckifDiscountExists(quoteDiscountEntity); quoteDiscountHandler.ReplicateDiscountInformation(quoteDiscountEntity, 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 ExecutePostQuoteDiscountUpdate(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; if (!(context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)) { return; } Entity quoteDiscountEntity = (Entity)context.InputParameters["Target"]; if (quoteDiscountEntity.LogicalName != "gsc_cmn_quotediscount") { return; } if (context.Mode == 0) //Synchronous Plugin { try { #region Pre-images var preImageQuoteId = preImageEntity.Contains("gsc_quoteid") ? preImageEntity.GetAttributeValue <EntityReference>("gsc_quoteid").Id : Guid.Empty; var preImagePriceListId = preImageEntity.Contains("gsc_pricelistid") ? preImageEntity.GetAttributeValue <EntityReference>("gsc_pricelistid").Id : Guid.Empty; var preImageDiscountAmount = preImageEntity.Contains("gsc_discountamount") ? preImageEntity.GetAttributeValue <Money>("gsc_discountamount").Value : Decimal.Zero; var preImageApplyAmountToDp = preImageEntity.GetAttributeValue <Money>("gsc_applyamounttodp") != null ? preImageEntity.GetAttributeValue <Money>("gsc_applyamounttodp").Value : Decimal.Zero; var preImageApplyAmountToAf = preImageEntity.GetAttributeValue <Money>("gsc_applyamounttoaf") != null ? preImageEntity.GetAttributeValue <Money>("gsc_applyamounttoaf").Value : Decimal.Zero; var preImageApplyAmountToUp = preImageEntity.GetAttributeValue <Money>("gsc_applyamounttoup") != null ? preImageEntity.GetAttributeValue <Money>("gsc_applyamounttoup").Value : Decimal.Zero; #endregion #region Post-images var postImageQuoteId = postImageEntity.Contains("gsc_quoteid") ? postImageEntity.GetAttributeValue <EntityReference>("gsc_quoteid").Id : Guid.Empty; var postImagePriceListId = postImageEntity.Contains("gsc_pricelistid") ? postImageEntity.GetAttributeValue <EntityReference>("gsc_pricelistid").Id : Guid.Empty; var postImageDiscountAmount = postImageEntity.Contains("gsc_discountamount") ? postImageEntity.GetAttributeValue <Money>("gsc_discountamount").Value : Decimal.Zero; var postImageApplyAmountToDp = postImageEntity.GetAttributeValue <Money>("gsc_applyamounttodp") != null ? postImageEntity.GetAttributeValue <Money>("gsc_applyamounttodp").Value : Decimal.Zero; var postImageApplyAmountToAf = postImageEntity.GetAttributeValue <Money>("gsc_applyamounttoaf") != null ? postImageEntity.GetAttributeValue <Money>("gsc_applyamounttoaf").Value : Decimal.Zero; var postImageApplyAmountToUp = postImageEntity.GetAttributeValue <Money>("gsc_applyamounttoup") != null ? postImageEntity.GetAttributeValue <Money>("gsc_applyamounttoup").Value : Decimal.Zero; #endregion string message = context.MessageName; QuoteDiscountHandler quoteDiscountHandler = new QuoteDiscountHandler(service, trace); if (preImagePriceListId != postImagePriceListId) { quoteDiscountHandler.ReplicateDiscountInformation(quoteDiscountEntity, message); } //Functions triggered on Sales Order, Apply Amount to DP, Apply Amount to AF, Apply Amount to DP change if (preImageQuoteId != postImageQuoteId || preImageApplyAmountToDp != postImageApplyAmountToDp || preImageApplyAmountToAf != postImageApplyAmountToAf || preImageApplyAmountToUp != postImageApplyAmountToUp) { quoteDiscountHandler.SetQuoteTotalDiscountAmount(postImageEntity, message); } } catch (Exception ex) { if (ex.Message.Contains("Cannot associate discount. Vehicle is missing in Quotation Record.")) { throw new InvalidPluginExecutionException("Cannot associate discount. Vehicle is missing in Quotation Record."); } else if (ex.Message.Contains("The Promo selected is not applicable for the product of this Quote.")) { throw new InvalidPluginExecutionException("The Promo selected is not applicable for the product of this Quote."); } else { throw new InvalidPluginExecutionException(String.Concat("(Exception)\n", ex.Message, Environment.NewLine, ex.StackTrace)); } } } }