/// <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 ExecutePostOpportunityUpdate(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; string message = context.MessageName; if (!(context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)) { return; } Entity opportunityEntity = (Entity)context.InputParameters["Target"]; if (opportunityEntity.LogicalName != "opportunity") { return; } if (context.Mode == 0) //Synchronous Plugin { try { var preImageGenerateQuote = preImageEntity.GetAttributeValue <int>("gsc_quotecount"); var preImageStatus = preImageEntity.GetAttributeValue <OptionSetValue>("statecode").Value; var postImageGenerateQuote = postImageEntity.GetAttributeValue <int>("gsc_quotecount"); var postImageStatus = postImageEntity.GetAttributeValue <OptionSetValue>("statecode").Value; OpportunityHandler opportunityHandler = new OpportunityHandler(service, trace); if (preImageGenerateQuote != postImageGenerateQuote) { opportunityHandler.generateQuote(postImageEntity); opportunityHandler.ReplicateProspectInfo(postImageEntity); } if (preImageStatus != postImageStatus) { opportunityHandler.RestrictOpportunityCloseAsLost(postImageEntity); opportunityHandler.RestrictOpportunityCloseAsWon(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); } } }
/// <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 ExecutePreValidateOpportunityDelete(LocalPluginContext localContext) { if (localContext == null) { throw new ArgumentNullException("localContext"); } IPluginExecutionContext context = localContext.PluginExecutionContext; IOrganizationService service = localContext.OrganizationService; ITracingService trace = localContext.TracingService; var opportunity = (EntityReference)context.InputParameters["Target"]; try { EntityCollection opportuntiyCollection = CommonHandler.RetrieveRecordsByOneValue("opportunity", "opportunityid", opportunity.Id, service, null, OrderType.Ascending, new[] { "opportunityid" }); OpportunityHandler opportunityHandler = new OpportunityHandler(service, trace); opportunityHandler.ValidateDelete(opportuntiyCollection.Entities[0]); } 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 ExecutePostOpportunityCreate(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["Target"]; string message = context.MessageName; string error = ""; try { OpportunityHandler opportunityHandler = new OpportunityHandler(service, trace); opportunityHandler.ReplicateProspectInquiryActivities(opportunityEntity); opportunityHandler.ReplicateProspectInfo(opportunityEntity); } 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 ExecutePreOpportunityCreate(LocalPluginContext localContext) { if (localContext == null) { throw new ArgumentNullException("localContext"); } IPluginExecutionContext context = localContext.PluginExecutionContext; IOrganizationService service = localContext.OrganizationService; ITracingService trace = localContext.TracingService; Entity opportunity = (Entity)context.InputParameters["Target"]; string message = context.MessageName; OpportunityHandler handler = new OpportunityHandler(service, trace); handler.ReplicateInquiryInfo(opportunity); }
/// <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 linkContact() { #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", EntityState = EntityState.Changed, Attributes = new AttributeCollection { { "parentcontactid", new EntityReference("contact", Guid.NewGuid()) }, { "parentaccountid", new EntityReference("account", Guid.NewGuid()) }, { "originatingleadid", new EntityReference("lead", Guid.NewGuid()) }, } }, new Entity { Id = Guid.NewGuid(), LogicalName = "opportunity", EntityState = EntityState.Changed, Attributes = new AttributeCollection { { "parentcontactid", new EntityReference("contact", Guid.NewGuid()) }, { "parentaccountid", new EntityReference("account", Guid.NewGuid()) }, { "originatingleadid", new EntityReference("lead", Guid.NewGuid()) }, } } } }; #endregion #region Contact EntityCollection var ContactCollection = new EntityCollection { EntityName = "contact", Entities = { new Entity { Id = OpportunityCollection.Entities[0].GetAttributeValue <EntityReference>("parentcontactid").Id, LogicalName = "contact", }, new Entity { Id = OpportunityCollection.Entities[1].GetAttributeValue <EntityReference>("parentcontactid").Id, LogicalName = "contact", } } }; #endregion #region Lead EntityCollection var LeadCollection = new EntityCollection { EntityName = "lead", Entities = { new Entity { Id = OpportunityCollection.Entities[0].GetAttributeValue <EntityReference>("originatingleadid").Id, LogicalName = "lead", Attributes = new AttributeCollection { { "parentcontactid", new EntityReference("contact", Guid.NewGuid()) }, { "parentaccountid", new EntityReference("contact", Guid.NewGuid()) }, { "customerid", new EntityReference("customer", Guid.NewGuid()) } } }, new Entity { Id = OpportunityCollection.Entities[1].GetAttributeValue <EntityReference>("originatingleadid").Id, LogicalName = "lead", Attributes = new AttributeCollection { { "parentcontactid", new EntityReference("contact", Guid.NewGuid()) }, { "parentaccountid", new EntityReference("contact", Guid.NewGuid()) }, { "customerid", new EntityReference("customer", Guid.NewGuid()) } } } } }; #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.linkCustomer(OpportunityCollection.Entities[0], orgService, orgTracing); #endregion #region 3. Verify Assert.AreEqual(OpportunityCollection.Entities[0].GetAttributeValue <EntityReference>("parentcontactid").Id, LeadCollection.Entities[0].GetAttributeValue <EntityReference>("parentcontactid").Id); Assert.AreEqual(OpportunityCollection.Entities[0].GetAttributeValue <EntityReference>("parentcontactid").Id, LeadCollection.Entities[0].GetAttributeValue <EntityReference>("customerid").Id); // Assert.AreEqual(ContactCollection.Entities[0].Id, LeadCollection.Entities[0].GetAttributeValue<EntityReference>("parentcontactid").Id); #endregion }
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 }
public void ReplicateProspectInquiryInfoUnitTest() { #region 1. Setup / Arrange var orgServiceMock = new Mock <IOrganizationService>(); var orgService = orgServiceMock.Object; var orgTracingMock = new Mock <ITracingService>(); var orgTracing = orgTracingMock.Object; #region ProspectInquiry EntityCollection var ProspectInquiryCollection = new EntityCollection { EntityName = "lead", Entities = { new Entity { Id = Guid.NewGuid(), LogicalName = "lead", EntityState = EntityState.Changed, Attributes = new AttributeCollection { { "gsc_salesexecutiveid", new EntityReference("contact", Guid.NewGuid()) { Name = "Citimotors" } }, { "gsc_vehiclebasemodelid", new EntityReference("gsc_iv_vehiclebasemodel", Guid.NewGuid()) { Name = "Montero" } }, { "gsc_colorid", new EntityReference("gsc_iv_color", Guid.NewGuid()) { Name = "Jet Black" } }, { "gsc_leadsourceid", new EntityReference("gsc_sls_leadsource", Guid.NewGuid()) { Name = "Mall Display" } }, { "subject", "Montero Sport SUA Terrorista" }, } } } }; #endregion #region Opportunity EntityCollection var OpportunityCollection = new EntityCollection { EntityName = "opportunity", Entities = { new Entity { Id = Guid.NewGuid(), LogicalName = "opportunity", EntityState = EntityState.Created, Attributes = new AttributeCollection { { "gsc_salesexecutiveid", "" }, { "gsc_vehiclebasemodelid", "" }, { "gsc_colorid", "" }, { "gsc_leadsourceid", "" }, { "gsc_topic", "" }, { "originatingleadid", new EntityReference("lead", ProspectInquiryCollection.Entities[0].Id)}, } } } }; #endregion orgServiceMock.Setup((service => service.RetrieveMultiple( It.Is <QueryExpression>(expression => expression.EntityName == ProspectInquiryCollection.EntityName) ))).Returns(ProspectInquiryCollection); #endregion #region 2. Call/Action var OpportunityHandler = new OpportunityHandler(orgService, orgTracing); Entity opportunity = OpportunityHandler.ReplicateInquiryInfo(OpportunityCollection.Entities[0]); #endregion #region 3. Verify Assert.AreEqual(ProspectInquiryCollection.Entities[0].GetAttributeValue <EntityReference>("gsc_salesexecutiveid").Id, opportunity.GetAttributeValue <EntityReference>("gsc_salesexecutiveid").Id); Assert.AreEqual(ProspectInquiryCollection.Entities[0].GetAttributeValue <EntityReference>("gsc_vehiclebasemodelid").Id, opportunity.GetAttributeValue <EntityReference>("gsc_vehiclebasemodelid").Id); Assert.AreEqual(ProspectInquiryCollection.Entities[0].GetAttributeValue <EntityReference>("gsc_colorid").Id, opportunity.GetAttributeValue <EntityReference>("gsc_colorid").Id); Assert.AreEqual(ProspectInquiryCollection.Entities[0].GetAttributeValue <EntityReference>("gsc_leadsourceid").Id, opportunity.GetAttributeValue <EntityReference>("gsc_leadsourceid").Id); Assert.AreEqual(ProspectInquiryCollection.Entities[0]["subject"], opportunity["gsc_topic"]); #endregion }