| | 1 | | using System; |
| | 2 | | using System.Linq; |
| | 3 | | using Microsoft.Xrm.Sdk; |
| | 4 | | using Microsoft.Crm.Sdk.Messages; |
| | 5 | |
|
| | 6 | | namespace FakeXrmEasy.FakeMessageExecutors |
| | 7 | | { |
| | 8 | | public class QualifyLeadRequestExecutor : IFakeMessageExecutor |
| | 9 | | { |
| | 10 | | public bool CanExecute(OrganizationRequest request) |
| 30 | 11 | | { |
| 30 | 12 | | return request is QualifyLeadRequest; |
| 30 | 13 | | } |
| | 14 | |
|
| | 15 | | public OrganizationResponse Execute(OrganizationRequest request, XrmFakedContext ctx) |
| 30 | 16 | | { |
| 30 | 17 | | var req = request as QualifyLeadRequest; |
| | 18 | |
|
| 30 | 19 | | var orgService = ctx.GetOrganizationService(); |
| | 20 | |
|
| 30 | 21 | | if (req.LeadId == null) throw new Exception("Lead Id must be set in request."); |
| | 22 | |
|
| 30 | 23 | | var leads = (from l in ctx.CreateQuery("lead") |
| 30 | 24 | | where l.Id == req.LeadId.Id |
| 30 | 25 | | select l); |
| | 26 | |
|
| 30 | 27 | | var leadsCount = leads.Count(); |
| | 28 | |
|
| 30 | 29 | | 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 |
| 30 | 32 | | var response = new QualifyLeadResponse(); |
| 30 | 33 | | response["CreatedEntities"] = new EntityReferenceCollection(); |
| | 34 | |
|
| | 35 | | // Create Account |
| 30 | 36 | | if (req.CreateAccount) // ParentAccount |
| 6 | 37 | | { |
| 6 | 38 | | var account = new Entity("account") |
| 6 | 39 | | { |
| 6 | 40 | | Id = Guid.NewGuid() |
| 6 | 41 | | }; |
| 6 | 42 | | account.Attributes["originatingleadid"] = req.LeadId; |
| 6 | 43 | | orgService.Create(account); |
| 6 | 44 | | response.CreatedEntities.Add(account.ToEntityReference()); |
| 6 | 45 | | } |
| | 46 | |
|
| | 47 | | // Create Contact |
| 30 | 48 | | if (req.CreateContact) |
| 6 | 49 | | { |
| 6 | 50 | | var contact = new Entity("contact") |
| 6 | 51 | | { |
| 6 | 52 | | Id = Guid.NewGuid() |
| 6 | 53 | | }; |
| 6 | 54 | | contact.Attributes["originatingleadid"] = req.LeadId; |
| 6 | 55 | | orgService.Create(contact); |
| 6 | 56 | | response.CreatedEntities.Add(contact.ToEntityReference()); |
| 6 | 57 | | } |
| | 58 | |
|
| | 59 | | // Create Opportunity |
| 30 | 60 | | if (req.CreateOpportunity) |
| 12 | 61 | | { |
| 12 | 62 | | var opportunity = new Entity("opportunity") |
| 12 | 63 | | { |
| 12 | 64 | | Id = Guid.NewGuid() |
| 12 | 65 | | }; |
| | 66 | |
|
| | 67 | | // Set OpportunityCurrencyId if given |
| | 68 | | // MSDN link: |
| | 69 | | // https://msdn.microsoft.com/en-us/library/microsoft.crm.sdk.messages.qualifyleadrequest.opportunitycur |
| 12 | 70 | | if (req.OpportunityCurrencyId != null) |
| 0 | 71 | | { |
| 0 | 72 | | opportunity.Attributes["transactioncurrencyid"] = req.OpportunityCurrencyId; |
| 0 | 73 | | } |
| | 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 |
| 12 | 78 | | if (req.OpportunityCustomerId != null) |
| 6 | 79 | | { |
| 6 | 80 | | var logicalName = req.OpportunityCustomerId.LogicalName; |
| | 81 | |
|
| | 82 | | // Associate Account or Contact |
| 6 | 83 | | if (logicalName.Equals("account") || logicalName.Equals("contact")) |
| 6 | 84 | | { |
| 6 | 85 | | opportunity.Attributes["customerid"] = req.OpportunityCustomerId; |
| 6 | 86 | | } |
| | 87 | | // Wrong Entity was given as parameter |
| | 88 | | else |
| 0 | 89 | | { |
| 0 | 90 | | throw new Exception(string.Format("Opportunity Customer Id should be connected with Account or C |
| | 91 | | } |
| 6 | 92 | | } |
| | 93 | |
|
| 12 | 94 | | opportunity.Attributes["originatingleadid"] = req.LeadId; |
| 12 | 95 | | orgService.Create(opportunity); |
| 12 | 96 | | response.CreatedEntities.Add(opportunity.ToEntityReference()); |
| 12 | 97 | | } |
| | 98 | |
|
| | 99 | | // Actual Lead |
| 30 | 100 | | var lead = leads.First(); |
| 30 | 101 | | lead.Attributes["statuscode"] = new OptionSetValue(req.Status.Value); |
| 30 | 102 | | orgService.Update(lead); |
| | 103 | |
|
| 30 | 104 | | return response; |
| 30 | 105 | | } |
| | 106 | |
|
| | 107 | | public Type GetResponsibleRequestType() |
| 4270 | 108 | | { |
| 4270 | 109 | | return typeof(QualifyLeadRequest); |
| 4270 | 110 | | } |
| | 111 | | } |
| | 112 | | } |