|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.ServiceModel; |
| 5 | +using Microsoft.Crm.Sdk.Messages; |
| 6 | +using Microsoft.Extensions.Logging; |
| 7 | +using Microsoft.Xrm.Sdk; |
| 8 | +using Microsoft.Xrm.Sdk.Messages; |
| 9 | +using Microsoft.Xrm.Sdk.Metadata; |
| 10 | +using DG.Tools.XrmMockup.Database; |
| 11 | +using DG.Tools.XrmMockup.Internal; |
| 12 | + |
| 13 | +namespace DG.Tools.XrmMockup |
| 14 | +{ |
| 15 | + internal class QualifyLeadRequestHandler : RequestHandler |
| 16 | + { |
| 17 | + // Lead state codes (statecode option values). |
| 18 | + private const int LeadStateOpen = 0; |
| 19 | + private const int LeadStateQualified = 1; |
| 20 | + |
| 21 | + // Maps a source attribute on the lead to the destination attribute on the created record. |
| 22 | + // Only attributes present on both the lead and the target's metadata are copied. |
| 23 | + private static readonly Dictionary<string, string> LeadToAccountAttributeMap = new Dictionary<string, string> |
| 24 | + { |
| 25 | + { "sic", "sic" }, |
| 26 | + { "emailaddress1", "emailaddress1" }, |
| 27 | + { "companyname", "name" }, |
| 28 | + { "fax", "fax" }, |
| 29 | + { "websiteurl", "websiteurl" }, |
| 30 | + { "address1_country", "address1_country" }, |
| 31 | + { "address1_city", "address1_city" }, |
| 32 | + { "address1_line1", "address1_line1" }, |
| 33 | + { "address1_line2", "address1_line2" }, |
| 34 | + { "address1_line3", "address1_line3" }, |
| 35 | + { "address1_postalcode", "address1_postalcode" }, |
| 36 | + { "address1_stateorprovince", "address1_stateorprovince" }, |
| 37 | + { "telephone2", "telephone2" }, |
| 38 | + { "donotpostalmail", "donotpostalmail" }, |
| 39 | + { "donotphone", "donotphone" }, |
| 40 | + { "donotfax", "donotfax" }, |
| 41 | + { "donotsendmm", "donotsendmm" }, |
| 42 | + { "description", "description" }, |
| 43 | + { "donotemail", "donotemail" }, |
| 44 | + { "yomicompanyname", "yominame" }, |
| 45 | + { "donotbulkemail", "donotbulkemail" }, |
| 46 | + }; |
| 47 | + |
| 48 | + private static readonly Dictionary<string, string> LeadToContactAttributeMap = new Dictionary<string, string> |
| 49 | + { |
| 50 | + { "mobilephone", "mobilephone" }, |
| 51 | + { "emailaddress1", "emailaddress1" }, |
| 52 | + { "emailaddress2", "emailaddress2" }, |
| 53 | + { "emailaddress3", "emailaddress3" }, |
| 54 | + { "websiteurl", "websiteurl" }, |
| 55 | + { "yomifirstname", "yomifirstname" }, |
| 56 | + { "yomimiddlename", "yomimiddlename" }, |
| 57 | + { "yomilastname", "yomilastname" }, |
| 58 | + { "firstname", "firstname" }, |
| 59 | + { "lastname", "lastname" }, |
| 60 | + { "jobtitle", "jobtitle" }, |
| 61 | + { "pager", "pager" }, |
| 62 | + { "fax", "fax" }, |
| 63 | + { "telephone2", "telephone2" }, |
| 64 | + { "telephone3", "telephone3" }, |
| 65 | + { "description", "description" }, |
| 66 | + { "donotpostalmail", "donotpostalmail" }, |
| 67 | + { "donotphone", "donotphone" }, |
| 68 | + { "donotfax", "donotfax" }, |
| 69 | + { "donotemail", "donotemail" }, |
| 70 | + { "donotsendmm", "donotsendmm" }, |
| 71 | + { "donotbulkemail", "donotbulkemail" }, |
| 72 | + { "address1_country", "address1_country" }, |
| 73 | + { "address1_city", "address1_city" }, |
| 74 | + { "address1_line1", "address1_line1" }, |
| 75 | + { "address1_line2", "address1_line2" }, |
| 76 | + { "address1_line3", "address1_line3" }, |
| 77 | + { "address1_postalcode", "address1_postalcode" }, |
| 78 | + { "address1_stateorprovince", "address1_stateorprovince" }, |
| 79 | + }; |
| 80 | + |
| 81 | + private static readonly Dictionary<string, string> LeadToOpportunityAttributeMap = new Dictionary<string, string> |
| 82 | + { |
| 83 | + { "subject", "name" }, |
| 84 | + { "qualificationcomments", "qualificationcomments" }, |
| 85 | + { "description", "description" }, |
| 86 | + }; |
| 87 | + |
| 88 | + internal QualifyLeadRequestHandler(Core core, XrmDb db, MetadataSkeleton metadata, Security security) |
| 89 | + : base(core, db, metadata, security, "QualifyLead") { } |
| 90 | + |
| 91 | + internal override OrganizationResponse Execute(OrganizationRequest orgRequest, EntityReference userRef) |
| 92 | + { |
| 93 | + var request = MakeRequest<QualifyLeadRequest>(orgRequest); |
| 94 | + |
| 95 | + if (request.LeadId == null) |
| 96 | + { |
| 97 | + throw new FaultException("Required field 'LeadId' is missing"); |
| 98 | + } |
| 99 | + |
| 100 | + // GetMetadata throws a descriptive fault if the entity is unknown to the metadata cache. |
| 101 | + var leadMetadata = metadata.EntityMetadata.GetMetadata(request.LeadId.LogicalName); |
| 102 | + if (request.OpportunityCurrencyId != null && |
| 103 | + request.OpportunityCurrencyId.LogicalName != LogicalNames.TransactionCurrency) |
| 104 | + { |
| 105 | + throw new FaultException($"OpportunityCurrencyId must reference a '{LogicalNames.TransactionCurrency}'."); |
| 106 | + } |
| 107 | + if (request.OpportunityCustomerId != null) |
| 108 | + { |
| 109 | + metadata.EntityMetadata.GetMetadata(request.OpportunityCustomerId.LogicalName); |
| 110 | + } |
| 111 | + |
| 112 | + var leadRow = db.GetDbRowOrNull(request.LeadId); |
| 113 | + if (leadRow == null || request.LeadId.LogicalName != LogicalNames.Lead) |
| 114 | + { |
| 115 | + throw new FaultException($"{LogicalNames.Lead} With Id = {request.LeadId.Id} Does Not Exist"); |
| 116 | + } |
| 117 | + |
| 118 | + var lead = leadRow.ToEntity(); |
| 119 | + |
| 120 | + // A lead can only be qualified/disqualified while it is still open. |
| 121 | + var currentState = lead.GetAttributeValue<OptionSetValue>("statecode"); |
| 122 | + if (currentState != null && currentState.Value != LeadStateOpen) |
| 123 | + { |
| 124 | + throw new FaultException($"The {LogicalNames.Lead} is already closed."); |
| 125 | + } |
| 126 | + |
| 127 | + if (request.Status == null) |
| 128 | + { |
| 129 | + throw new FaultException("Required field 'Status' is missing"); |
| 130 | + } |
| 131 | + var status = request.Status.Value; |
| 132 | + |
| 133 | + var statusOption = Utility.GetStatusOptionMetadata(leadMetadata) |
| 134 | + .FirstOrDefault(s => s.Value == status) as StatusOptionMetadata; |
| 135 | + if (statusOption == null) |
| 136 | + { |
| 137 | + throw new FaultException($"{status} is not a valid status code on {LogicalNames.Lead} with Id {request.LeadId.Id}"); |
| 138 | + } |
| 139 | + |
| 140 | + // The requested status determines the resulting state (Qualified/Disqualified/...). |
| 141 | + var state = statusOption.State ?? LeadStateQualified; |
| 142 | + |
| 143 | + var createdEntities = new EntityReferenceCollection(); |
| 144 | + |
| 145 | + if (request.CreateAccount) |
| 146 | + { |
| 147 | + createdEntities.Add(CreateFromLead(lead, LogicalNames.Account, LeadToAccountAttributeMap, userRef)); |
| 148 | + } |
| 149 | + if (request.CreateContact) |
| 150 | + { |
| 151 | + createdEntities.Add(CreateFromLead(lead, LogicalNames.Contact, LeadToContactAttributeMap, userRef)); |
| 152 | + } |
| 153 | + if (request.CreateOpportunity) |
| 154 | + { |
| 155 | + createdEntities.Add(CreateOpportunityFromLead(lead, request.OpportunityCustomerId, request.OpportunityCurrencyId, userRef)); |
| 156 | + } |
| 157 | + |
| 158 | + // Close the lead in the requested state (mirrors SetStateRequestHandler). |
| 159 | + if (Utility.IsValidAttribute("statecode", leadMetadata) && Utility.IsValidAttribute("statuscode", leadMetadata)) |
| 160 | + { |
| 161 | + var previous = lead.CloneEntity(); |
| 162 | + lead["statecode"] = new OptionSetValue(state); |
| 163 | + lead["statuscode"] = new OptionSetValue(status); |
| 164 | + Utility.CheckStatusTransitions(leadMetadata, lead, previous); |
| 165 | + Utility.HandleCurrencies(metadata, db, lead); |
| 166 | + Utility.Touch(lead, leadMetadata, core.TimeOffset, userRef); |
| 167 | + db.Update(lead); |
| 168 | + } |
| 169 | + |
| 170 | + var response = new QualifyLeadResponse(); |
| 171 | + response.Results["CreatedEntities"] = createdEntities; |
| 172 | + return response; |
| 173 | + } |
| 174 | + |
| 175 | + private EntityReference CreateFromLead(Entity lead, string targetLogicalName, IDictionary<string, string> attributeMap, EntityReference userRef) |
| 176 | + { |
| 177 | + var target = new Entity(targetLogicalName); |
| 178 | + MapAttributesFromLead(lead, target, attributeMap); |
| 179 | + SetIfValid(target, "originatingleadid", lead.ToEntityReference()); |
| 180 | + target.Id = CreateEntity(target, userRef); |
| 181 | + return target.ToEntityReference(); |
| 182 | + } |
| 183 | + |
| 184 | + private EntityReference CreateOpportunityFromLead(Entity lead, EntityReference customer, EntityReference currency, EntityReference userRef) |
| 185 | + { |
| 186 | + if (customer != null && |
| 187 | + customer.LogicalName != LogicalNames.Account && |
| 188 | + customer.LogicalName != LogicalNames.Contact) |
| 189 | + { |
| 190 | + throw new FaultException($"CustomerIdType for {LogicalNames.Opportunity} can either be an {LogicalNames.Account} or {LogicalNames.Contact}"); |
| 191 | + } |
| 192 | + |
| 193 | + var opportunity = new Entity(LogicalNames.Opportunity); |
| 194 | + MapAttributesFromLead(lead, opportunity, LeadToOpportunityAttributeMap); |
| 195 | + SetIfValid(opportunity, "originatingleadid", lead.ToEntityReference()); |
| 196 | + if (customer != null) |
| 197 | + { |
| 198 | + SetIfValid(opportunity, "customerid", customer); |
| 199 | + } |
| 200 | + if (currency != null) |
| 201 | + { |
| 202 | + SetIfValid(opportunity, "transactioncurrencyid", currency); |
| 203 | + } |
| 204 | + opportunity.Id = CreateEntity(opportunity, userRef); |
| 205 | + return opportunity.ToEntityReference(); |
| 206 | + } |
| 207 | + |
| 208 | + private void MapAttributesFromLead(Entity lead, Entity target, IDictionary<string, string> attributeMap) |
| 209 | + { |
| 210 | + var targetMetadata = metadata.EntityMetadata.GetMetadata(target.LogicalName); |
| 211 | + foreach (var mapping in attributeMap) |
| 212 | + { |
| 213 | + if (!lead.Attributes.Contains(mapping.Key)) |
| 214 | + { |
| 215 | + continue; |
| 216 | + } |
| 217 | + if (!Utility.IsValidAttribute(mapping.Value, targetMetadata)) |
| 218 | + { |
| 219 | + core.Logger?.LogWarning( |
| 220 | + "QualifyLead: skipped copying lead.{Source} to {Target}.{Destination} because the target entity's metadata does not define that attribute.", |
| 221 | + mapping.Key, target.LogicalName, mapping.Value); |
| 222 | + continue; |
| 223 | + } |
| 224 | + target[mapping.Value] = lead[mapping.Key]; |
| 225 | + } |
| 226 | + } |
| 227 | + |
| 228 | + private void SetIfValid(Entity target, string attribute, object value) |
| 229 | + { |
| 230 | + var targetMetadata = metadata.EntityMetadata.GetMetadata(target.LogicalName); |
| 231 | + if (Utility.IsValidAttribute(attribute, targetMetadata)) |
| 232 | + { |
| 233 | + target[attribute] = value; |
| 234 | + } |
| 235 | + else |
| 236 | + { |
| 237 | + core.Logger?.LogWarning( |
| 238 | + "QualifyLead: skipped setting {Target}.{Attribute} because the target entity's metadata does not define that attribute.", |
| 239 | + target.LogicalName, attribute); |
| 240 | + } |
| 241 | + } |
| 242 | + |
| 243 | + private Guid CreateEntity(Entity entity, EntityReference userRef) |
| 244 | + { |
| 245 | + var response = core.Execute(new CreateRequest { Target = entity }, userRef) as CreateResponse; |
| 246 | + return response.id; |
| 247 | + } |
| 248 | + } |
| 249 | +} |
0 commit comments