From dbb0080083a9fde42e378d982f4e90618d5c8b0e Mon Sep 17 00:00:00 2001 From: Niels Ladekarl Date: Mon, 16 Apr 2018 15:02:08 +0200 Subject: [PATCH 1/4] Implemented QualifyLeadRequestHandler --- src/XrmMockupShared/Core.cs | 1 + .../Requests/QualifyLeadRequestHandler.cs | 153 +++++ src/XrmMockupShared/Utility.cs | 13 +- src/XrmMockupShared/XrmMockupShared.projitems | 1 + tests/SharedTests/SharedTests.projitems | 1 + tests/SharedTests/TestLeads.cs | 646 ++++++++++++++++++ 6 files changed, 808 insertions(+), 7 deletions(-) create mode 100644 src/XrmMockupShared/Requests/QualifyLeadRequestHandler.cs create mode 100644 tests/SharedTests/TestLeads.cs diff --git a/src/XrmMockupShared/Core.cs b/src/XrmMockupShared/Core.cs index b46208d8..62c4055e 100644 --- a/src/XrmMockupShared/Core.cs +++ b/src/XrmMockupShared/Core.cs @@ -133,6 +133,7 @@ private void InitializeDB() { new RetrieveAllOptionSetsRequestHandler(this, db, metadata, security), new RetrieveOptionSetRequestHandler(this, db, metadata, security), new RetrieveExchangeRateRequestHandler(this, db, metadata, security), + new QualifyLeadRequestHandler(this, db, metadata, security), #if !(XRM_MOCKUP_2011 || XRM_MOCKUP_2013) new IsValidStateTransitionRequestHandler(this, db, metadata, security), new CalculateRollupFieldRequestHandler(this, db, metadata, security), diff --git a/src/XrmMockupShared/Requests/QualifyLeadRequestHandler.cs b/src/XrmMockupShared/Requests/QualifyLeadRequestHandler.cs new file mode 100644 index 00000000..e91d9087 --- /dev/null +++ b/src/XrmMockupShared/Requests/QualifyLeadRequestHandler.cs @@ -0,0 +1,153 @@ +using DG.Tools.XrmMockup; +using DG.Tools.XrmMockup.Database; +using Microsoft.Crm.Sdk.Messages; +using Microsoft.Xrm.Sdk; +using Microsoft.Xrm.Sdk.Metadata; +using System; +using System.Collections.Generic; +using System.Linq; +using System.ServiceModel; +using System.Text; + +namespace DG.Tools.XrmMockup +{ + internal class QualifyLeadRequestHandler : RequestHandler + { + internal QualifyLeadRequestHandler(Core core, XrmDb db, MetadataSkeleton metadata, Security security) : base(core, db, metadata, security, "QualifyLead") + { + } + + internal override OrganizationResponse Execute(OrganizationRequest orgRequest, EntityReference userRef) + { + var request = MakeRequest(orgRequest); + + if (request.LeadId == null) + { + throw new FaultException("Required field 'LeadId' is missing"); + } + + var leadMetadata = metadata.EntityMetadata.GetMetadata(request.LeadId.LogicalName); + + if (leadMetadata == null) + { + throw new FaultException($"The entity with a name = '{request.LeadId.LogicalName}' was not found in the MetadataCache."); + } + + if (request.OpportunityCurrencyId != null) + { + var currencyMetadata = metadata.EntityMetadata.GetMetadata(request.OpportunityCurrencyId.LogicalName); + if (currencyMetadata == null) + { + throw new FaultException($"The entity with a name = '{request.OpportunityCurrencyId.LogicalName}' was not found in the MetadataCache."); + } + } + + EntityMetadata customerMetadata = null; + if (request.OpportunityCustomerId != null) + { + customerMetadata = metadata.EntityMetadata.GetMetadata(request.OpportunityCustomerId.LogicalName); + if (customerMetadata == null) + { + throw new FaultException($"The entity with a name = '{request.OpportunityCustomerId.LogicalName}' was not found in the MetadataCache."); + } + } + + var lead = db.GetDbRow(request.LeadId); + + if (lead == null || request.LeadId.LogicalName != "lead") + { + throw new FaultException($"lead With Id = {request.LeadId.Id} Does Not Exist"); + } + + var currentState = lead.GetColumn("statecode"); + + if (currentState == 1) + { + throw new FaultException("The lead is already closed."); + } + + var status = request.Status != null ? request.Status.Value : 0; + var statusOptionMetadata = Utility.GetStatusOptionMetadata(leadMetadata) + .FirstOrDefault(s => s.Value == status) as StatusOptionMetadata; + + if (statusOptionMetadata == null) + { + throw new FaultException($"{status} is not a valid status code on lead with Id {request.LeadId.Id}"); + } + + if (statusOptionMetadata.State != 1) + { + throw new FaultException($"{request.Status.Value} is not a valid status code for state code LeadState.Qualified lead with Id {request.LeadId.Id}"); + } + + var createdEntities = new EntityReferenceCollection(); + + if (request.CreateAccount) + { + var accountAlreadyExists = db["account"].Any(row => row["name"] != null && row["name"].Equals(lead["fullname"])); + + if (accountAlreadyExists) + { + throw new FaultException("A record was not created or updated because a duplicate of the current record already exists."); + } + + var account = new Entity + { + Id = Guid.NewGuid(), + LogicalName = "account", + }; + + createdEntities.Add(account.ToEntityReference()); + } + + if (request.CreateContact) + { + var contactAlreadyExists = db["contact"].Any(row => row["fullname"] != null && row["fullname"].Equals(lead["fullname"])); + + if (contactAlreadyExists) + { + throw new FaultException("A record was not created or updated because a duplicate of the current record already exists."); + } + + var contact = new Entity + { + Id = Guid.NewGuid(), + LogicalName = "contact", + }; + + createdEntities.Add(contact.ToEntityReference()); + } + + if (request.CreateOpportunity) + { + if (request.OpportunityCustomerId != null) + { + if (request.OpportunityCustomerId.LogicalName != "account" && + request.OpportunityCustomerId.LogicalName != "contact") + { + throw new FaultException($"CustomerIdType for opportunity can either be an account or contact."); + } + + var customer = db.GetDbRow(request.OpportunityCustomerId); + + if (customer == null) + { + throw new FaultException($"{customerMetadata.DisplayName} With Id = {request.OpportunityCustomerId.Id} Does Not Exist"); + } + } + + var opportunity = new Entity + { + Id = Guid.NewGuid(), + LogicalName = "opportunity" + }; + + createdEntities.Add(opportunity.ToEntityReference()); + } + + var resp = new QualifyLeadResponse(); + resp.Results["CreatedEntities"] = createdEntities; + return resp; + } + } +} \ No newline at end of file diff --git a/src/XrmMockupShared/Utility.cs b/src/XrmMockupShared/Utility.cs index ce864655..e61f3773 100644 --- a/src/XrmMockupShared/Utility.cs +++ b/src/XrmMockupShared/Utility.cs @@ -304,13 +304,6 @@ internal static void CheckStatusTransitions(EntityMetadata metadata, Entity newE throw new FaultException($"Trying to switch {newEntity.LogicalName} from status {prevValue.Value} to {newValue.Value}"); } - internal static OptionMetadataCollection GetStatusOptionMetadata(EntityMetadata metadata) - { - return (metadata.Attributes - .FirstOrDefault(a => a is StatusAttributeMetadata) as StatusAttributeMetadata) - .OptionSet.Options; - } - internal static bool IsValidStatusTransition(string transitionData, int newStatusCode) { var ns = XNamespace.Get("http://schemas.microsoft.com/crm/2009/WebServices"); @@ -324,6 +317,12 @@ internal static bool IsValidStatusTransition(string transitionData, int newStatu return false; } #endif + internal static OptionMetadataCollection GetStatusOptionMetadata(EntityMetadata metadata) + { + return (metadata.Attributes + .FirstOrDefault(a => a is StatusAttributeMetadata) as StatusAttributeMetadata) + .OptionSet.Options; + } internal static EntityReference GetBaseCurrency(MetadataSkeleton metadata) { diff --git a/src/XrmMockupShared/XrmMockupShared.projitems b/src/XrmMockupShared/XrmMockupShared.projitems index b93828cd..5af81b98 100644 --- a/src/XrmMockupShared/XrmMockupShared.projitems +++ b/src/XrmMockupShared/XrmMockupShared.projitems @@ -17,6 +17,7 @@ + diff --git a/tests/SharedTests/SharedTests.projitems b/tests/SharedTests/SharedTests.projitems index 32986672..991787ff 100644 --- a/tests/SharedTests/SharedTests.projitems +++ b/tests/SharedTests/SharedTests.projitems @@ -12,6 +12,7 @@ + diff --git a/tests/SharedTests/TestLeads.cs b/tests/SharedTests/TestLeads.cs new file mode 100644 index 00000000..7f9d7aee --- /dev/null +++ b/tests/SharedTests/TestLeads.cs @@ -0,0 +1,646 @@ +using System; +using System.Text; +using System.Collections.Generic; +using DG.Some.Namespace; +using System.Linq; +using Microsoft.Xrm.Sdk; +using System.Diagnostics; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Microsoft.Xrm.Sdk.Query; +using System.ServiceModel; +using Microsoft.Xrm.Sdk.Messages; +using DG.XrmFramework.BusinessDomain.ServiceContext; +using Microsoft.Crm.Sdk.Messages; + +namespace DG.XrmMockupTest +{ + [TestClass] + public class TestLeads : UnitTestBase + { + [TestMethod] + public void TestQualifyLeadRequestLeadIdMissingFails() + { + var request = new QualifyLeadRequest + { + Status = new OptionSetValue((int)Lead_StatusCode.Qualified) + }; + try + { + orgAdminUIService.Execute(request); + Assert.Fail(); + } + catch (Exception e) + { + Assert.IsInstanceOfType(e, typeof(FaultException)); + } + } + + [TestMethod] + public void TestQualifyLeadRequestLeadIdNotExistingFails() + { + var lead = new Lead + { + FirstName = "Test", + LastName = "Lead", + Id = Guid.NewGuid() + }; + var request = new QualifyLeadRequest + { + Status = new OptionSetValue((int)Lead_StatusCode.Qualified), + LeadId = lead.ToEntityReference() + }; + try + { + orgAdminUIService.Execute(request); + Assert.Fail(); + } + catch (Exception e) + { + Assert.IsInstanceOfType(e, typeof(FaultException)); + } + } + + [TestMethod] + public void TestQualifyLeadRequestOpportunityCurrencyIdWrongLogicalNameFails() + { + var lead = new Lead + { + FirstName = "Test", + LastName = "Lead" + }; + + lead.Id = orgAdminUIService.Create(lead); + + var currency = new Entity + { + LogicalName = "Wrong Logical Name", + Id = Guid.NewGuid() + }; + + var request = new QualifyLeadRequest + { + Status = new OptionSetValue((int)Lead_StatusCode.Qualified), + LeadId = lead.ToEntityReference(), + OpportunityCurrencyId = currency.ToEntityReference() + }; + try + { + orgAdminUIService.Execute(request); + Assert.Fail(); + } + catch (Exception e) + { + Assert.IsInstanceOfType(e, typeof(FaultException)); + } + } + + [TestMethod] + public void TestQualifyLeadRequestOpportunityCustomerIdNotExistingFails() + { + var lead = new Lead + { + FirstName = "Test", + LastName = "Lead" + }; + + lead.Id = orgAdminUIService.Create(lead); + + var customer = new Account() + { + Id = Guid.NewGuid() + }; + + var request = new QualifyLeadRequest + { + Status = new OptionSetValue((int)Lead_StatusCode.Qualified), + LeadId = lead.ToEntityReference(), + CreateOpportunity = true, + OpportunityCustomerId = customer.ToEntityReference() + }; + try + { + orgAdminUIService.Execute(request); + Assert.Fail(); + } + catch (Exception e) + { + Assert.IsInstanceOfType(e, typeof(FaultException)); + } + } + + [TestMethod] + public void TestQualifyLeadRequestLeadNotExistingFails() + { + var lead = new Lead + { + FirstName = "Test", + LastName = "Lead", + Id = Guid.NewGuid() + }; + + var request = new QualifyLeadRequest + { + Status = new OptionSetValue((int)Lead_StatusCode.Qualified), + LeadId = lead.ToEntityReference() + }; + try + { + orgAdminUIService.Execute(request); + Assert.Fail(); + } + catch (Exception e) + { + Assert.IsInstanceOfType(e, typeof(FaultException)); + } + } + + [TestMethod] + public void TestQualifyLeadRequestLeadWrongLogicalNameFails() + { + var account = new Account(); + + account.Id = orgAdminUIService.Create(account); + + var request = new QualifyLeadRequest + { + Status = new OptionSetValue((int)Lead_StatusCode.Qualified), + LeadId = account.ToEntityReference(), + }; + try + { + orgAdminUIService.Execute(request); + Assert.Fail(); + } + catch (Exception e) + { + Assert.IsInstanceOfType(e, typeof(FaultException)); + } + } + +#if !(XRM_MOCKUP_TEST_2011 || XRM_MOCKUP_TEST_2013) + [TestMethod] + public void TestQualifyLeadRequestLeadAlreadyClosedFails() + { + var lead = new Lead + { + FirstName = "Test", + LastName = "Lead" + }; + + lead.Id = orgAdminUIService.Create(lead); + lead.SetState(orgAdminUIService, LeadState.Qualified, Lead_StatusCode.Qualified); + + var request = new QualifyLeadRequest + { + Status = new OptionSetValue((int)Lead_StatusCode.Qualified), + LeadId = lead.ToEntityReference() + }; + try + { + orgAdminUIService.Execute(request); + Assert.Fail(); + } + catch (Exception e) + { + Assert.IsInstanceOfType(e, typeof(FaultException)); + } + } +#else + [TestMethod] + public void TestQualifyLeadRequestLeadAlreadyClosedFails() + { + var lead = new Lead + { + FirstName = "Test", + LastName = "Lead" + }; + + lead.Id = orgAdminUIService.Create(lead); + lead.StateCode = LeadState.Qualified; + lead.StatusCode = Lead_StatusCode.Qualified; + + orgAdminUIService.Update(lead); + + var request = new QualifyLeadRequest + { + Status = new OptionSetValue((int)Lead_StatusCode.Qualified), + LeadId = lead.ToEntityReference() + }; + try + { + orgAdminUIService.Execute(request); + Assert.Fail(); + } + catch (Exception e) + { + Assert.IsInstanceOfType(e, typeof(FaultException)); + } + } +#endif + + [TestMethod] + public void TestQualifyLeadRequestInvalidStatusCodeFails() + { + var lead = new Lead + { + FirstName = "Test", + LastName = "Lead" + }; + + lead.Id = orgAdminUIService.Create(lead); + + var request = new QualifyLeadRequest + { + Status = new OptionSetValue(9999), + LeadId = lead.ToEntityReference() + }; + try + { + orgAdminUIService.Execute(request); + Assert.Fail(); + } + catch (Exception e) + { + Assert.IsInstanceOfType(e, typeof(FaultException)); + } + } + + [TestMethod] + public void TestQualifyLeadRequestStateNotQualifiedFails() + { + var lead = new Lead + { + FirstName = "Test", + LastName = "Lead" + }; + + lead.Id = orgAdminUIService.Create(lead); + + var request = new QualifyLeadRequest + { + Status = new OptionSetValue((int)Lead_StatusCode.Canceled), + LeadId = lead.ToEntityReference() + }; + try + { + orgAdminUIService.Execute(request); + Assert.Fail(); + } + catch (Exception e) + { + Assert.IsInstanceOfType(e, typeof(FaultException)); + } + } + + [TestMethod] + public void TestQualifyLeadRequestCreateAccountAccountAlreadyExistsFails() + { + var lead = new Lead + { + FirstName = "Test", + LastName = "Lead" + }; + + lead.Id = orgAdminUIService.Create(lead); + + var account = new Account + { + Name = "Test Lead" + }; + + account.Id = orgAdminUIService.Create(account); + + var request = new QualifyLeadRequest + { + Status = new OptionSetValue((int)Lead_StatusCode.Qualified), + CreateAccount = true, + LeadId = lead.ToEntityReference() + }; + try + { + orgAdminUIService.Execute(request); + Assert.Fail(); + } + catch (Exception e) + { + Assert.IsInstanceOfType(e, typeof(FaultException)); + } + } + + [TestMethod] + public void TestQualifyLeadRequestCreateContactContactAlreadyExistsFails() + { + var lead = new Lead + { + FirstName = "Test", + LastName = "Lead" + }; + + lead.Id = orgAdminUIService.Create(lead); + + var contact = new Contact + { + FirstName = "Test", + LastName = "Lead" + }; + + contact.Id = orgAdminUIService.Create(contact); + + var request = new QualifyLeadRequest + { + Status = new OptionSetValue((int)Lead_StatusCode.Qualified), + CreateContact = true, + LeadId = lead.ToEntityReference() + }; + try + { + orgAdminUIService.Execute(request); + Assert.Fail(); + } + catch (Exception e) + { + Assert.IsInstanceOfType(e, typeof(FaultException)); + } + } + + [TestMethod] + public void TestQualifyLeadRequestCreateOpportunityOpportunityCustomerIdWrongLogicalNameFails() + { + var lead = new Lead + { + FirstName = "Test", + LastName = "Lead" + }; + + lead.Id = orgAdminUIService.Create(lead); + + var request = new QualifyLeadRequest + { + Status = new OptionSetValue((int)Lead_StatusCode.Qualified), + CreateOpportunity = true, + OpportunityCustomerId = lead.ToEntityReference(), + LeadId = lead.ToEntityReference() + }; + try + { + orgAdminUIService.Execute(request); + Assert.Fail(); + } + catch (Exception e) + { + Assert.IsInstanceOfType(e, typeof(FaultException)); + } + } + + [TestMethod] + public void TestQualifyLeadRequestCreateOpportunityOpportunityCustomerIdDoesNotExistFails() + { + var lead = new Lead + { + FirstName = "Test", + LastName = "Lead" + }; + + lead.Id = orgAdminUIService.Create(lead); + + var contact = new Contact + { + FirstName = "Test", + LastName = "Lead", + Id = Guid.NewGuid() + }; + + var request = new QualifyLeadRequest + { + Status = new OptionSetValue((int)Lead_StatusCode.Qualified), + CreateOpportunity = true, + OpportunityCustomerId = contact.ToEntityReference(), + LeadId = lead.ToEntityReference() + }; + try + { + orgAdminUIService.Execute(request); + Assert.Fail(); + } + catch (Exception e) + { + Assert.IsInstanceOfType(e, typeof(FaultException)); + } + } + + [TestMethod] + public void TestQualifyLeadRequestSuccess() + { + var lead = new Lead + { + FirstName = "Test", + LastName = "Lead" + }; + + lead.Id = orgAdminUIService.Create(lead); + + var request = new QualifyLeadRequest + { + Status = new OptionSetValue((int)Lead_StatusCode.Qualified), + LeadId = lead.ToEntityReference() + }; + + var response = orgAdminUIService.Execute(request) as QualifyLeadResponse; + Assert.IsNotNull(response); + var createdEntities = response.CreatedEntities; + Assert.IsNotNull(createdEntities); + Assert.IsTrue(createdEntities.Count == 0); + } + + [TestMethod] + public void TestQualifyLeadRequestOpportunityCurrencyIdSuccess() + { + var lead = new Lead + { + FirstName = "Test", + LastName = "Lead" + }; + var currency = new TransactionCurrency() + { + ExchangeRate = 0.5m, + CurrencyPrecision = 2 + }; + + lead.Id = orgAdminUIService.Create(lead); + currency.Id = orgAdminUIService.Create(currency); + + var request = new QualifyLeadRequest + { + Status = new OptionSetValue((int)Lead_StatusCode.Qualified), + LeadId = lead.ToEntityReference(), + OpportunityCurrencyId = currency.ToEntityReference() + }; + + var response = orgAdminUIService.Execute(request) as QualifyLeadResponse; + Assert.IsNotNull(response); + var createdEntities = response.CreatedEntities; + Assert.IsNotNull(createdEntities); + Assert.IsTrue(createdEntities.Count == 0); + } + + [TestMethod] + public void TestQualifyLeadRequestOpportunityCustomerIdSuccess() + { + var lead = new Lead + { + FirstName = "Test", + LastName = "Lead" + }; + var account = new Account(); + lead.Id = orgAdminUIService.Create(lead); + account.Id = orgAdminUIService.Create(account); + + var request = new QualifyLeadRequest + { + Status = new OptionSetValue((int)Lead_StatusCode.Qualified), + LeadId = lead.ToEntityReference(), + OpportunityCustomerId = account.ToEntityReference() + }; + + var response = orgAdminUIService.Execute(request) as QualifyLeadResponse; + Assert.IsNotNull(response); + var createdEntities = response.CreatedEntities; + Assert.IsNotNull(createdEntities); + Assert.IsTrue(createdEntities.Count == 0); + } + + [TestMethod] + public void TestQualifyLeadRequestCreateAccountSuccess() + { + var lead = new Lead + { + FirstName = "Test", + LastName = "Lead" + }; + lead.Id = orgAdminUIService.Create(lead); + + var request = new QualifyLeadRequest + { + Status = new OptionSetValue((int)Lead_StatusCode.Qualified), + LeadId = lead.ToEntityReference(), + CreateAccount = true + }; + + var response = orgAdminUIService.Execute(request) as QualifyLeadResponse; + Assert.IsNotNull(response); + var createdEntities = response.CreatedEntities; + Assert.IsNotNull(createdEntities); + Assert.IsTrue(createdEntities.Count == 1); + } + + [TestMethod] + public void TestQualifyLeadRequestCreateContactSuccess() + { + var lead = new Lead + { + FirstName = "Test", + LastName = "Lead" + }; + lead.Id = orgAdminUIService.Create(lead); + + var request = new QualifyLeadRequest + { + Status = new OptionSetValue((int)Lead_StatusCode.Qualified), + LeadId = lead.ToEntityReference(), + CreateContact = true + }; + + var response = orgAdminUIService.Execute(request) as QualifyLeadResponse; + Assert.IsNotNull(response); + var createdEntities = response.CreatedEntities; + Assert.IsNotNull(createdEntities); + Assert.IsTrue(createdEntities.Count == 1); + } + + [TestMethod] + public void TestQualifyLeadRequestCreateOpportunityAccountSuccess() + { + var lead = new Lead + { + FirstName = "Test", + LastName = "Lead" + }; + lead.Id = orgAdminUIService.Create(lead); + + var account = new Account(); + account.Id = orgAdminUIService.Create(account); + + var request = new QualifyLeadRequest + { + Status = new OptionSetValue((int)Lead_StatusCode.Qualified), + LeadId = lead.ToEntityReference(), + CreateOpportunity = true, + OpportunityCustomerId = account.ToEntityReference() + }; + + var response = orgAdminUIService.Execute(request) as QualifyLeadResponse; + Assert.IsNotNull(response); + var createdEntities = response.CreatedEntities; + Assert.IsNotNull(createdEntities); + Assert.IsTrue(createdEntities.Count == 1); + } + + [TestMethod] + public void TestQualifyLeadRequestCreateOpportunityContactSuccess() + { + var lead = new Lead + { + FirstName = "Test", + LastName = "Lead" + }; + lead.Id = orgAdminUIService.Create(lead); + + var contact = new Contact(); + contact.Id = orgAdminUIService.Create(contact); + + var request = new QualifyLeadRequest + { + Status = new OptionSetValue((int)Lead_StatusCode.Qualified), + LeadId = lead.ToEntityReference(), + CreateOpportunity = true, + OpportunityCustomerId = contact.ToEntityReference() + }; + + var response = orgAdminUIService.Execute(request) as QualifyLeadResponse; + Assert.IsNotNull(response); + var createdEntities = response.CreatedEntities; + Assert.IsNotNull(createdEntities); + Assert.IsTrue(createdEntities.Count == 1); + } + + [TestMethod] + public void TestQualifyLeadRequestCreateAllSuccess() + { + var lead = new Lead + { + FirstName = "Test", + LastName = "Lead" + }; + lead.Id = orgAdminUIService.Create(lead); + + var contact = new Contact(); + contact.Id = orgAdminUIService.Create(contact); + + var request = new QualifyLeadRequest + { + Status = new OptionSetValue((int)Lead_StatusCode.Qualified), + LeadId = lead.ToEntityReference(), + CreateOpportunity = true, + CreateAccount = true, + CreateContact = true, + OpportunityCustomerId = contact.ToEntityReference() + }; + + var response = orgAdminUIService.Execute(request) as QualifyLeadResponse; + Assert.IsNotNull(response); + var createdEntities = response.CreatedEntities; + Assert.IsNotNull(createdEntities); + Assert.IsTrue(createdEntities.Count == 3); + } + } +} From f017e950cbc6839aa54c4165c474be04c079f162 Mon Sep 17 00:00:00 2001 From: Niels Ladekarl Date: Mon, 16 Apr 2018 16:28:40 +0200 Subject: [PATCH 2/4] moved if preprocessor inside test --- tests/SharedTests/TestLeads.cs | 32 +++----------------------------- 1 file changed, 3 insertions(+), 29 deletions(-) diff --git a/tests/SharedTests/TestLeads.cs b/tests/SharedTests/TestLeads.cs index 7f9d7aee..b53ddcc1 100644 --- a/tests/SharedTests/TestLeads.cs +++ b/tests/SharedTests/TestLeads.cs @@ -177,7 +177,6 @@ public void TestQualifyLeadRequestLeadWrongLogicalNameFails() } } -#if !(XRM_MOCKUP_TEST_2011 || XRM_MOCKUP_TEST_2013) [TestMethod] public void TestQualifyLeadRequestLeadAlreadyClosedFails() { @@ -188,38 +187,14 @@ public void TestQualifyLeadRequestLeadAlreadyClosedFails() }; lead.Id = orgAdminUIService.Create(lead); - lead.SetState(orgAdminUIService, LeadState.Qualified, Lead_StatusCode.Qualified); - var request = new QualifyLeadRequest - { - Status = new OptionSetValue((int)Lead_StatusCode.Qualified), - LeadId = lead.ToEntityReference() - }; - try - { - orgAdminUIService.Execute(request); - Assert.Fail(); - } - catch (Exception e) - { - Assert.IsInstanceOfType(e, typeof(FaultException)); - } - } +#if (XRM_MOCKUP_TEST_2011 || XRM_MOCKUP_TEST_2013) + lead.SetState(orgAdminUIService, LeadState.Qualified, Lead_StatusCode.Qualified); #else - [TestMethod] - public void TestQualifyLeadRequestLeadAlreadyClosedFails() - { - var lead = new Lead - { - FirstName = "Test", - LastName = "Lead" - }; - - lead.Id = orgAdminUIService.Create(lead); lead.StateCode = LeadState.Qualified; lead.StatusCode = Lead_StatusCode.Qualified; - orgAdminUIService.Update(lead); +#endif var request = new QualifyLeadRequest { @@ -236,7 +211,6 @@ public void TestQualifyLeadRequestLeadAlreadyClosedFails() Assert.IsInstanceOfType(e, typeof(FaultException)); } } -#endif [TestMethod] public void TestQualifyLeadRequestInvalidStatusCodeFails() From dd351521740f669abff6313fe27943c9cd58ff35 Mon Sep 17 00:00:00 2001 From: Niels Ladekarl Date: Fri, 10 Aug 2018 16:57:02 +0200 Subject: [PATCH 3/4] Added changes in db and copying attributes from lead to created entities --- .../Requests/QualifyLeadRequestHandler.cs | 236 +++++++--- src/XrmMockupShared/Utility.cs | 1 + tests/SharedTests/TestLeads.cs | 405 +++++++++++++----- 3 files changed, 479 insertions(+), 163 deletions(-) diff --git a/src/XrmMockupShared/Requests/QualifyLeadRequestHandler.cs b/src/XrmMockupShared/Requests/QualifyLeadRequestHandler.cs index e91d9087..6c576b2d 100644 --- a/src/XrmMockupShared/Requests/QualifyLeadRequestHandler.cs +++ b/src/XrmMockupShared/Requests/QualifyLeadRequestHandler.cs @@ -2,12 +2,12 @@ using DG.Tools.XrmMockup.Database; using Microsoft.Crm.Sdk.Messages; using Microsoft.Xrm.Sdk; +using Microsoft.Xrm.Sdk.Messages; using Microsoft.Xrm.Sdk.Metadata; using System; using System.Collections.Generic; using System.Linq; using System.ServiceModel; -using System.Text; namespace DG.Tools.XrmMockup { @@ -52,18 +52,18 @@ internal override OrganizationResponse Execute(OrganizationRequest orgRequest, E } } - var lead = db.GetDbRow(request.LeadId); + var leadRow = db.GetDbRow(request.LeadId); - if (lead == null || request.LeadId.LogicalName != "lead") + if (leadRow == null || request.LeadId.LogicalName != LogicalNames.Lead) { - throw new FaultException($"lead With Id = {request.LeadId.Id} Does Not Exist"); + throw new FaultException($"{LogicalNames.Lead} With Id = {request.LeadId.Id} Does Not Exist"); } - var currentState = lead.GetColumn("statecode"); + var currentState = leadRow.GetColumn("statecode"); if (currentState == 1) { - throw new FaultException("The lead is already closed."); + throw new FaultException($"The {LogicalNames.Lead} is already closed."); } var status = request.Status != null ? request.Status.Value : 0; @@ -82,72 +82,198 @@ internal override OrganizationResponse Execute(OrganizationRequest orgRequest, E var createdEntities = new EntityReferenceCollection(); + var lead = leadRow.ToEntity(); + if (request.CreateAccount) { - var accountAlreadyExists = db["account"].Any(row => row["name"] != null && row["name"].Equals(lead["fullname"])); - - if (accountAlreadyExists) - { - throw new FaultException("A record was not created or updated because a duplicate of the current record already exists."); - } - - var account = new Entity - { - Id = Guid.NewGuid(), - LogicalName = "account", - }; - - createdEntities.Add(account.ToEntityReference()); + createdEntities.Add(CreateAccountFromLead(lead, userRef)); } if (request.CreateContact) { - var contactAlreadyExists = db["contact"].Any(row => row["fullname"] != null && row["fullname"].Equals(lead["fullname"])); + createdEntities.Add(CreateContactFromLead(lead, userRef)); + } - if (contactAlreadyExists) - { - throw new FaultException("A record was not created or updated because a duplicate of the current record already exists."); - } + if (request.CreateOpportunity) + { + createdEntities.Add(CreateOpportunityFromLead(lead, userRef, request.OpportunityCustomerId, request.OpportunityCurrencyId)); + } - var contact = new Entity - { - Id = Guid.NewGuid(), - LogicalName = "contact", - }; + lead["statuscode"] = status; + db.Update(lead); - createdEntities.Add(contact.ToEntityReference()); - } + var resp = new QualifyLeadResponse(); + resp.Results["CreatedEntities"] = createdEntities; + return resp; + } - if (request.CreateOpportunity) + private EntityReference CreateOpportunityFromLead(Entity lead, EntityReference userRef, EntityReference customer, EntityReference currency) + { + if (customer != null) { - if (request.OpportunityCustomerId != null) + if (customer.LogicalName != LogicalNames.Account && + customer.LogicalName != LogicalNames.Contact) { - if (request.OpportunityCustomerId.LogicalName != "account" && - request.OpportunityCustomerId.LogicalName != "contact") - { - throw new FaultException($"CustomerIdType for opportunity can either be an account or contact."); - } - - var customer = db.GetDbRow(request.OpportunityCustomerId); - - if (customer == null) - { - throw new FaultException($"{customerMetadata.DisplayName} With Id = {request.OpportunityCustomerId.Id} Does Not Exist"); - } + throw new FaultException($"CustomerIdType for {LogicalNames.Opportunity} can either be an {LogicalNames.Account} or {LogicalNames.Contact}"); } + } - var opportunity = new Entity - { - Id = Guid.NewGuid(), - LogicalName = "opportunity" - }; + var opportunity = new Entity(LogicalNames.Opportunity); + if (lead.Attributes.Contains("subject")) + opportunity["name"] = lead["subject"]; + if (lead.Attributes.Contains("qualificationcomments")) + opportunity["qualificationcomments"] = lead["qualificationcomments"]; + if (lead.Attributes.Contains("description")) + opportunity["description"] = lead["description"]; + opportunity["originatingleadid"] = lead.ToEntityReference(); - createdEntities.Add(opportunity.ToEntityReference()); + if(customer != null) + { + opportunity["customerid"] = customer; } - var resp = new QualifyLeadResponse(); - resp.Results["CreatedEntities"] = createdEntities; - return resp; + if(currency != null) + { + opportunity["transactioncurrencyid"] = currency; + } + + opportunity.Id = CreateEntity(opportunity, userRef); + return opportunity.ToEntityReference(); + } + + private EntityReference CreateContactFromLead(Entity lead, EntityReference userRef) + { + var contact = new Entity(LogicalNames.Contact); + if (lead.Attributes.Contains("mobilephone")) + contact["mobilephone"] = lead["mobilephone"]; + if (lead.Attributes.Contains("emailaddress1")) + contact["emailaddress1"] = lead["emailaddress1"]; + if (lead.Attributes.Contains("emailaddress3")) + contact["emailaddress3"] = lead["emailaddress3"]; + if (lead.Attributes.Contains("websiteurl")) + contact["websiteurl"] = lead["websiteurl"]; + if (lead.Attributes.Contains("yomilastname")) + contact["yomilastname"] = lead["yomilastname"]; + if (lead.Attributes.Contains("lastname")) + contact["lastname"] = lead["lastname"]; + if (lead.Attributes.Contains("donotpostalmail")) + contact["donotpostalmail"] = lead["donotpostalmail"]; + if (lead.Attributes.Contains("donotphone")) + contact["donotphone"] = lead["donotphone"]; + if (lead.Attributes.Contains("yomimiddlename")) + contact["yomimiddlename"] = lead["yomimiddlename"]; + if (lead.Attributes.Contains("description")) + contact["description"] = lead["description"]; + if (lead.Attributes.Contains("firstname")) + contact["firstname"] = lead["firstname"]; + if (lead.Attributes.Contains("donotemail")) + contact["donotemail"] = lead["donotemail"]; + if (lead.Attributes.Contains("address1_stateorprovince")) + contact["address1_stateorprovince"] = lead["address1_stateorprovince"]; + if (lead.Attributes.Contains("address2_county")) + contact["address2_county"] = lead["address2_county"]; + if (lead.Attributes.Contains("donotfax")) + contact["donotfax"] = lead["donotfax"]; + if (lead.Attributes.Contains("donotsendmm")) + contact["donotsendmm"] = lead["donotsendmm"]; + if (lead.Attributes.Contains("jobtitle")) + contact["jobtitle"] = lead["jobtitle"]; + if (lead.Attributes.Contains("pager")) + contact["pager"] = lead["pager"]; + if (lead.Attributes.Contains("address1_country")) + contact["address1_country"] = lead["address1_country"]; + if (lead.Attributes.Contains("address1_line1")) + contact["address1_line1"] = lead["address1_line1"]; + if (lead.Attributes.Contains("address1_line2")) + contact["address1_line2"] = lead["address1_line2"]; + if (lead.Attributes.Contains("address1_line3")) + contact["address1_line3"] = lead["address1_line3"]; + if (lead.Attributes.Contains("telephone2")) + contact["telephone2"] = lead["telephone2"]; + if (lead.Attributes.Contains("telephone3")) + contact["telephone3"] = lead["telephone3"]; + if (lead.Attributes.Contains("address2_fax")) + contact["address2_fax"] = lead["address2_fax"]; + if (lead.Attributes.Contains("donotbulkemail")) + contact["donotbulkemail"] = lead["donotbulkemail"]; + if (lead.Attributes.Contains("emailaddress2")) + contact["emailaddress2"] = lead["emailaddress2"]; + if (lead.Attributes.Contains("fax")) + contact["fax"] = lead["fax"]; + if (lead.Attributes.Contains("yomifirstname")) + contact["yomifirstname"] = lead["yomifirstname"]; + if (lead.Attributes.Contains("address1_postalcode")) + contact["address1_postalcode"] = lead["address1_postalcode"]; + if (lead.Attributes.Contains("address1_city")) + contact["address1_city"] = lead["address1_city"]; + if (lead.Attributes.Contains("address2_country")) + contact["address2_country"] = lead["address2_country"]; + contact["originatingleadid"] = lead.ToEntityReference(); + + contact.Id = CreateEntity(contact, userRef); + return contact.ToEntityReference(); + } + + private EntityReference CreateAccountFromLead(Entity lead, EntityReference userRef) + { + var account = new Entity(LogicalNames.Account); + + if(lead.Attributes.Contains("sic")) + account["sic"] = lead["sic"]; + if(lead.Attributes.Contains("emailaddress1")) + account["emailaddress1"] = lead["emailaddress1"]; + if(lead.Attributes.Contains("companyname")) + account["name"] = lead["companyname"]; + if (lead.Attributes.Contains("fax")) + account["fax"] = lead["fax"]; + if (lead.Attributes.Contains("websiteurl")) + account["websiteurl"] = lead["websiteurl"]; + if (lead.Attributes.Contains("address1_country")) + account["address1_country"] = lead["address1_country"]; + if (lead.Attributes.Contains("address1_city")) + account["address1_city"] = lead["address1_city"]; + if (lead.Attributes.Contains("address1_line1")) + account["address1_line1"] = lead["address1_line1"]; + if (lead.Attributes.Contains("address1_line2")) + account["address1_line2"] = lead["address1_line2"]; + if (lead.Attributes.Contains("address1_line3")) + account["address1_line3"] = lead["address1_line3"]; + if (lead.Attributes.Contains("address1_postalcode")) + account["address1_postalcode"] = lead["address1_postalcode"]; + if (lead.Attributes.Contains("address1_stateorprovince")) + account["address1_stateorprovince"] = lead["address1_stateorprovince"]; + if (lead.Attributes.Contains("telephone2")) + account["telephone2"] = lead["telephone2"]; + if (lead.Attributes.Contains("donotpostalmail")) + account["donotpostalmail"] = lead["donotpostalmail"]; + if (lead.Attributes.Contains("donotphone")) + account["donotphone"] = lead["donotphone"]; + if (lead.Attributes.Contains("donotfax")) + account["donotfax"] = lead["donotfax"]; + if (lead.Attributes.Contains("donotsendmm")) + account["donotsendmm"] = lead["donotsendmm"]; + if (lead.Attributes.Contains("description")) + account["description"] = lead["description"]; + if (lead.Attributes.Contains("donotemail")) + account["donotemail"] = lead["donotemail"]; + if (lead.Attributes.Contains("yominame")) + account["yominame"] = lead["yominame"]; + if (lead.Attributes.Contains("donotbulkemail")) + account["donotbulkemail"] = lead["donotbulkemail"]; + account["originatingleadid"] = lead.ToEntityReference(); + + account.Id = CreateEntity(account, userRef); + return account.ToEntityReference(); + } + + private Guid CreateEntity(Entity entity, EntityReference userRef) + { + var req = new CreateRequest + { + Target = entity + }; + var response = core.Execute(req, userRef) as CreateResponse; + return response.id; } } } \ No newline at end of file diff --git a/src/XrmMockupShared/Utility.cs b/src/XrmMockupShared/Utility.cs index eeafb0a3..179f414c 100644 --- a/src/XrmMockupShared/Utility.cs +++ b/src/XrmMockupShared/Utility.cs @@ -921,6 +921,7 @@ internal static Entity ToActivityPointer(this Entity entity) internal class LogicalNames { + public const string Account = "account"; public const string TransactionCurrency = "transactioncurrency"; public const string BusinessUnit = "businessunit"; public const string SystemUser = "systemuser"; diff --git a/tests/SharedTests/TestLeads.cs b/tests/SharedTests/TestLeads.cs index b53ddcc1..4668b914 100644 --- a/tests/SharedTests/TestLeads.cs +++ b/tests/SharedTests/TestLeads.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using DG.Some.Namespace; using System.Linq; +using DG.XrmMockupTest; using Microsoft.Xrm.Sdk; using System.Diagnostics; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -266,77 +267,6 @@ public void TestQualifyLeadRequestStateNotQualifiedFails() } } - [TestMethod] - public void TestQualifyLeadRequestCreateAccountAccountAlreadyExistsFails() - { - var lead = new Lead - { - FirstName = "Test", - LastName = "Lead" - }; - - lead.Id = orgAdminUIService.Create(lead); - - var account = new Account - { - Name = "Test Lead" - }; - - account.Id = orgAdminUIService.Create(account); - - var request = new QualifyLeadRequest - { - Status = new OptionSetValue((int)Lead_StatusCode.Qualified), - CreateAccount = true, - LeadId = lead.ToEntityReference() - }; - try - { - orgAdminUIService.Execute(request); - Assert.Fail(); - } - catch (Exception e) - { - Assert.IsInstanceOfType(e, typeof(FaultException)); - } - } - - [TestMethod] - public void TestQualifyLeadRequestCreateContactContactAlreadyExistsFails() - { - var lead = new Lead - { - FirstName = "Test", - LastName = "Lead" - }; - - lead.Id = orgAdminUIService.Create(lead); - - var contact = new Contact - { - FirstName = "Test", - LastName = "Lead" - }; - - contact.Id = orgAdminUIService.Create(contact); - - var request = new QualifyLeadRequest - { - Status = new OptionSetValue((int)Lead_StatusCode.Qualified), - CreateContact = true, - LeadId = lead.ToEntityReference() - }; - try - { - orgAdminUIService.Execute(request); - Assert.Fail(); - } - catch (Exception e) - { - Assert.IsInstanceOfType(e, typeof(FaultException)); - } - } - [TestMethod] public void TestQualifyLeadRequestCreateOpportunityOpportunityCustomerIdWrongLogicalNameFails() { @@ -424,16 +354,17 @@ public void TestQualifyLeadRequestSuccess() var createdEntities = response.CreatedEntities; Assert.IsNotNull(createdEntities); Assert.IsTrue(createdEntities.Count == 0); + + var qualifiedLead = orgAdminUIService.Retrieve(Lead.EntityLogicalName, lead.Id, new ColumnSet(true)) as Lead; + + Assert.IsNotNull(qualifiedLead); + Assert.AreEqual(Lead_StatusCode.Qualified, qualifiedLead.StatusCode); } [TestMethod] public void TestQualifyLeadRequestOpportunityCurrencyIdSuccess() { - var lead = new Lead - { - FirstName = "Test", - LastName = "Lead" - }; + var lead = GetLeadWithAttributes(); var currency = new TransactionCurrency() { ExchangeRate = 0.5m, @@ -455,16 +386,18 @@ public void TestQualifyLeadRequestOpportunityCurrencyIdSuccess() var createdEntities = response.CreatedEntities; Assert.IsNotNull(createdEntities); Assert.IsTrue(createdEntities.Count == 0); + + var qualifiedLead = orgAdminUIService.Retrieve(Lead.EntityLogicalName, lead.Id, new ColumnSet(true)) as Lead; + + Assert.IsNotNull(qualifiedLead); + Assert.AreEqual(Lead_StatusCode.Qualified, qualifiedLead.StatusCode); } [TestMethod] public void TestQualifyLeadRequestOpportunityCustomerIdSuccess() { - var lead = new Lead - { - FirstName = "Test", - LastName = "Lead" - }; + var lead = GetLeadWithAttributes(); + var account = new Account(); lead.Id = orgAdminUIService.Create(lead); account.Id = orgAdminUIService.Create(account); @@ -481,16 +414,17 @@ public void TestQualifyLeadRequestOpportunityCustomerIdSuccess() var createdEntities = response.CreatedEntities; Assert.IsNotNull(createdEntities); Assert.IsTrue(createdEntities.Count == 0); + + var qualifiedLead = orgAdminUIService.Retrieve(Lead.EntityLogicalName, lead.Id, new ColumnSet(true)) as Lead; + + Assert.IsNotNull(qualifiedLead); + Assert.AreEqual(Lead_StatusCode.Qualified, qualifiedLead.StatusCode); } [TestMethod] public void TestQualifyLeadRequestCreateAccountSuccess() { - var lead = new Lead - { - FirstName = "Test", - LastName = "Lead" - }; + var lead = GetLeadWithAttributes(); lead.Id = orgAdminUIService.Create(lead); var request = new QualifyLeadRequest @@ -505,23 +439,61 @@ public void TestQualifyLeadRequestCreateAccountSuccess() var createdEntities = response.CreatedEntities; Assert.IsNotNull(createdEntities); Assert.IsTrue(createdEntities.Count == 1); + + var qualifiedLead = orgAdminUIService.Retrieve(Lead.EntityLogicalName, lead.Id, new ColumnSet(true)) as Lead; + + Assert.IsNotNull(qualifiedLead); + Assert.AreEqual(Lead_StatusCode.Qualified, qualifiedLead.StatusCode); + + var accountId = createdEntities.First(e => e.LogicalName == Account.EntityLogicalName).Id; + var account = orgAdminUIService.Retrieve(Account.EntityLogicalName, accountId, new ColumnSet(true)) as Account; + + Assert.IsNotNull(account); + AssertAccountMatchesLead(account, lead); } [TestMethod] public void TestQualifyLeadRequestCreateContactSuccess() { - var lead = new Lead + var lead = GetLeadWithAttributes(); + lead.Id = orgAdminUIService.Create(lead); + + var request = new QualifyLeadRequest { - FirstName = "Test", - LastName = "Lead" + Status = new OptionSetValue((int)Lead_StatusCode.Qualified), + LeadId = lead.ToEntityReference(), + CreateContact = true }; + + var response = orgAdminUIService.Execute(request) as QualifyLeadResponse; + Assert.IsNotNull(response); + var createdEntities = response.CreatedEntities; + Assert.IsNotNull(createdEntities); + Assert.IsTrue(createdEntities.Count == 1); + + var qualifiedLead = orgAdminUIService.Retrieve(Lead.EntityLogicalName, lead.Id, new ColumnSet(true)) as Lead; + + Assert.IsNotNull(qualifiedLead); + Assert.AreEqual(Lead_StatusCode.Qualified, qualifiedLead.StatusCode); + + var contactId = createdEntities.First(e => e.LogicalName == Contact.EntityLogicalName).Id; + var contact = orgAdminUIService.Retrieve(Contact.EntityLogicalName, contactId, new ColumnSet(true)) as Contact; + + Assert.IsNotNull(contact); + AssertContactMatchesLead(contact, lead); + } + + [TestMethod] + public void TestQualifyLeadRequestCreateOpportunitySuccess() + { + var lead = GetLeadWithAttributes(); lead.Id = orgAdminUIService.Create(lead); var request = new QualifyLeadRequest { Status = new OptionSetValue((int)Lead_StatusCode.Qualified), LeadId = lead.ToEntityReference(), - CreateContact = true + CreateOpportunity = true }; var response = orgAdminUIService.Execute(request) as QualifyLeadResponse; @@ -529,16 +501,63 @@ public void TestQualifyLeadRequestCreateContactSuccess() var createdEntities = response.CreatedEntities; Assert.IsNotNull(createdEntities); Assert.IsTrue(createdEntities.Count == 1); + + var qualifiedLead = orgAdminUIService.Retrieve(Lead.EntityLogicalName, lead.Id, new ColumnSet(true)) as Lead; + + Assert.IsNotNull(qualifiedLead); + Assert.AreEqual(Lead_StatusCode.Qualified, qualifiedLead.StatusCode); + + var opportunityId = createdEntities.First(e => e.LogicalName == Opportunity.EntityLogicalName).Id; + var opportunity = orgAdminUIService.Retrieve(Opportunity.EntityLogicalName, opportunityId, new ColumnSet(true)) as Opportunity; + + Assert.IsNotNull(opportunity); + AssertOpportunityMatchesLead(opportunity, lead); } [TestMethod] - public void TestQualifyLeadRequestCreateOpportunityAccountSuccess() + public void TestQualifyLeadRequestCreateOpportunityCurrencySuccess() { - var lead = new Lead + var lead = GetLeadWithAttributes(); + lead.Id = orgAdminUIService.Create(lead); + + var currency = new TransactionCurrency() { - FirstName = "Test", - LastName = "Lead" + ExchangeRate = 0.5m, + CurrencyPrecision = 2 + }; + + currency.Id = orgAdminUIService.Create(currency); + + var request = new QualifyLeadRequest + { + Status = new OptionSetValue((int)Lead_StatusCode.Qualified), + LeadId = lead.ToEntityReference(), + CreateOpportunity = true, + OpportunityCurrencyId = currency.ToEntityReference() }; + + var response = orgAdminUIService.Execute(request) as QualifyLeadResponse; + Assert.IsNotNull(response); + var createdEntities = response.CreatedEntities; + Assert.IsNotNull(createdEntities); + Assert.IsTrue(createdEntities.Count == 1); + + var qualifiedLead = orgAdminUIService.Retrieve(Lead.EntityLogicalName, lead.Id, new ColumnSet(true)) as Lead; + + Assert.IsNotNull(qualifiedLead); + Assert.AreEqual(Lead_StatusCode.Qualified, qualifiedLead.StatusCode); + + var opportunityId = createdEntities.First(e => e.LogicalName == Opportunity.EntityLogicalName).Id; + var opportunity = orgAdminUIService.Retrieve(Opportunity.EntityLogicalName, opportunityId, new ColumnSet(true)) as Opportunity; + + Assert.IsNotNull(opportunity); + AssertOpportunityMatchesLead(opportunity, lead); + } + + [TestMethod] + public void TestQualifyLeadRequestCreateOpportunityCustomerAccountSuccess() + { + var lead = GetLeadWithAttributes(); lead.Id = orgAdminUIService.Create(lead); var account = new Account(); @@ -549,7 +568,7 @@ public void TestQualifyLeadRequestCreateOpportunityAccountSuccess() Status = new OptionSetValue((int)Lead_StatusCode.Qualified), LeadId = lead.ToEntityReference(), CreateOpportunity = true, - OpportunityCustomerId = account.ToEntityReference() + OpportunityCustomerId = account.ToEntityReference(), }; var response = orgAdminUIService.Execute(request) as QualifyLeadResponse; @@ -557,16 +576,23 @@ public void TestQualifyLeadRequestCreateOpportunityAccountSuccess() var createdEntities = response.CreatedEntities; Assert.IsNotNull(createdEntities); Assert.IsTrue(createdEntities.Count == 1); + + var qualifiedLead = orgAdminUIService.Retrieve(Lead.EntityLogicalName, lead.Id, new ColumnSet(true)) as Lead; + + Assert.IsNotNull(qualifiedLead); + Assert.AreEqual(Lead_StatusCode.Qualified, qualifiedLead.StatusCode); + + var opportunityId = createdEntities.First(e => e.LogicalName == Opportunity.EntityLogicalName).Id; + var opportunity = orgAdminUIService.Retrieve(Opportunity.EntityLogicalName, opportunityId, new ColumnSet(true)) as Opportunity; + + Assert.IsNotNull(opportunity); + AssertOpportunityMatchesLead(opportunity, lead, account); } [TestMethod] - public void TestQualifyLeadRequestCreateOpportunityContactSuccess() + public void TestQualifyLeadRequestCreateOpportunityCustomerContactSuccess() { - var lead = new Lead - { - FirstName = "Test", - LastName = "Lead" - }; + var lead = GetLeadWithAttributes(); lead.Id = orgAdminUIService.Create(lead); var contact = new Contact(); @@ -585,20 +611,35 @@ public void TestQualifyLeadRequestCreateOpportunityContactSuccess() var createdEntities = response.CreatedEntities; Assert.IsNotNull(createdEntities); Assert.IsTrue(createdEntities.Count == 1); + + var qualifiedLead = orgAdminUIService.Retrieve(Lead.EntityLogicalName, lead.Id, new ColumnSet(true)) as Lead; + + Assert.IsNotNull(qualifiedLead); + Assert.AreEqual(Lead_StatusCode.Qualified, qualifiedLead.StatusCode); + + var opportunityId = createdEntities.First(e => e.LogicalName == Opportunity.EntityLogicalName).Id; + var opportunity = orgAdminUIService.Retrieve(Opportunity.EntityLogicalName, opportunityId, new ColumnSet(true)) as Opportunity; + + Assert.IsNotNull(opportunity); + AssertOpportunityMatchesLead(opportunity, lead, contact); } [TestMethod] public void TestQualifyLeadRequestCreateAllSuccess() { - var lead = new Lead + var lead = GetLeadWithAttributes(); + lead.Id = orgAdminUIService.Create(lead); + + var customer = new Contact(); + customer.Id = orgAdminUIService.Create(customer); + + var currency = new TransactionCurrency() { - FirstName = "Test", - LastName = "Lead" + ExchangeRate = 0.5m, + CurrencyPrecision = 2 }; - lead.Id = orgAdminUIService.Create(lead); - var contact = new Contact(); - contact.Id = orgAdminUIService.Create(contact); + currency.Id = orgAdminUIService.Create(currency); var request = new QualifyLeadRequest { @@ -607,7 +648,8 @@ public void TestQualifyLeadRequestCreateAllSuccess() CreateOpportunity = true, CreateAccount = true, CreateContact = true, - OpportunityCustomerId = contact.ToEntityReference() + OpportunityCustomerId = customer.ToEntityReference(), + OpportunityCurrencyId = currency.ToEntityReference() }; var response = orgAdminUIService.Execute(request) as QualifyLeadResponse; @@ -615,6 +657,153 @@ public void TestQualifyLeadRequestCreateAllSuccess() var createdEntities = response.CreatedEntities; Assert.IsNotNull(createdEntities); Assert.IsTrue(createdEntities.Count == 3); + + var qualifiedLead = orgAdminUIService.Retrieve(Lead.EntityLogicalName, lead.Id, new ColumnSet(true)) as Lead; + + Assert.IsNotNull(qualifiedLead); + Assert.AreEqual(Lead_StatusCode.Qualified, qualifiedLead.StatusCode); + + var opportunityId = createdEntities.First(e => e.LogicalName == Opportunity.EntityLogicalName).Id; + var opportunity = orgAdminUIService.Retrieve(Opportunity.EntityLogicalName, opportunityId, new ColumnSet(true)) as Opportunity; + Assert.IsNotNull(opportunity); + AssertOpportunityMatchesLead(opportunity, lead, customer, currency); + + var contactId = createdEntities.First(e => e.LogicalName == Contact.EntityLogicalName).Id; + var contact = orgAdminUIService.Retrieve(Contact.EntityLogicalName, contactId, new ColumnSet(true)) as Contact; + AssertContactMatchesLead(contact, lead); + + var accountId = createdEntities.First(e => e.LogicalName == Account.EntityLogicalName).Id; + var account = orgAdminUIService.Retrieve(Account.EntityLogicalName, accountId, new ColumnSet(true)) as Account; + AssertAccountMatchesLead(account, lead); + } + + private Lead GetLeadWithAttributes() + { + return new Lead + { + FirstName = "Test", + LastName = "Lead", + Subject = "Subject", + SIC = "SIC", + EMailAddress1 = "EMailAddress1", + CompanyName = "CompanyName", + Fax = "Fax", + WebSiteUrl = "WebSiteUrl", + Address1_Country = "Address1_Country", + Address1_City = "Address1_City", + Address1_Line1 = "Address1_Line1", + Address1_Line2 = "Address1_Line2", + Address1_Line3 = "Address1_Line3", + Address1_PostalCode = "Address1_PostalCode", + Address1_StateOrProvince = "Address1_StateOrProvince", + Telephone2 = "Telephone2", + DoNotPostalMail = true, + DoNotPhone = true, + DoNotFax = true, + DoNotSendMM = true, + Description = "Description", + DoNotEMail = true, + YomiCompanyName = "YomiCompanyName", + DoNotBulkEMail = true, + MobilePhone = "MobilePhone", + EMailAddress3 = "EMailAddress3", + YomiLastName = "YomiLastName", + YomiMiddleName = "YomiMiddleName", + Address2_Country = "Address2_Country", + JobTitle = "JobTitle", + Pager = "Pager", + Telephone3 = "Telephone3", + Address2_Fax = "Address2_Fax", + EMailAddress2 = "EMailAddress2", + YomiFirstName = "YomiFirstName", + QualificationComments = "QualificationComments" + }; + } + + private void AssertOpportunityMatchesLead(Opportunity opportunity, Lead lead, Entity customer = null, TransactionCurrency currency = null) + { + Assert.AreEqual(lead.Subject, opportunity.Name); + Assert.AreEqual(lead.QualificationComments, opportunity.QualificationComments); + Assert.AreEqual(lead.Description, opportunity.Description); + Assert.IsNotNull(opportunity.OriginatingLeadId); + Assert.AreEqual(lead.LeadId, opportunity.OriginatingLeadId.Id); + + if(customer != null) + { + Assert.IsNotNull(opportunity.CustomerId); + Assert.AreEqual(customer.Id, opportunity.CustomerId.Id); + } + + if(currency != null) + { + Assert.IsNotNull(opportunity.TransactionCurrencyId); + Assert.AreEqual(currency.Id, opportunity.TransactionCurrencyId.Id); + } + } + + private void AssertContactMatchesLead(Contact contact, Lead lead) + { + Assert.AreEqual(lead.MobilePhone, contact.MobilePhone); + Assert.AreEqual(lead.EMailAddress1, contact.EMailAddress1); + Assert.AreEqual(lead.EMailAddress3, contact.EMailAddress3); + Assert.AreEqual(lead.WebSiteUrl, contact.WebSiteUrl); + Assert.AreEqual(lead.YomiLastName, contact.YomiLastName); + Assert.AreEqual(lead.LastName, contact.LastName); + Assert.AreEqual(lead.DoNotPostalMail, contact.DoNotPostalMail); + Assert.AreEqual(lead.DoNotPhone, contact.DoNotPhone); + Assert.AreEqual(lead.YomiMiddleName, contact.YomiMiddleName); + Assert.AreEqual(lead.Description, contact.Description); + Assert.AreEqual(lead.FirstName, contact.FirstName); + Assert.AreEqual(lead.DoNotEMail, contact.DoNotEMail); + Assert.AreEqual(lead.Address1_StateOrProvince, contact.Address1_StateOrProvince); + Assert.AreEqual(lead.Address2_Country, contact.Address2_Country); + Assert.AreEqual(lead.DoNotFax, contact.DoNotFax); + Assert.AreEqual(lead.DoNotSendMM, contact.DoNotSendMM); + Assert.AreEqual(lead.JobTitle, contact.JobTitle); + Assert.AreEqual(lead.Pager, contact.Pager); + Assert.AreEqual(lead.Address1_Country, contact.Address1_Country); + Assert.AreEqual(lead.Address1_Line1, contact.Address1_Line1); + Assert.AreEqual(lead.Address1_Line2, contact.Address1_Line2); + Assert.AreEqual(lead.Address1_Line3, contact.Address1_Line3); + Assert.AreEqual(lead.Telephone2, contact.Telephone2); + Assert.AreEqual(lead.Telephone3, contact.Telephone3); + Assert.AreEqual(lead.Address2_Fax, contact.Address2_Fax); + Assert.AreEqual(lead.DoNotBulkEMail, contact.DoNotBulkEMail); + Assert.AreEqual(lead.EMailAddress2, contact.EMailAddress2); + Assert.AreEqual(lead.Fax, contact.Fax); + Assert.AreEqual(lead.YomiFirstName, contact.YomiFirstName); + Assert.AreEqual(lead.Address1_PostalCode, contact.Address1_PostalCode); + Assert.AreEqual(lead.Address1_City, contact.Address1_City); + Assert.AreEqual(lead.Address2_Country, contact.Address2_Country); + Assert.IsNotNull(contact.OriginatingLeadId); + Assert.AreEqual(lead.Id, contact.OriginatingLeadId.Id); + } + + private void AssertAccountMatchesLead(Account account, Lead lead) + { + Assert.AreEqual(lead.SIC, account.SIC); + Assert.AreEqual(lead.EMailAddress1, account.EMailAddress1); + Assert.AreEqual(lead.CompanyName, account.Name); + Assert.AreEqual(lead.Fax, account.Fax); + Assert.AreEqual(lead.WebSiteUrl, account.WebSiteURL); + Assert.AreEqual(lead.Address1_City, account.Address1_City); + Assert.AreEqual(lead.Address1_Country, account.Address1_Country); + Assert.AreEqual(lead.Address1_Line1, account.Address1_Line1); + Assert.AreEqual(lead.Address1_Line2, account.Address1_Line2); + Assert.AreEqual(lead.Address1_Line3, account.Address1_Line3); + Assert.AreEqual(lead.Address1_PostalCode, account.Address1_PostalCode); + Assert.AreEqual(lead.Address1_StateOrProvince, account.Address1_StateOrProvince); + Assert.AreEqual(lead.Telephone2, account.Telephone2); + Assert.AreEqual(lead.DoNotPostalMail, account.DoNotPostalMail); + Assert.AreEqual(lead.DoNotPhone, account.DoNotPhone); + Assert.AreEqual(lead.DoNotFax, account.DoNotFax); + Assert.AreEqual(lead.DoNotSendMM, account.DoNotSendMM); + Assert.AreEqual(lead.Description, account.Description); + Assert.AreEqual(lead.DoNotEMail, account.DoNotEMail); + Assert.AreEqual(lead.YomiCompanyName, account.YomiName); + Assert.AreEqual(lead.DoNotBulkEMail, account.DoNotBulkEMail); + Assert.IsNotNull(account.OriginatingLeadId); + Assert.AreEqual(lead.Id, account.OriginatingLeadId.Id); } } } From 3ebc1e737b5e818dcf831016d903638c83524847 Mon Sep 17 00:00:00 2001 From: Niels Ladekarl Date: Mon, 13 Aug 2018 10:15:07 +0200 Subject: [PATCH 4/4] refactored assignment of attributes --- .../Requests/QualifyLeadRequestHandler.cs | 223 +++++++----------- 1 file changed, 91 insertions(+), 132 deletions(-) diff --git a/src/XrmMockupShared/Requests/QualifyLeadRequestHandler.cs b/src/XrmMockupShared/Requests/QualifyLeadRequestHandler.cs index 6c576b2d..324e0891 100644 --- a/src/XrmMockupShared/Requests/QualifyLeadRequestHandler.cs +++ b/src/XrmMockupShared/Requests/QualifyLeadRequestHandler.cs @@ -13,6 +13,74 @@ namespace DG.Tools.XrmMockup { internal class QualifyLeadRequestHandler : RequestHandler { + private readonly Dictionary leadToAccountAttributesMap = new Dictionary + { + { "sic", "sic" }, + { "emailaddress1", "emailaddress1" }, + { "companyname", "name" }, + { "fax", "fax" }, + { "websiteurl", "websiteurl" }, + { "address1_country", "address1_country" }, + { "address1_city", "address1_city" }, + { "address1_line1", "address1_line1" }, + { "address1_line2", "address1_line2" }, + { "address1_line3", "address1_line3" }, + { "address1_postalcode", "address1_postalcode" }, + { "address1_stateorprovince", "address1_stateorprovince" }, + { "telephone2", "telephone2" }, + { "donotpostalmail", "donotpostalmail" }, + { "donotphone", "donotphone" }, + { "donotfax", "donotfax" }, + { "donotsendmm", "donotsendmm" }, + { "description", "description" }, + { "donotemail", "donotemail" }, + { "yomicompanyname", "yominame" }, + { "donotbulkemail", "donotbulkemail" } + }; + + private readonly Dictionary leadToContactAttributesMap = new Dictionary + { + { "mobilephone", "mobilephone" }, + { "emailaddress1", "emailaddress1" }, + { "emailaddress3", "emailaddress3" }, + { "websiteurl", "websiteurl" }, + { "yomilastname", "yomilastname" }, + { "lastname", "lastname" }, + { "donotpostalmail", "donotpostalmail" }, + { "donotphone", "donotphone" }, + { "yomimiddlename", "yomimiddlename" }, + { "description", "description" }, + { "firstname", "firstname" }, + { "donotemail", "donotemail" }, + { "address1_stateorprovince", "address1_stateorprovince" }, + { "address2_county", "address2_county" }, + { "donotfax", "donotfax" }, + { "donotsendmm", "donotsendmm" }, + { "jobtitle", "jobtitle" }, + { "pager", "pager" }, + { "address1_country", "address1_country" }, + { "address1_line1", "address1_line1" }, + { "address1_line2", "address1_line2" }, + { "address1_line3", "address1_line3" }, + { "telephone2", "telephone2" }, + { "telephone3", "telephone3" }, + { "address2_fax", "address2_fax" }, + { "donotbulkemail", "donotbulkemail" }, + { "emailaddress2", "emailaddress2" }, + { "fax", "fax" }, + { "yomifirstname", "yomifirstname" }, + { "address1_postalcode", "address1_postalcode" }, + { "address1_city", "address1_city" }, + { "address2_country", "address2_country" } + }; + + private readonly Dictionary leadToOpportunityAttributesMap = new Dictionary + { + { "subject", "name" }, + { "qualificationcomments", "qualificationcomments" }, + { "description", "description" } + }; + internal QualifyLeadRequestHandler(Core core, XrmDb db, MetadataSkeleton metadata, Security security) : base(core, db, metadata, security, "QualifyLead") { } @@ -86,17 +154,17 @@ internal override OrganizationResponse Execute(OrganizationRequest orgRequest, E if (request.CreateAccount) { - createdEntities.Add(CreateAccountFromLead(lead, userRef)); + createdEntities.Add(CreateEntityFromLead(lead, LogicalNames.Account, leadToAccountAttributesMap, userRef).ToEntityReference()); } if (request.CreateContact) { - createdEntities.Add(CreateContactFromLead(lead, userRef)); + createdEntities.Add(CreateEntityFromLead(lead, LogicalNames.Contact, leadToContactAttributesMap, userRef).ToEntityReference()); } if (request.CreateOpportunity) { - createdEntities.Add(CreateOpportunityFromLead(lead, userRef, request.OpportunityCustomerId, request.OpportunityCurrencyId)); + createdEntities.Add(CreateOpportunityFromLead(lead, request.OpportunityCustomerId, request.OpportunityCurrencyId, userRef).ToEntityReference()); } lead["statuscode"] = status; @@ -107,7 +175,7 @@ internal override OrganizationResponse Execute(OrganizationRequest orgRequest, E return resp; } - private EntityReference CreateOpportunityFromLead(Entity lead, EntityReference userRef, EntityReference customer, EntityReference currency) + private Entity CreateOpportunityFromLead(Entity lead, EntityReference customer, EntityReference currency, EntityReference userRef) { if (customer != null) { @@ -117,17 +185,11 @@ private EntityReference CreateOpportunityFromLead(Entity lead, EntityReference u throw new FaultException($"CustomerIdType for {LogicalNames.Opportunity} can either be an {LogicalNames.Account} or {LogicalNames.Contact}"); } } - var opportunity = new Entity(LogicalNames.Opportunity); - if (lead.Attributes.Contains("subject")) - opportunity["name"] = lead["subject"]; - if (lead.Attributes.Contains("qualificationcomments")) - opportunity["qualificationcomments"] = lead["qualificationcomments"]; - if (lead.Attributes.Contains("description")) - opportunity["description"] = lead["description"]; + MapAttributesFromLead(lead, leadToOpportunityAttributesMap, ref opportunity); opportunity["originatingleadid"] = lead.ToEntityReference(); - if(customer != null) + if (customer != null) { opportunity["customerid"] = customer; } @@ -138,132 +200,29 @@ private EntityReference CreateOpportunityFromLead(Entity lead, EntityReference u } opportunity.Id = CreateEntity(opportunity, userRef); - return opportunity.ToEntityReference(); + + return opportunity; } - private EntityReference CreateContactFromLead(Entity lead, EntityReference userRef) + private Entity CreateEntityFromLead(Entity lead, string newEntityLogicalName, IDictionary attributesMap, EntityReference userRef) { - var contact = new Entity(LogicalNames.Contact); - if (lead.Attributes.Contains("mobilephone")) - contact["mobilephone"] = lead["mobilephone"]; - if (lead.Attributes.Contains("emailaddress1")) - contact["emailaddress1"] = lead["emailaddress1"]; - if (lead.Attributes.Contains("emailaddress3")) - contact["emailaddress3"] = lead["emailaddress3"]; - if (lead.Attributes.Contains("websiteurl")) - contact["websiteurl"] = lead["websiteurl"]; - if (lead.Attributes.Contains("yomilastname")) - contact["yomilastname"] = lead["yomilastname"]; - if (lead.Attributes.Contains("lastname")) - contact["lastname"] = lead["lastname"]; - if (lead.Attributes.Contains("donotpostalmail")) - contact["donotpostalmail"] = lead["donotpostalmail"]; - if (lead.Attributes.Contains("donotphone")) - contact["donotphone"] = lead["donotphone"]; - if (lead.Attributes.Contains("yomimiddlename")) - contact["yomimiddlename"] = lead["yomimiddlename"]; - if (lead.Attributes.Contains("description")) - contact["description"] = lead["description"]; - if (lead.Attributes.Contains("firstname")) - contact["firstname"] = lead["firstname"]; - if (lead.Attributes.Contains("donotemail")) - contact["donotemail"] = lead["donotemail"]; - if (lead.Attributes.Contains("address1_stateorprovince")) - contact["address1_stateorprovince"] = lead["address1_stateorprovince"]; - if (lead.Attributes.Contains("address2_county")) - contact["address2_county"] = lead["address2_county"]; - if (lead.Attributes.Contains("donotfax")) - contact["donotfax"] = lead["donotfax"]; - if (lead.Attributes.Contains("donotsendmm")) - contact["donotsendmm"] = lead["donotsendmm"]; - if (lead.Attributes.Contains("jobtitle")) - contact["jobtitle"] = lead["jobtitle"]; - if (lead.Attributes.Contains("pager")) - contact["pager"] = lead["pager"]; - if (lead.Attributes.Contains("address1_country")) - contact["address1_country"] = lead["address1_country"]; - if (lead.Attributes.Contains("address1_line1")) - contact["address1_line1"] = lead["address1_line1"]; - if (lead.Attributes.Contains("address1_line2")) - contact["address1_line2"] = lead["address1_line2"]; - if (lead.Attributes.Contains("address1_line3")) - contact["address1_line3"] = lead["address1_line3"]; - if (lead.Attributes.Contains("telephone2")) - contact["telephone2"] = lead["telephone2"]; - if (lead.Attributes.Contains("telephone3")) - contact["telephone3"] = lead["telephone3"]; - if (lead.Attributes.Contains("address2_fax")) - contact["address2_fax"] = lead["address2_fax"]; - if (lead.Attributes.Contains("donotbulkemail")) - contact["donotbulkemail"] = lead["donotbulkemail"]; - if (lead.Attributes.Contains("emailaddress2")) - contact["emailaddress2"] = lead["emailaddress2"]; - if (lead.Attributes.Contains("fax")) - contact["fax"] = lead["fax"]; - if (lead.Attributes.Contains("yomifirstname")) - contact["yomifirstname"] = lead["yomifirstname"]; - if (lead.Attributes.Contains("address1_postalcode")) - contact["address1_postalcode"] = lead["address1_postalcode"]; - if (lead.Attributes.Contains("address1_city")) - contact["address1_city"] = lead["address1_city"]; - if (lead.Attributes.Contains("address2_country")) - contact["address2_country"] = lead["address2_country"]; - contact["originatingleadid"] = lead.ToEntityReference(); - - contact.Id = CreateEntity(contact, userRef); - return contact.ToEntityReference(); + var newEntity = new Entity(newEntityLogicalName); + + MapAttributesFromLead(lead, attributesMap, ref newEntity); + + newEntity["originatingleadid"] = lead.ToEntityReference(); + + newEntity.Id = CreateEntity(newEntity, userRef); + return newEntity; } - private EntityReference CreateAccountFromLead(Entity lead, EntityReference userRef) + private void MapAttributesFromLead(Entity lead, IDictionary attributesMap, ref Entity toEntity) { - var account = new Entity(LogicalNames.Account); - - if(lead.Attributes.Contains("sic")) - account["sic"] = lead["sic"]; - if(lead.Attributes.Contains("emailaddress1")) - account["emailaddress1"] = lead["emailaddress1"]; - if(lead.Attributes.Contains("companyname")) - account["name"] = lead["companyname"]; - if (lead.Attributes.Contains("fax")) - account["fax"] = lead["fax"]; - if (lead.Attributes.Contains("websiteurl")) - account["websiteurl"] = lead["websiteurl"]; - if (lead.Attributes.Contains("address1_country")) - account["address1_country"] = lead["address1_country"]; - if (lead.Attributes.Contains("address1_city")) - account["address1_city"] = lead["address1_city"]; - if (lead.Attributes.Contains("address1_line1")) - account["address1_line1"] = lead["address1_line1"]; - if (lead.Attributes.Contains("address1_line2")) - account["address1_line2"] = lead["address1_line2"]; - if (lead.Attributes.Contains("address1_line3")) - account["address1_line3"] = lead["address1_line3"]; - if (lead.Attributes.Contains("address1_postalcode")) - account["address1_postalcode"] = lead["address1_postalcode"]; - if (lead.Attributes.Contains("address1_stateorprovince")) - account["address1_stateorprovince"] = lead["address1_stateorprovince"]; - if (lead.Attributes.Contains("telephone2")) - account["telephone2"] = lead["telephone2"]; - if (lead.Attributes.Contains("donotpostalmail")) - account["donotpostalmail"] = lead["donotpostalmail"]; - if (lead.Attributes.Contains("donotphone")) - account["donotphone"] = lead["donotphone"]; - if (lead.Attributes.Contains("donotfax")) - account["donotfax"] = lead["donotfax"]; - if (lead.Attributes.Contains("donotsendmm")) - account["donotsendmm"] = lead["donotsendmm"]; - if (lead.Attributes.Contains("description")) - account["description"] = lead["description"]; - if (lead.Attributes.Contains("donotemail")) - account["donotemail"] = lead["donotemail"]; - if (lead.Attributes.Contains("yominame")) - account["yominame"] = lead["yominame"]; - if (lead.Attributes.Contains("donotbulkemail")) - account["donotbulkemail"] = lead["donotbulkemail"]; - account["originatingleadid"] = lead.ToEntityReference(); - - account.Id = CreateEntity(account, userRef); - return account.ToEntityReference(); + foreach(var attr in attributesMap) + { + if(lead.Attributes.Contains(attr.Key)) + toEntity[attr.Value] = lead[attr.Key]; + } } private Guid CreateEntity(Entity entity, EntityReference userRef)