Skip to content
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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/XrmMockupShared/Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
153 changes: 153 additions & 0 deletions src/XrmMockupShared/Requests/QualifyLeadRequestHandler.cs
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"]));
Copy link
Member

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.


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
Copy link
Member

Choose a reason for hiding this comment

The 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"]));
Copy link
Member

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.


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
Copy link
Member

Choose a reason for hiding this comment

The 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;
Copy link
Member

Choose a reason for hiding this comment

The 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;
}
}
}
13 changes: 6 additions & 7 deletions src/XrmMockupShared/Utility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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)
{
Expand Down
1 change: 1 addition & 0 deletions src/XrmMockupShared/XrmMockupShared.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Plugin\Plugin.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Plugin\SystemPlugins\ContactTest.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Requests\IsValidStateTransitionRequestHandler.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Requests\QualifyLeadRequestHandler.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Requests\RetrieveExchangeRateRequestHandler.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Security.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Domain.cs" />
Expand Down
1 change: 1 addition & 0 deletions tests/SharedTests/SharedTests.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<Compile Include="$(MSBuildThisFileDirectory)TestActivities.cs" />
<Compile Include="$(MSBuildThisFileDirectory)TestAction.cs" />
<Compile Include="$(MSBuildThisFileDirectory)TestAlternateKeys.cs" />
<Compile Include="$(MSBuildThisFileDirectory)TestLeads.cs" />
<Compile Include="$(MSBuildThisFileDirectory)TestOpportunity.cs" />
<Compile Include="$(MSBuildThisFileDirectory)TestOrderByQueryExpression.cs" />
<Compile Include="$(MSBuildThisFileDirectory)TestAssocDissoc.cs" />
Expand Down
Loading