Пример #1
0
        public override void ExecuteCRMWorkFlowActivity(CodeActivityContext executionContext, LocalWorkflowContext crmWorkflowContext)
        {
            ITracingService      tracingService      = executionContext.GetExtension <ITracingService>();
            IOrganizationService organisationService = crmWorkflowContext.OrganizationService;

            tracingService.Trace("Started...");



            EntityReference application = Application.Get(executionContext);

            if (application == null || application.Id == Guid.Empty)
            {
                throw new ArgumentException("Application parameter is invalid", nameof(application));
            }

            // Read the task type ids to filter application tasks by
            List <Guid> taskTypeIds = new List <Guid>();

            AddIdToList(TaskType1.Get(executionContext), taskTypeIds);
            AddIdToList(TaskType2.Get(executionContext), taskTypeIds);
            AddIdToList(TaskType3.Get(executionContext), taskTypeIds);
            AddIdToList(TaskType4.Get(executionContext), taskTypeIds);
            AddIdToList(TaskType5.Get(executionContext), taskTypeIds);
            AddIdToList(TaskType6.Get(executionContext), taskTypeIds);
            AddIdToList(TaskType7.Get(executionContext), taskTypeIds);
            AddIdToList(TaskType8.Get(executionContext), taskTypeIds);
            AddIdToList(TaskType9.Get(executionContext), taskTypeIds);
            AddIdToList(TaskType10.Get(executionContext), taskTypeIds);
            Guid[] taskTypeIdArray = taskTypeIds.ToArray();

            var fetchCrBuilder = "";

            foreach (var task in taskTypeIdArray)
            {
                fetchCrBuilder += "<value>{" + task.ToString() + "}</value>";
            }

            var fetch = @"<fetch>
                              <entity name='defra_applicationtaskdefinition'>
                                <attribute name='defra_applicationtaskdefinitionid' />
                                <filter type='and'>
                                  <condition attribute='defra_tasktypeid' operator='in'>
                                   " + fetchCrBuilder + @"
                                  </condition>
                                  <condition attribute='statecode' operator='eq' value='0' />
                                </filter>
                              </entity>
                            </fetch>";

            tracingService.Trace(string.Format("fetchXML: {0}", fetch));

            tracingService.Trace("Try to run RetrieveMultiple...");
            var appTaskdefs = organisationService.RetrieveMultiple(new FetchExpression(fetch)).Entities;

            if (appTaskdefs.Count == 0)
            {
                return;
            }

            tracingService.Trace("DataAccessApplicationTask");
            DataAccessApplicationTask dalAppTask = new DataAccessApplicationTask(organisationService, tracingService);

            tracingService.Trace("DataAccessApplication");
            DataAccessApplication dalApp = new DataAccessApplication(organisationService, tracingService);

            tracingService.Trace("GetApplicationTypeAndOwner");
            ApplicationTypesAndOwners applicationDetails = dalApp.GetApplicationTypeAndOwner(application.Id);


            foreach (var appTaskdef in appTaskdefs)
            {
                tracingService.Trace(string.Format("Create application task name: {0}", appTaskdef.Id.ToString()));
                dalAppTask.CreateApplicationTask(application.Id, applicationDetails.OwningUser, applicationDetails.OwningTeam, appTaskdef.Id);
            }

            tracingService.Trace("Finished");
        }
Пример #2
0
        /// <summary>
        /// Main code activity methoid
        /// </summary>
        /// <param name="executionContext">Standard CRM execution context</param>
        /// <param name="crmWorkflowContext">Standard CRM workflow context</param>
        public override void ExecuteCRMWorkFlowActivity(CodeActivityContext executionContext, LocalWorkflowContext crmWorkflowContext)
        {
            // 1. SETUP

            ITracingService      tracingService      = executionContext.GetExtension <ITracingService>();
            IOrganizationService organisationService = crmWorkflowContext.OrganizationService;

            tracingService.Trace("Started");

            // Validation
            EntityReference application = Application.Get(executionContext);

            if (application == null || application.Id == Guid.Empty)
            {
                throw new ArgumentException("Application parameter is invalid", nameof(application));
            }

            // Read the task type ids to filter application tasks by
            List <Guid> taskTypeIds = new List <Guid>();

            AddIdToList(TaskType1.Get(executionContext), taskTypeIds);
            AddIdToList(TaskType2.Get(executionContext), taskTypeIds);
            AddIdToList(TaskType3.Get(executionContext), taskTypeIds);
            AddIdToList(TaskType4.Get(executionContext), taskTypeIds);
            AddIdToList(TaskType5.Get(executionContext), taskTypeIds);
            AddIdToList(TaskType6.Get(executionContext), taskTypeIds);
            AddIdToList(TaskType7.Get(executionContext), taskTypeIds);
            AddIdToList(TaskType8.Get(executionContext), taskTypeIds);
            AddIdToList(TaskType9.Get(executionContext), taskTypeIds);
            AddIdToList(TaskType10.Get(executionContext), taskTypeIds);
            Guid[] taskTypeIdArray = taskTypeIds.ToArray();

            // Prepare the DALs
            DataAccessApplicationTask dalAppTask = new DataAccessApplicationTask(organisationService, tracingService);
            DataAccessApplication     dalApp     = new DataAccessApplication(organisationService, tracingService);

            // 2. PROCESSING

            // Get Application Type, SubType and owners
            ApplicationTypesAndOwners applicationDetails = dalApp.GetApplicationTypeAndOwner(application.Id);

            // Which tasks should be linked to the application?
            List <Guid> applicableTasks = dalAppTask.GetTaskDefinitionIdsThatApplyToApplication(application.Id, applicationDetails.ApplicationType, applicationDetails.ApplicationSubType, taskTypeIdArray) ?? new List <Guid>();

            // Which tasks are already linked?
            List <ApplicationTaskAndDefinitionId> existingTasks = dalAppTask.GetApplicationTaskIdsLinkedToApplication(application.Id, taskTypeIdArray) ?? new List <ApplicationTaskAndDefinitionId>();

            // Deactivate application tasks that no longer apply
            List <Guid> tasksToRemove = existingTasks.Where(existingTask => !applicableTasks.Contains(existingTask.ApplicationTaskDefinitionId))
                                        .Select(t => t.ApplicationTaskId)
                                        .ToList();

            tasksToRemove.ForEach(dalAppTask.DeactivateApplicationTask);

            // Create application tasks that apply
            List <Guid> tasksToAdd = applicableTasks.Where(applicableTask => existingTasks.All(t => t.ApplicationTaskDefinitionId != applicableTask)).ToList();

            tasksToAdd.ForEach(newtask => dalAppTask.CreateApplicationTask(application.Id, applicationDetails.OwningUser, applicationDetails.OwningTeam, newtask));

            tracingService.Trace("Done");
        }