| | 1 | | using Microsoft.Crm.Sdk.Messages; |
| | 2 | | using Microsoft.Xrm.Sdk; |
| | 3 | | using System; |
| | 4 | | using System.Linq; |
| | 5 | |
|
| | 6 | | namespace FakeXrmEasy.FakeMessageExecutors |
| | 7 | | { |
| | 8 | | public class WinOpportunityRequestExecutor : IFakeMessageExecutor |
| | 9 | | { |
| | 10 | | public bool CanExecute(OrganizationRequest request) |
| 6 | 11 | | { |
| 6 | 12 | | return request is WinOpportunityRequest; |
| 6 | 13 | | } |
| | 14 | |
|
| | 15 | | public OrganizationResponse Execute(OrganizationRequest request, XrmFakedContext ctx) |
| 6 | 16 | | { |
| 6 | 17 | | var req = request as WinOpportunityRequest; |
| | 18 | |
|
| | 19 | | // Check if OpportunityClose and Status were passed to request |
| 6 | 20 | | if (req.OpportunityClose != null && |
| 6 | 21 | | req.Status != null) |
| 6 | 22 | | { |
| | 23 | | // WinOpportunityRequest.OpportunityClose.OpportunityId |
| 6 | 24 | | var opportunityReference = req.OpportunityClose.GetAttributeValue<EntityReference>("opportunityid"); |
| 6 | 25 | | var opportunityId = opportunityReference.Id; |
| | 26 | |
|
| | 27 | | // Get Opportunities (in good scenario, should return 1 record) |
| 6 | 28 | | var opportunities = (from op in ctx.CreateQuery("opportunity") |
| 6 | 29 | | where op.Id == opportunityId |
| 6 | 30 | | select op); |
| | 31 | |
|
| | 32 | | // More than one if to check and give better feedback to user |
| 6 | 33 | | if (opportunities.Count() < 1) throw new Exception(string.Format("No Opportunity found with Id = {0}", o |
| 6 | 34 | | else if (opportunities.Count() > 1) throw new Exception(string.Format("More than one Opportunity found w |
| | 35 | | else |
| 6 | 36 | | { |
| 6 | 37 | | var opportunity = opportunities.FirstOrDefault(); |
| 6 | 38 | | opportunity.Attributes["statuscode"] = req.Status; |
| | 39 | |
|
| 6 | 40 | | ctx.GetOrganizationService().Update(opportunity); |
| | 41 | |
|
| 6 | 42 | | return new WinOpportunityResponse(); |
| | 43 | | } |
| | 44 | | } |
| | 45 | | else |
| 0 | 46 | | { |
| 0 | 47 | | throw new Exception("OpportunityClose or Status was not passed to request."); |
| | 48 | | } |
| 6 | 49 | | } |
| | 50 | |
|
| | 51 | | public Type GetResponsibleRequestType() |
| 4270 | 52 | | { |
| 4270 | 53 | | return typeof(WinOpportunityRequest); |
| 4270 | 54 | | } |
| | 55 | | } |
| | 56 | | } |