Summary

Class:FakeXrmEasy.FakeMessageExecutors.QualifyLeadRequestExecutor
Assembly:FakeXrmEasy
File(s):C:\code\jordimontana82\fake-xrm-easy\FakeXrmEasy.Shared\FakeMessageExecutors\QualifyLeadRequestExecutor.cs
Covered lines:61
Uncovered lines:5
Coverable lines:66
Total lines:112
Line coverage:92.4%
Branch coverage:75%

Metrics

MethodCyclomatic ComplexitySequence CoverageBranch Coverage
CanExecute(...)1100100
Execute(...)1086.2776.47
GetResponsibleRequestType()1100100

File(s)

C:\code\jordimontana82\fake-xrm-easy\FakeXrmEasy.Shared\FakeMessageExecutors\QualifyLeadRequestExecutor.cs

#LineLine coverage
 1using System;
 2using System.Linq;
 3using Microsoft.Xrm.Sdk;
 4using Microsoft.Crm.Sdk.Messages;
 5
 6namespace FakeXrmEasy.FakeMessageExecutors
 7{
 8    public class QualifyLeadRequestExecutor : IFakeMessageExecutor
 9    {
 10        public bool CanExecute(OrganizationRequest request)
 3011        {
 3012            return request is QualifyLeadRequest;
 3013        }
 14
 15        public OrganizationResponse Execute(OrganizationRequest request, XrmFakedContext ctx)
 3016        {
 3017            var req = request as QualifyLeadRequest;
 18
 3019            var orgService = ctx.GetOrganizationService();
 20
 3021             if (req.LeadId == null) throw new Exception("Lead Id must be set in request.");
 22
 3023            var leads = (from l in ctx.CreateQuery("lead")
 3024                         where l.Id == req.LeadId.Id
 3025                         select l);
 26
 3027            var leadsCount = leads.Count();
 28
 3029             if (leadsCount != 1) throw new Exception(string.Format("Number of Leads by given LeadId should be 1. Instead
 30
 31            // Made here to get access to CreatedEntities collection
 3032            var response = new QualifyLeadResponse();
 3033            response["CreatedEntities"] = new EntityReferenceCollection();
 34
 35            // Create Account
 3036             if (req.CreateAccount) // ParentAccount
 637            {
 638                var account = new Entity("account")
 639                {
 640                    Id = Guid.NewGuid()
 641                };
 642                account.Attributes["originatingleadid"] = req.LeadId;
 643                orgService.Create(account);
 644                response.CreatedEntities.Add(account.ToEntityReference());
 645            }
 46
 47            // Create Contact
 3048             if (req.CreateContact)
 649            {
 650                var contact = new Entity("contact")
 651                {
 652                    Id = Guid.NewGuid()
 653                };
 654                contact.Attributes["originatingleadid"] = req.LeadId;
 655                orgService.Create(contact);
 656                response.CreatedEntities.Add(contact.ToEntityReference());
 657            }
 58
 59            // Create Opportunity
 3060             if (req.CreateOpportunity)
 1261            {
 1262                var opportunity = new Entity("opportunity")
 1263                {
 1264                    Id = Guid.NewGuid()
 1265                };
 66
 67                // Set OpportunityCurrencyId if given
 68                // MSDN link:
 69                // https://msdn.microsoft.com/en-us/library/microsoft.crm.sdk.messages.qualifyleadrequest.opportunitycur
 1270                 if (req.OpportunityCurrencyId != null)
 071                {
 072                    opportunity.Attributes["transactioncurrencyid"] = req.OpportunityCurrencyId;
 073                }
 74
 75                // Associate Account or Contact with Opportunity
 76                // MSDN link:
 77                // https://msdn.microsoft.com/en-us/library/microsoft.crm.sdk.messages.qualifyleadrequest.opportunitycus
 1278                 if (req.OpportunityCustomerId != null)
 679                {
 680                    var logicalName = req.OpportunityCustomerId.LogicalName;
 81
 82                    // Associate Account or Contact
 683                     if (logicalName.Equals("account") || logicalName.Equals("contact"))
 684                    {
 685                        opportunity.Attributes["customerid"] = req.OpportunityCustomerId;
 686                    }
 87                    // Wrong Entity was given as parameter
 88                    else
 089                    {
 090                        throw new Exception(string.Format("Opportunity Customer Id should be connected with Account or C
 91                    }
 692                }
 93
 1294                opportunity.Attributes["originatingleadid"] = req.LeadId;
 1295                orgService.Create(opportunity);
 1296                response.CreatedEntities.Add(opportunity.ToEntityReference());
 1297            }
 98
 99            // Actual Lead
 30100            var lead = leads.First();
 30101            lead.Attributes["statuscode"] = new OptionSetValue(req.Status.Value);
 30102            orgService.Update(lead);
 103
 30104            return response;
 30105        }
 106
 107        public Type GetResponsibleRequestType()
 4270108        {
 4270109            return typeof(QualifyLeadRequest);
 4270110        }
 111    }
 112}