/// <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 ExecutePostPriceListItemCreate(LocalPluginContext localContext) { if (localContext == null) { throw new ArgumentNullException("localContext"); } IPluginExecutionContext context = localContext.PluginExecutionContext; IOrganizationService service = localContext.OrganizationService; ITracingService trace = localContext.TracingService; Entity priceListItemEntity = (Entity)context.InputParameters["Target"]; string message = context.MessageName; string error = ""; try { PriceListItemHandler priceListItemHandler = new PriceListItemHandler(service, trace); #region Calling SetStandardSellPriceAmount method EntityCollection priceListItemRecords = CommonHandler.RetrieveRecordsByOneValue("productpricelevel", "productpricelevelid", priceListItemEntity.Id, service, null, OrderType.Ascending, new[] { "productid", "pricelevelid", "amount", "transactioncurrencyid" }); if (priceListItemRecords != null && priceListItemRecords.Entities.Count > 0) { Entity priceListItem = priceListItemRecords.Entities[0]; priceListItemHandler.CheckifPriceListisDefault(priceListItem); // priceListItemHandler.CreateExtendedPriceListItemRecord(priceListItem); } #endregion } catch (Exception ex) { //throw new InvalidPluginExecutionException(String.Concat("(Exception)\n", ex.Message, Environment.NewLine, ex.StackTrace, Environment.NewLine, error)); 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 ExecutePostPriceListItemUpdate(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 priceListItemEntity = (Entity)context.InputParameters["Target"]; if (priceListItemEntity.LogicalName != "productpricelevel") { return; } if (context.Mode == 0) //Synchronous Plugin { try { #region Pre-images var preImageAmount = preImageEntity.GetAttributeValue <Money>("amount") != null ? preImageEntity.GetAttributeValue <Money>("amount").Value : Decimal.Zero; var preImageProductId = preImageEntity.GetAttributeValue <EntityReference>("productid") != null ? preImageEntity.GetAttributeValue <EntityReference>("productid").Id : Guid.Empty; #endregion #region Post-images var postImageAmount = postImageEntity.GetAttributeValue <Money>("amount") != null ? postImageEntity.GetAttributeValue <Money>("amount").Value : Decimal.Zero; var postImageProductId = postImageEntity.GetAttributeValue <EntityReference>("productid") != null ? postImageEntity.GetAttributeValue <EntityReference>("productid").Id : Guid.Empty; #endregion if (preImageAmount != postImageAmount || preImageProductId != postImageProductId) { string message = context.MessageName; PriceListItemHandler priceListItemHandler = new PriceListItemHandler(service, trace); priceListItemHandler.CheckifPriceListisDefault(postImageEntity); } } catch (Exception ex) { //throw new InvalidPluginExecutionException(String.Concat("(Exception)\n", ex.Message, Environment.NewLine, ex.StackTrace, Environment.NewLine, error)); throw new InvalidPluginExecutionException(ex.Message); } } }