| | 1 | | using Microsoft.Crm.Sdk.Messages; |
| | 2 | | using Microsoft.Xrm.Sdk; |
| | 3 | | using Microsoft.Xrm.Sdk.Query; |
| | 4 | | using System; |
| | 5 | | using System.Linq; |
| | 6 | | using System.ServiceModel; |
| | 7 | |
|
| | 8 | | namespace FakeXrmEasy.FakeMessageExecutors |
| | 9 | | { |
| | 10 | | public class AddToQueueRequestExecutor : IFakeMessageExecutor |
| | 11 | | { |
| | 12 | | public bool CanExecute(OrganizationRequest request) |
| 6 | 13 | | { |
| 6 | 14 | | return request is AddToQueueRequest; |
| 6 | 15 | | } |
| | 16 | |
|
| | 17 | | public OrganizationResponse Execute(OrganizationRequest request, XrmFakedContext ctx) |
| 18 | 18 | | { |
| 18 | 19 | | var addToQueueRequest = (AddToQueueRequest)request; |
| | 20 | |
|
| 18 | 21 | | var target = addToQueueRequest.Target; |
| 18 | 22 | | var destinationQueueId = addToQueueRequest.DestinationQueueId; |
| 18 | 23 | | var queueItemProperties = addToQueueRequest.QueueItemProperties; |
| | 24 | |
|
| 18 | 25 | | if (target == null) |
| 0 | 26 | | { |
| 0 | 27 | | throw new FaultException<OrganizationServiceFault>(new OrganizationServiceFault(), "Can not add to queue |
| | 28 | | } |
| | 29 | |
|
| 18 | 30 | | if (destinationQueueId == null) |
| | 31 | | { |
| | 32 | | throw new FaultException<OrganizationServiceFault>(new OrganizationServiceFault(), "Can not add to queue |
| | 33 | | } |
| | 34 | |
|
| 18 | 35 | | var service = ctx.GetOrganizationService(); |
| | 36 | |
|
| | 37 | | // CRM updates existing queue item if one already exists for a given objectid |
| 18 | 38 | | var existingQueueItem = service.RetrieveMultiple(new QueryExpression |
| 18 | 39 | | { |
| 18 | 40 | | EntityName = "queueitem", |
| 18 | 41 | | ColumnSet = new ColumnSet(true), |
| 18 | 42 | | Criteria = new FilterExpression |
| 18 | 43 | | { |
| 18 | 44 | | Conditions = |
| 18 | 45 | | { |
| 18 | 46 | | new ConditionExpression("objectid", ConditionOperator.Equal, target.Id) |
| 18 | 47 | | } |
| 18 | 48 | | } |
| 18 | 49 | | }).Entities.FirstOrDefault(); |
| | 50 | |
|
| 18 | 51 | | var createQueueItem = existingQueueItem ?? new Entity |
| 18 | 52 | | { |
| 18 | 53 | | LogicalName = "queueitem", |
| 18 | 54 | | // QueueItemProperties are used for initializing new queueitems |
| 18 | 55 | | Attributes = queueItemProperties?.Attributes |
| 18 | 56 | | }; |
| | 57 | |
|
| 18 | 58 | | createQueueItem["queueid"] = new EntityReference("queue", destinationQueueId); |
| 18 | 59 | | createQueueItem["objectid"] = target; |
| | 60 | |
|
| 18 | 61 | | var guid = service.Create(createQueueItem); |
| | 62 | |
|
| 18 | 63 | | return new AddToQueueResponse() |
| 18 | 64 | | { |
| 18 | 65 | | ResponseName = "AddToQueue", |
| 18 | 66 | | Results = new ParameterCollection { { "QueueItemId", guid } } |
| 18 | 67 | | }; |
| 18 | 68 | | } |
| | 69 | |
|
| | 70 | | public Type GetResponsibleRequestType() |
| 4270 | 71 | | { |
| 4270 | 72 | | return typeof(AddToQueueRequest); |
| 4270 | 73 | | } |
| | 74 | | } |
| | 75 | | } |