/// <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 ExecutePreProspectCreate(LocalPluginContext localContext) { if (localContext == null) { throw new ArgumentNullException("localContext"); } IPluginExecutionContext context = localContext.PluginExecutionContext; IOrganizationService service = localContext.OrganizationService; ITracingService trace = localContext.TracingService; Entity prospectEntity = (Entity)context.InputParameters["Target"]; try { ProspectHandler prospect = new ProspectHandler(service, trace); if (prospect.CheckForExistingRecords(prospectEntity) == true) { throw new InvalidPluginExecutionException("This record has been identified as a fraud account. Please ask the customer to provide further information."); } } catch (Exception ex) { if (ex.Message.Contains("This record has been identified as a fraud account. Please ask the customer to provide further information.")) { throw new InvalidPluginExecutionException(ex.Message); } else { throw new InvalidPluginExecutionException(String.Concat("(Exception)\n", ex.Message, Environment.NewLine, ex.StackTrace, Environment.NewLine, ex)); } } }
public void CheckForExistingRecords() { #region 1. Setup / Arrange var orgServiceMock = new Mock <IOrganizationService>(); var orgService = orgServiceMock.Object; var orgTracingMock = new Mock <ITracingService>(); var orgTracing = orgTracingMock.Object; #region Prospect EntityCollection var prospectCollection = new EntityCollection { EntityName = "gsc_sls_prospect", Entities = { new Entity { Id = Guid.NewGuid(), LogicalName = "gsc_sls_prospect", EntityState = EntityState.Created, Attributes = new AttributeCollection { { "gsc_firstname", "testfirst" }, { "gsc_lastname", "testlast" }, { "gsc_mobileno", "09999999999" }, { "gsc_birthday", new DateTime(1990, 1, 1)} } } } }; #endregion #region Contact EntityCollection var ContactCollection = new EntityCollection { EntityName = "contact", Entities = { new Entity { Id = Guid.NewGuid(), LogicalName = "contact", Attributes = new AttributeCollection { { "contactid", "2" }, { "firstname", "testfirst" }, { "lastname", "testlast" }, { "mobileno", "09999999999" }, { "birthday", new DateTime(1990, 1, 1)}, { "gsc_fraud", true } } }, new Entity { Id = Guid.NewGuid(), LogicalName = "contact", Attributes = new AttributeCollection { { "contactid", "3" }, { "firstname", "" }, { "lastname", "" }, { "mobileno", "" }, { "birthday", "" }, { "gsc_fraud", false } } } } }; #endregion orgServiceMock.Setup((service => service.RetrieveMultiple( It.Is <QueryExpression>(expression => expression.EntityName == ContactCollection.EntityName) ))).Returns(ContactCollection); #endregion #region 2. Call/Action var prospectHandler = new ProspectHandler(orgService, orgTracing); bool res = prospectHandler.CheckForExistingRecords(prospectCollection[0]); #endregion #region 3. Verify Assert.AreEqual(true, res); #endregion }