Summary

Class:FakeXrmEasy.FakeMessageExecutors.CustomExecutors.NavigateToNextEntityOrganizationRequestExecutor
Assembly:FakeXrmEasy
File(s):C:\code\jordimontana82\fake-xrm-easy\FakeXrmEasy.Shared\FakeMessageExecutors\CustomExecutors\NavigateToNextEntityOrganizationRequestExecutor.cs
Covered lines:57
Uncovered lines:0
Coverable lines:57
Total lines:115
Line coverage:100%
Branch coverage:50%

Metrics

MethodCyclomatic ComplexitySequence CoverageBranch Coverage
CanExecute(...)1100100
Execute(...)789.1955.56
GetResponsibleRequestType()1100100
.cctor()1100100

File(s)

C:\code\jordimontana82\fake-xrm-easy\FakeXrmEasy.Shared\FakeMessageExecutors\CustomExecutors\NavigateToNextEntityOrganizationRequestExecutor.cs

#LineLine coverage
 1using System;
 2using System.Linq;
 3using Microsoft.Xrm.Sdk;
 4
 5namespace 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    {
 617        public static readonly string RequestName = "NavigateToNextEntity";
 18
 19        // Required Parameters - comment on each key describes the output type of value = request.Prameters[key]
 620        public static readonly string ParameterProcessId = "ProcessId"; // Workflow Id - Guid
 621        public static readonly string ParameterNewActiveStageId = "NewActiveStageId"; // ProcessStage Id - Guid
 22
 623        public static readonly string ParameterCurrentEntityLogicalName = "CurrentEntityLogicalName"; // string
 624        public static readonly string ParameterCurrentEntityId = "CurrentEntityId"; // Guid
 25
 626        public static readonly string ParameterNextEntityLogicalName = "NextEntityLogicalName"; // string
 627        public static readonly string ParameterNextEntityId = "NextEntityId"; // Guid
 28
 629        public static readonly string ParameterNewTraversedPath = "NewTraversedPath"; // string
 630        public static readonly string ParameterTraversedPath = "TraversedPath"; // string
 31
 32        public bool CanExecute(OrganizationRequest request)
 2433        {
 34            // Since it is a custom OrganizationRequest it can only be execute if the Request Name is correct.
 2435            return request.RequestName.Equals(RequestName);
 2436        }
 37
 38        public OrganizationResponse Execute(OrganizationRequest request, XrmFakedContext ctx)
 639        {
 640            var orgService = ctx.GetOrganizationService();
 41
 42            // Checking parameters
 43
 644            Guid processId = (Guid)request.Parameters[ParameterProcessId]; // Workflow Id
 645            if (processId == null) throw new Exception(ParameterProcessId + " is a required parameter.");
 46
 647            Guid newActiveStageId = (Guid)request.Parameters[ParameterNewActiveStageId];
 648            if (newActiveStageId == null) throw new Exception(ParameterNewActiveStageId + " is a required parameter.");
 49
 650            string currentEntityLogicalName = (string)request.Parameters[ParameterCurrentEntityLogicalName];
 651             if (currentEntityLogicalName == null) throw new Exception(ParameterCurrentEntityLogicalName + " is a require
 52
 653            Guid currentEntityId = (Guid)request.Parameters[ParameterCurrentEntityId];
 654            if (currentEntityId == null) throw new Exception(ParameterCurrentEntityId + " is a required parameter.");
 55
 656            string nextEntityLogicalName = (string)request.Parameters[ParameterNextEntityLogicalName];
 657             if (nextEntityLogicalName == null) throw new Exception(ParameterNextEntityLogicalName + " is a required para
 58
 659            Guid nextEntityId = (Guid)request.Parameters[ParameterNextEntityId];
 660            if (nextEntityId == null) throw new Exception(ParameterNextEntityId + " is a required parameter.");
 61
 662            string traversedPath = (string)request.Parameters[ParameterNewTraversedPath];
 63
 64            // Actual request logic
 65
 66            // All current Entities (should be only one)
 667            var currentEntities = (from c in ctx.CreateQuery(currentEntityLogicalName)
 668                                   where c.Id == currentEntityId
 669                                   select c);
 70
 671             if (!currentEntities.Any() && currentEntities.Count() != 1) throw new Exception(string.Format("There are no 
 72
 73            // Current Entity
 674            var currentEntity = currentEntities.First();
 675            currentEntity["stageid"] = newActiveStageId;
 676            currentEntity["processid"] = processId;
 677            currentEntity["traversedpath"] = traversedPath;
 78
 679            orgService.Update(currentEntity);
 80
 81            // All next Entities (should be only one)
 682            var nextEntities = (from n in ctx.CreateQuery(nextEntityLogicalName)
 683                                where n.Id == nextEntityId
 684                                select n);
 85
 686             if (!nextEntities.Any() && nextEntities.Count() != 1) throw new Exception(string.Format("There are no or mor
 87
 88            // Next Entity
 689            var nextEntity = nextEntities.First();
 690            nextEntity["stageid"] = newActiveStageId;
 691            nextEntity["processid"] = processId;
 692            nextEntity["traversedpath"] = traversedPath;
 93
 694            orgService.Update(nextEntity);
 95
 96            // Response
 697            var response = new OrganizationResponse()
 698            {
 699                // Response name should be equal with Request name to check if the response is corrent.
 6100                ResponseName = RequestName,
 6101                Results = new ParameterCollection()
 6102            };
 103
 104            // Add TraversedPath parameter
 6105            response.Results[ParameterTraversedPath] = traversedPath;
 106
 6107            return response;
 6108        }
 109
 110        public Type GetResponsibleRequestType()
 4270111        {
 4270112            return typeof(OrganizationRequest);
 4270113        }
 114    }
 115}