public void ReplicateProspectInfoUnitTest() { #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.Changed, Attributes = new AttributeCollection { { "gsc_firstname", "Mark" }, { "gsc_middlename", "" }, { "gsc_lastname", "Opaco" }, { "gsc_mobileno", "09123456789" }, { "gsc_emailaddress", "*****@*****.**" }, { "gsc_street", "Antipolo St." }, { "gsc_cityid", new EntityReference("gsc_syscity", Guid.NewGuid()) { Name = "Manila" } }, { "gsc_provinceid", new EntityReference("gsc_sysregion", Guid.NewGuid()) { Name = "Manila" } }, { "gsc_countryid", new EntityReference("gsc_syscountry", Guid.NewGuid()) { Name = "Philippines" } }, } } } }; #endregion #region ProspectInquiry EntityCollection var ProspectInquiryCollection = new EntityCollection { EntityName = "Prospect Inquiry", Entities = { new Entity { Id = Guid.NewGuid(), LogicalName = "lead", EntityState = EntityState.Created, Attributes = new AttributeCollection { { "firstname", "" }, { "middlename", "" }, { "lastname", "" }, { "fullname", "" }, { "mobilephone", "" }, { "emailaddress1", "" }, { "address1_line1", "" }, { "address1_city", "" }, { "address1_stateorprovince", "" }, { "address1_country", "" }, { "gsc_prospectid", new EntityReference("gsc_sls_prospect", ProspectCollection.Entities[0].Id)}, } } } }; #endregion orgServiceMock.Setup((service => service.RetrieveMultiple( It.Is <QueryExpression>(expression => expression.EntityName == ProspectCollection.EntityName) ))).Returns(ProspectCollection); #endregion #region 2. Call/Action var ProspectInquiryHandler = new ProspectInquiryHandler(orgService, orgTracing); Entity prospectInquiry = ProspectInquiryHandler.ReplicateProspectInfo(ProspectInquiryCollection.Entities[0]); #endregion #region 3. Verify var fullname = ProspectCollection.Entities[0]["gsc_firstname"] + " " + ProspectCollection.Entities[0]["gsc_lastname"]; Assert.AreEqual(ProspectCollection.Entities[0]["gsc_firstname"], prospectInquiry["firstname"]); Assert.AreEqual(ProspectCollection.Entities[0]["gsc_middlename"], prospectInquiry["middlename"]); Assert.AreEqual(ProspectCollection.Entities[0]["gsc_lastname"], prospectInquiry["lastname"]); Assert.AreEqual(fullname, prospectInquiry["fullname"]); Assert.AreEqual(ProspectCollection.Entities[0]["gsc_mobileno"], prospectInquiry["mobilephone"]); Assert.AreEqual(ProspectCollection.Entities[0]["gsc_emailaddress"], prospectInquiry["emailaddress1"]); Assert.AreEqual(ProspectCollection.Entities[0]["gsc_street"], prospectInquiry["address1_line1"]); Assert.AreEqual(ProspectCollection.Entities[0].GetAttributeValue <EntityReference>("gsc_cityid").Name, prospectInquiry["address1_city"]); Assert.AreEqual(ProspectCollection.Entities[0].GetAttributeValue <EntityReference>("gsc_provinceid").Name, prospectInquiry["address1_stateorprovince"]); Assert.AreEqual(ProspectCollection.Entities[0].GetAttributeValue <EntityReference>("gsc_countryid").Name, prospectInquiry["address1_country"]); #endregion }
/// <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 ExecutePreProspectInquiryCreate(LocalPluginContext localContext) { if (localContext == null) { throw new ArgumentNullException("localContext"); } IPluginExecutionContext context = localContext.PluginExecutionContext; IOrganizationService service = localContext.OrganizationService; ITracingService trace = localContext.TracingService; Entity prospectInquiry = (Entity)context.InputParameters["Target"]; if (!(context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)) { return; } if (prospectInquiry.LogicalName != "lead") { return; } string message = context.MessageName; if (context.Mode == 0) //synchronous plugin { try { ProspectInquiryHandler prospectInquiryHandler = new ProspectInquiryHandler(service, trace); prospectInquiryHandler.GetDefaultTax(); prospectInquiryHandler.ReplicateProspectInfo(prospectInquiry); prospectInquiryHandler.ConcatenateVehicleInfo(prospectInquiry, message); //set Portal User Id prospectInquiry["gsc_portaluserid"] = prospectInquiry.GetAttributeValue <EntityReference>("gsc_recordownerid") != null ? prospectInquiry.GetAttributeValue <EntityReference>("gsc_recordownerid").Id.ToString() : String.Empty; //Set Name for corporate var prospectType = prospectInquiry.Contains("gsc_prospecttype") ? prospectInquiry.GetAttributeValue <OptionSetValue>("gsc_prospecttype").Value : 0; if (prospectType != 100000000) { prospectInquiry["gsc_prospectname"] = prospectInquiry.GetAttributeValue <String>("companyname") != null ? prospectInquiry.GetAttributeValue <String>("companyname") : String.Empty; } else { prospectInquiry["gsc_prospectname"] = prospectInquiry.GetAttributeValue <String>("fullname") != null ? prospectInquiry.GetAttributeValue <String>("fullname") : String.Empty; } if (prospectInquiryHandler.CheckForExistingRecords(prospectInquiry) == 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("This record has been identified as a fraud account. Please ask the customer to provide further information."); } else { throw new InvalidPluginExecutionException(ex.Message); } } } }