/// <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 ExecutePostQuoteProductUpdate(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; } if (postImageEntity.LogicalName != "quotedetail") { return; } string message = context.MessageName; //Added by: Raphael Herrera, Added On: 4/29/2016 #region Call functions that will run if gsc_free was updated var preImageFree = preImageEntity.Contains("gsc_free") ? preImageEntity.GetAttributeValue <bool>("gsc_free") : false; var postImageFree = postImageEntity.Contains("gsc_free") ? postImageEntity.GetAttributeValue <bool>("gsc_free") : false; if (preImageFree != postImageFree) { QuoteProductHandler qph = new QuoteProductHandler(service, trace); qph.setActualCost(postImageEntity); } #endregion }
public void SetActualCost() { #region 1. Setup / Arrange var orgServiceMock = new Mock <IOrganizationService>(); var orgService = orgServiceMock.Object; var orgTracingMock = new Mock <ITracingService>(); var orgTracing = orgTracingMock.Object; #region Quote Product Entity Collection var QuoteProductCollection = new EntityCollection { EntityName = "quotedetail", Entities = { new Entity { Id = Guid.NewGuid(), LogicalName = "quotedetail", Attributes = new AttributeCollection { { "gsc_free", (bool)true }, { "gsc_amount", new Money(10000) }, } } } }; #endregion #endregion #region 2. Call/Action QuoteProductHandler quoteproducthandler = new QuoteProductHandler(orgService, orgTracing); quoteproducthandler.setActualCost(QuoteProductCollection.Entities[0]); #endregion #region 3. Verify Assert.AreEqual(0, QuoteProductCollection[0].GetAttributeValue <Money>("gsc_amount").Value); #endregion }