| | 1 | | using FakeXrmEasy.Extensions; |
| | 2 | | using Microsoft.Xrm.Sdk; |
| | 3 | | using Microsoft.Xrm.Sdk.Messages; |
| | 4 | | using System; |
| | 5 | |
|
| | 6 | | #if !FAKE_XRM_EASY && !FAKE_XRM_EASY_2013 && !FAKE_XRM_EASY_2015 |
| | 7 | |
|
| | 8 | | namespace FakeXrmEasy.FakeMessageExecutors |
| | 9 | | { |
| | 10 | | public class UpsertRequestExecutor : IFakeMessageExecutor |
| | 11 | | { |
| | 12 | | public bool CanExecute(OrganizationRequest request) |
| 12 | 13 | | { |
| 12 | 14 | | return request is UpsertRequest; |
| 12 | 15 | | } |
| | 16 | |
|
| | 17 | | public OrganizationResponse Execute(OrganizationRequest request, XrmFakedContext ctx) |
| 12 | 18 | | { |
| 12 | 19 | | var upsertRequest = (UpsertRequest)request; |
| | 20 | | bool recordCreated; |
| | 21 | |
|
| 12 | 22 | | var service = ctx.GetOrganizationService(); |
| | 23 | |
|
| 12 | 24 | | var entityLogicalName = upsertRequest.Target.LogicalName; |
| 12 | 25 | | var entityId = ctx.GetRecordUniqueId(upsertRequest.Target.ToEntityReferenceWithKeyAttributes(), validate: fa |
| | 26 | |
|
| 12 | 27 | | if (ctx.Data.ContainsKey(entityLogicalName) && |
| 12 | 28 | | ctx.Data[entityLogicalName].ContainsKey(entityId)) |
| 6 | 29 | | { |
| 6 | 30 | | recordCreated = false; |
| 6 | 31 | | service.Update(upsertRequest.Target); |
| 6 | 32 | | } |
| | 33 | | else |
| 6 | 34 | | { |
| 6 | 35 | | recordCreated = true; |
| 6 | 36 | | entityId = service.Create(upsertRequest.Target); |
| 6 | 37 | | } |
| | 38 | |
|
| 12 | 39 | | var result = new UpsertResponse(); |
| 12 | 40 | | result.Results.Add("RecordCreated", recordCreated); |
| 12 | 41 | | result.Results.Add("Target", new EntityReference(entityLogicalName, entityId)); |
| 12 | 42 | | return result; |
| 12 | 43 | | } |
| | 44 | |
|
| | 45 | | public Type GetResponsibleRequestType() |
| 2201 | 46 | | { |
| 2201 | 47 | | return typeof(UpsertRequest); |
| 2201 | 48 | | } |
| | 49 | | } |
| | 50 | | } |
| | 51 | | #endif |