/// <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 ExecutePostOpportunityWin(LocalPluginContext localContext) { if (localContext == null) { throw new ArgumentNullException("localContext"); } IPluginExecutionContext context = localContext.PluginExecutionContext; IOrganizationService service = localContext.OrganizationService; ITracingService trace = localContext.TracingService; Entity opportunityEntity = (Entity)context.InputParameters["OpportunityClose"]; OpportunityHandler opportunityHandler = new OpportunityHandler(service, trace); try { opportunityHandler.updateCustomer(opportunityEntity, service, trace); } catch (Exception ex) { //throw new InvalidPluginExecutionException(String.Concat("(Exception)\n", ex.Message, Environment.NewLine, ex.StackTrace, Environment.NewLine, error)); throw new InvalidPluginExecutionException(ex.Message); } }
public void updateCustomer() { #region 1. Setup / Arrange var orgServiceMock = new Mock <IOrganizationService>(); var orgService = orgServiceMock.Object; var orgTracingMock = new Mock <ITracingService>(); var orgTracing = orgTracingMock.Object; #region Opportunity EntityCollection var OpportunityCollection = new EntityCollection { EntityName = "opportunity", Entities = { new Entity { Id = Guid.NewGuid(), LogicalName = "opportunity", Attributes = new AttributeCollection { { "parentcontactid", new EntityReference("contact", Guid.NewGuid()) }, { "parentaccountid", "" } } }, new Entity { Id = Guid.NewGuid(), LogicalName = "opportunity", Attributes = new AttributeCollection { { "parentcontactid", new EntityReference("contact", Guid.NewGuid()) }, { "parentaccountid", "" } } } } }; #endregion #region Contact EntityCollection var ContactCollection = new EntityCollection { EntityName = "contact", Entities = { new Entity { Id = OpportunityCollection.Entities[0].GetAttributeValue <EntityReference>("parentcontactid").Id, LogicalName = "contact", Attributes = new AttributeCollection { { "gsc_ispotential", true }, } }, new Entity { Id = OpportunityCollection.Entities[1].GetAttributeValue <EntityReference>("parentcontactid").Id, LogicalName = "contact", Attributes = new AttributeCollection { { "gsc_ispotential", true }, } } } }; #endregion orgServiceMock.Setup((service => service.RetrieveMultiple( It.Is <QueryExpression>(expression => expression.EntityName == ContactCollection.EntityName) ))).Returns(ContactCollection); #endregion #region 2. Call/Action OpportunityHandler OpportunityHandler = new OpportunityHandler(orgService, orgTracing); OpportunityHandler.updateCustomer(OpportunityCollection.Entities[0], orgService, orgTracing); #endregion #region 3. Verify Assert.AreEqual(false, ContactCollection.Entities[0].GetAttributeValue <bool>("gsc_ispotential")); Assert.AreEqual(true, ContactCollection.Entities[1].GetAttributeValue <bool>("gsc_ispotential")); #endregion }