protected abstract JobRunStatus ProcessSuccessPaging(JobPagingInfo pagingInfo);
 protected abstract JobRunStatus ProcessFailurePaging(ExecutionFailures failures, JobPagingInfo pagingInfo);
Example #3
0
        protected ExecutionFailures Execute(JobPagingInfo pagingInfo)
        {
            var result = new ExecutionFailures();

            var  contextService = GetServiceInContext();
            var  targets        = pagingInfo.Targets;
            Guid workflowId;

            if (Guid.TryParse(Job.ActionName, out workflowId) ||
                ((workflowId = Job.Workflow.GetValueOrDefault()) != Guid.Empty))
            {
                log.Log($"Running workflow '{workflowId}' ...");

                if (targets?.Any() == true)
                {
                    foreach (var target in targets)
                    {
                        try
                        {
                            log.Log($"Running workflow on '{target}'.");
                            contextService.Execute(
                                new ExecuteWorkflowRequest
                            {
                                EntityId   = target,
                                WorkflowId = workflowId
                            });
                        }
                        catch (Exception ex)
                        {
                            log.Log($"Failed at '{target}':'{ex.Message}'.");
                            result.Exceptions[target] = ex;

                            if (Job.ContinueOnError == false)
                            {
                                break;
                            }
                        }
                    }
                }
                else
                {
                    log.Log("No targets to run workflow on.");
                }

                log.Log($"Finished running workflow '{workflowId}'.");
            }
            else
            {
                var action = new OrganizationRequest(Job.ActionName);

                if (!string.IsNullOrEmpty(Job.SerialisedInputParams))
                {
                    log.Log($"Inputs: '{Job.SerialisedInputParams}'.");
                    action.Parameters.AddRange(GetInputs());
                }

                log.Log($"Executing action '{Job.ActionName}' ...");

                if (targets?.Any() == true)
                {
                    foreach (var target in targets)
                    {
                        try
                        {
                            log.Log($"Executing action on '{target}' ...");
                            action["Target"] = new EntityReference(Job.TargetLogicalName, target);
                            contextService.Execute(action);
                        }
                        catch (Exception ex)
                        {
                            log.Log($"Failed at '{target}':'{ex.Message}'.");
                            result.Exceptions[target] = ex;

                            if (Job.ContinueOnError == false)
                            {
                                break;
                            }
                        }
                    }
                }
                else if (string.IsNullOrEmpty(Job.TargetLogicalName))
                {
                    try
                    {
                        log.Log($"Executing global action ...");
                        contextService.Execute(action);
                    }
                    catch (Exception ex)
                    {
                        log.Log($"Failed: '{ex.Message}'.");
                        result.Exceptions[Job.Id] = ex;
                    }
                }

                log.Log($"Executed action '{Job.ActionName}'.");
            }

            return(result);
        }