-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implemented QualifyLeadRequestHandler #51
base: master
Are you sure you want to change the base?
Changes from 1 commit
dbb0080
fd3ae34
f017e95
8ad5d37
dd35152
3ebc1e7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<QualifyLeadRequest>(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<int>("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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Qualify lead uses some of the fields on the lead when creating the account. Such as the Company text field |
||
{ | ||
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"])); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think Qualified Lead handles any kind of duplicate detection in CRM. |
||
|
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Qualify Lead uses fields on the lead when creating the contact such as Contact Name and Job title. |
||
{ | ||
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You also need to create the entities in the mockup database. Currently you just respond with almost the same data as the user has requested. Additionally, you also need to update the state of the Lead to Qualified. |
||
return resp; | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think Qualified Lead handles any kind of duplicate detection in CRM.