public void CreateCorporateIndividual() { #region 1. Setup / Arrange var orgServiceMock = new Mock <IOrganizationService>(); var orgService = orgServiceMock.Object; var orgTracingMock = new Mock <ITracingService>(); var orgTracing = orgTracingMock.Object; var sampleGuid = new Guid("ccfa2910-fa16-4c77-9c73-a2e646a2fe44"); #region Collection Entity var ContactCollection = new EntityCollection() { EntityName = "account", Entities = { } }; #endregion #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_customertype", new OptionSetValue(100000001) }, { "gsc_firstname", "Leslie" }, { "gsc_middlename", "G" }, { "gsc_lastname", "Baliguat" }, { "gsc_emailaddress", "*****@*****.**" }, { "gsc_mobileno", "1" }, { "gsc_alternatecontactno", "2" }, { "gsc_fax", "3" }, { "gsc_gender", new OptionSetValue(1) }, { "gsc_maritalstatus", new OptionSetValue(1) }, { "gsc_countryid", new EntityReference("country", sampleGuid)}, { "gsc_provinceid", new EntityReference("province", sampleGuid)}, { "gsc_cityid", new EntityReference("city", sampleGuid)}, { "gsc_street", "1" }, { "gsc_zipcode", "1" }, { "gsc_dealerid", new EntityReference("account", sampleGuid)}, { "gsc_branchid", new EntityReference("account", sampleGuid)}, { "gsc_recordownerid", new EntityReference("contact", sampleGuid)}, { "gsc_companyname", "Star Magic" }, { "gsc_phone", "2" }, { "gsc_website", "www.starmagic.com" }, } } } }; #endregion #region ProspectInquiry EntityCollection var ProspectInquiryCollection = new EntityCollection { EntityName = "lead", Entities = { new Entity { Id = Guid.NewGuid(), LogicalName = "lead", EntityState = EntityState.Changed, Attributes = new AttributeCollection { { "gsc_qualified", true }, { "gsc_prospectid", new EntityReference(ProspectCollection.EntityName, ProspectCollection.Entities[0].Id)}, { "parentaccountid", null }, { "parentcontactid", null } } } } }; #endregion orgServiceMock.Setup((service => service.RetrieveMultiple( It.Is <QueryExpression>(expression => expression.EntityName == ProspectCollection.EntityName) ))).Returns(ProspectCollection); orgServiceMock.Setup((service => service.RetrieveMultiple( It.Is <QueryExpression>(expression => expression.EntityName == ContactCollection.EntityName) ))).Returns(ContactCollection); orgServiceMock.Setup(service => service.Retrieve( It.IsAny <string>(), It.IsAny <Guid>(), It.IsAny <ColumnSet>())).Returns(ProspectInquiryCollection.Entities[0]); orgServiceMock.Setup((service => service.Update(It.Is <Entity>(entity => entity.LogicalName == ProspectInquiryCollection.Entities[0].LogicalName)))).Callback <Entity>(s => ProspectInquiryCollection.Entities[0] = s); #endregion #region 2. Call/Action var ProspectInquiryHandler = new ProspectInquiryHandler(orgService, orgTracing); Entity ContactEntity = ProspectInquiryHandler.CreateCustomer(ProspectInquiryCollection.Entities[0]); #endregion #region 3. Verify //Assert.AreEqual(ProspectCollection.Entities[0].GetAttributeValue<OptionSetValue>("gsc_customertype").Value - 1, ContactEntity.GetAttributeValue<OptionSetValue>("gsc_customertype").Value); //Assert.AreEqual(ProspectCollection.Entities[0].GetAttributeValue<String>("gsc_companyname"), ContactEntity.GetAttributeValue<String>("name")); //Assert.AreEqual(ProspectCollection.Entities[0].GetAttributeValue<String>("gsc_phone"), ContactEntity.GetAttributeValue<String>("telephone1")); //Assert.AreEqual(ProspectCollection.Entities[0].GetAttributeValue<String>("gsc_website"), ContactEntity.GetAttributeValue<String>("websiteurl")); //Assert.AreEqual(ProspectCollection.Entities[0].GetAttributeValue<String>("gsc_fax"), ContactEntity.GetAttributeValue<String>("fax")); //Assert.AreEqual(ProspectCollection.Entities[0].GetAttributeValue<EntityReference>("gsc_countryid").Id, ContactEntity.GetAttributeValue<EntityReference>("gsc_countryid").Id); //Assert.AreEqual(ProspectCollection.Entities[0].GetAttributeValue<EntityReference>("gsc_provinceid").Id, ContactEntity.GetAttributeValue<EntityReference>("gsc_provinceid").Id); //Assert.AreEqual(ProspectCollection.Entities[0].GetAttributeValue<EntityReference>("gsc_cityid").Id, ContactEntity.GetAttributeValue<EntityReference>("gsc_cityid").Id); //Assert.AreEqual(ProspectCollection.Entities[0].GetAttributeValue<String>("gsc_street"), ContactEntity.GetAttributeValue<String>("address1_line1")); //Assert.AreEqual(ProspectCollection.Entities[0].GetAttributeValue<String>("gsc_zipcode"), ContactEntity.GetAttributeValue<String>("address1_postalcode")); //Assert.AreEqual(ProspectCollection.Entities[0].GetAttributeValue<EntityReference>("gsc_dealerid").Id, ContactEntity.GetAttributeValue<EntityReference>("gsc_dealerid").Id); //Assert.AreEqual(ProspectCollection.Entities[0].GetAttributeValue<EntityReference>("gsc_branchid").Id, ContactEntity.GetAttributeValue<EntityReference>("gsc_branchid").Id); //Assert.AreEqual(ProspectCollection.Entities[0].GetAttributeValue<EntityReference>("gsc_recordownerid").Id, ContactEntity.GetAttributeValue<EntityReference>("gsc_recordownerid").Id); //Assert.AreEqual(true, ContactEntity.GetAttributeValue<Boolean>("gsc_isaccount")); //Assert.AreEqual(true, ContactEntity.GetAttributeValue<Boolean>("gsc_ispotential")); //Assert.AreEqual(true, ContactEntity.GetAttributeValue<Boolean>("gsc_prospect")); Assert.AreEqual(true, ProspectInquiryCollection.Entities[0].GetAttributeValue <EntityReference>("parentcontactid") == null); Assert.AreEqual(false, ProspectInquiryCollection.Entities[0].GetAttributeValue <EntityReference>("parentaccountid") == null); #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 ExecutePostProspectInquiryUpdate(LocalPluginContext localContext) { if (localContext == null) { throw new ArgumentNullException("localContext"); } IPluginExecutionContext context = localContext.PluginExecutionContext; IOrganizationService service = localContext.OrganizationService; ITracingService trace = localContext.TracingService; 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; if (!(context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)) { return; } if (postImageEntity.LogicalName != "lead") { return; } string message = context.MessageName; if (context.Mode == 0) //synchronous plugin { try { ProspectInquiryHandler prospectInquiryHandler = new ProspectInquiryHandler(service, trace); var preImageBaseModelId = preImageEntity.Contains("gsc_vehiclebasemodelid") ? preImageEntity.GetAttributeValue <EntityReference>("gsc_vehiclebasemodelid").Id : Guid.Empty; var preImageDisqualified = preImageEntity.GetAttributeValue <Boolean>("gsc_disqualified"); var preImageDisqualifiedStatusReason = preImageEntity.Contains("gsc_disqualifiedstatusreason") ? preImageEntity.GetAttributeValue <Int32>("gsc_disqualifiedstatusreason") : 0; var preImageQualified = preImageEntity.GetAttributeValue <Boolean>("gsc_qualified"); var preImageCityName = preImageEntity.Contains("gsc_cityname") ? preImageEntity.GetAttributeValue <string>("gsc_cityname") : String.Empty; var preImageCityIdName = preImageEntity.Contains("gsc_cityid") ? preImageEntity.GetAttributeValue <EntityReference>("gsc_cityid").Name : String.Empty; var postImageBaseModelId = postImageEntity.Contains("gsc_vehiclebasemodelid") ? postImageEntity.GetAttributeValue <EntityReference>("gsc_vehiclebasemodelid").Id : Guid.Empty; var postImageDisqualified = postImageEntity.GetAttributeValue <Boolean>("gsc_disqualified"); var postImageDisqualifiedStatusReason = postImageEntity.Contains("gsc_disqualifiedstatusreason") ? postImageEntity.GetAttributeValue <Int32>("gsc_disqualifiedstatusreason") : 0; var postImageQualified = postImageEntity.GetAttributeValue <Boolean>("gsc_qualified"); var postImageCityName = postImageEntity.Contains("gsc_cityname") ? postImageEntity.GetAttributeValue <string>("gsc_cityname") : String.Empty; var postImageCityIdName = postImageEntity.Contains("gsc_cityid") ? postImageEntity.GetAttributeValue <EntityReference>("gsc_cityid").Name : String.Empty; //execute the method when Base Model Id was changed if (preImageBaseModelId != postImageBaseModelId) { prospectInquiryHandler.ConcatenateVehicleInfo(postImageEntity, message); } //Call DisqualifyProspectInquiry method on Disqualified and Disqualified Status Reason change if (preImageDisqualified != postImageDisqualified && preImageDisqualifiedStatusReason != postImageDisqualifiedStatusReason) { prospectInquiryHandler.DisqualifyProspectInquiry(postImageEntity); } //Call QualifyProsctInquiry if (preImageQualified != postImageQualified) { prospectInquiryHandler.CreateCustomer(postImageEntity); prospectInquiryHandler.CreateOpportunity(postImageEntity); prospectInquiryHandler.QualifyProspectInquiry(postImageEntity); } if (postImageCityName != postImageCityIdName && postImageCityName != String.Empty && preImageCityIdName == postImageCityIdName) { prospectInquiryHandler.SetCity(postImageEntity); } } catch (Exception ex) { throw new InvalidPluginExecutionException(String.Concat(ex.Message)); } } }