| | 1 | | using System; |
| | 2 | | using System.Linq; |
| | 3 | | using Microsoft.Xrm.Sdk; |
| | 4 | |
|
| | 5 | | namespace FakeXrmEasy.FakeMessageExecutors.CustomExecutors |
| | 6 | | { |
| | 7 | | /// <summary> |
| | 8 | | /// It will navigate to next Entity in Workflow path and add next Stage Id to traversed path. |
| | 9 | | /// |
| | 10 | | /// Additional links: |
| | 11 | | /// https://www.magnetismsolutions.com/blog/gayanperera/2016/02/19/programmatically-move-cross-entity-business-proce |
| | 12 | | /// https://community.dynamics.com/crm/b/magnetismsolutionscrmblog/archive/2016/02/19/programmatically-move-cross-en |
| | 13 | | /// https://crmtipoftheday.com/589/programmatically-move-cross-entity-business-process-flow-stages-in-crm-2016/ |
| | 14 | | /// </summary> |
| | 15 | | public class NavigateToNextEntityOrganizationRequestExecutor : IFakeMessageExecutor |
| | 16 | | { |
| 6 | 17 | | public static readonly string RequestName = "NavigateToNextEntity"; |
| | 18 | |
|
| | 19 | | // Required Parameters - comment on each key describes the output type of value = request.Prameters[key] |
| 6 | 20 | | public static readonly string ParameterProcessId = "ProcessId"; // Workflow Id - Guid |
| 6 | 21 | | public static readonly string ParameterNewActiveStageId = "NewActiveStageId"; // ProcessStage Id - Guid |
| | 22 | |
|
| 6 | 23 | | public static readonly string ParameterCurrentEntityLogicalName = "CurrentEntityLogicalName"; // string |
| 6 | 24 | | public static readonly string ParameterCurrentEntityId = "CurrentEntityId"; // Guid |
| | 25 | |
|
| 6 | 26 | | public static readonly string ParameterNextEntityLogicalName = "NextEntityLogicalName"; // string |
| 6 | 27 | | public static readonly string ParameterNextEntityId = "NextEntityId"; // Guid |
| | 28 | |
|
| 6 | 29 | | public static readonly string ParameterNewTraversedPath = "NewTraversedPath"; // string |
| 6 | 30 | | public static readonly string ParameterTraversedPath = "TraversedPath"; // string |
| | 31 | |
|
| | 32 | | public bool CanExecute(OrganizationRequest request) |
| 24 | 33 | | { |
| | 34 | | // Since it is a custom OrganizationRequest it can only be execute if the Request Name is correct. |
| 24 | 35 | | return request.RequestName.Equals(RequestName); |
| 24 | 36 | | } |
| | 37 | |
|
| | 38 | | public OrganizationResponse Execute(OrganizationRequest request, XrmFakedContext ctx) |
| 6 | 39 | | { |
| 6 | 40 | | var orgService = ctx.GetOrganizationService(); |
| | 41 | |
|
| | 42 | | // Checking parameters |
| | 43 | |
|
| 6 | 44 | | Guid processId = (Guid)request.Parameters[ParameterProcessId]; // Workflow Id |
| 6 | 45 | | if (processId == null) throw new Exception(ParameterProcessId + " is a required parameter."); |
| | 46 | |
|
| 6 | 47 | | Guid newActiveStageId = (Guid)request.Parameters[ParameterNewActiveStageId]; |
| 6 | 48 | | if (newActiveStageId == null) throw new Exception(ParameterNewActiveStageId + " is a required parameter."); |
| | 49 | |
|
| 6 | 50 | | string currentEntityLogicalName = (string)request.Parameters[ParameterCurrentEntityLogicalName]; |
| 6 | 51 | | if (currentEntityLogicalName == null) throw new Exception(ParameterCurrentEntityLogicalName + " is a require |
| | 52 | |
|
| 6 | 53 | | Guid currentEntityId = (Guid)request.Parameters[ParameterCurrentEntityId]; |
| 6 | 54 | | if (currentEntityId == null) throw new Exception(ParameterCurrentEntityId + " is a required parameter."); |
| | 55 | |
|
| 6 | 56 | | string nextEntityLogicalName = (string)request.Parameters[ParameterNextEntityLogicalName]; |
| 6 | 57 | | if (nextEntityLogicalName == null) throw new Exception(ParameterNextEntityLogicalName + " is a required para |
| | 58 | |
|
| 6 | 59 | | Guid nextEntityId = (Guid)request.Parameters[ParameterNextEntityId]; |
| 6 | 60 | | if (nextEntityId == null) throw new Exception(ParameterNextEntityId + " is a required parameter."); |
| | 61 | |
|
| 6 | 62 | | string traversedPath = (string)request.Parameters[ParameterNewTraversedPath]; |
| | 63 | |
|
| | 64 | | // Actual request logic |
| | 65 | |
|
| | 66 | | // All current Entities (should be only one) |
| 6 | 67 | | var currentEntities = (from c in ctx.CreateQuery(currentEntityLogicalName) |
| 6 | 68 | | where c.Id == currentEntityId |
| 6 | 69 | | select c); |
| | 70 | |
|
| 6 | 71 | | if (!currentEntities.Any() && currentEntities.Count() != 1) throw new Exception(string.Format("There are no |
| | 72 | |
|
| | 73 | | // Current Entity |
| 6 | 74 | | var currentEntity = currentEntities.First(); |
| 6 | 75 | | currentEntity["stageid"] = newActiveStageId; |
| 6 | 76 | | currentEntity["processid"] = processId; |
| 6 | 77 | | currentEntity["traversedpath"] = traversedPath; |
| | 78 | |
|
| 6 | 79 | | orgService.Update(currentEntity); |
| | 80 | |
|
| | 81 | | // All next Entities (should be only one) |
| 6 | 82 | | var nextEntities = (from n in ctx.CreateQuery(nextEntityLogicalName) |
| 6 | 83 | | where n.Id == nextEntityId |
| 6 | 84 | | select n); |
| | 85 | |
|
| 6 | 86 | | if (!nextEntities.Any() && nextEntities.Count() != 1) throw new Exception(string.Format("There are no or mor |
| | 87 | |
|
| | 88 | | // Next Entity |
| 6 | 89 | | var nextEntity = nextEntities.First(); |
| 6 | 90 | | nextEntity["stageid"] = newActiveStageId; |
| 6 | 91 | | nextEntity["processid"] = processId; |
| 6 | 92 | | nextEntity["traversedpath"] = traversedPath; |
| | 93 | |
|
| 6 | 94 | | orgService.Update(nextEntity); |
| | 95 | |
|
| | 96 | | // Response |
| 6 | 97 | | var response = new OrganizationResponse() |
| 6 | 98 | | { |
| 6 | 99 | | // Response name should be equal with Request name to check if the response is corrent. |
| 6 | 100 | | ResponseName = RequestName, |
| 6 | 101 | | Results = new ParameterCollection() |
| 6 | 102 | | }; |
| | 103 | |
|
| | 104 | | // Add TraversedPath parameter |
| 6 | 105 | | response.Results[ParameterTraversedPath] = traversedPath; |
| | 106 | |
|
| 6 | 107 | | return response; |
| 6 | 108 | | } |
| | 109 | |
|
| | 110 | | public Type GetResponsibleRequestType() |
| 4270 | 111 | | { |
| 4270 | 112 | | return typeof(OrganizationRequest); |
| 4270 | 113 | | } |
| | 114 | | } |
| | 115 | | } |