Пример #1
0
        internal void InitiateTransactionHandler(ICDSPluginExecutionContext executionContext)
        {
            var req = (executionContext.CreateOrganizationRequest <ccllc_BTF_InitiateTransactionRequest>());

            if (string.IsNullOrEmpty(req.ContextRecordType))
            {
                throw new ArgumentNullException("ContextRecordType cannot be null.");
            }
            if (string.IsNullOrEmpty(req.ContextRecordId))
            {
                throw new ArgumentNullException("ContextRecordId cannot be null.");
            }

            if (!Guid.TryParse(req.ContextRecordId, out Guid recordId))
            {
                throw new ArgumentException("ContextRecordId cannot be converted to a GUID.");
            }

            var contextRecordId   = new RecordPointer <Guid>(req.ContextRecordType, recordId);
            var transactionTypeId = req.TransactionTypeId ?? throw new NullReferenceException("TransactionTypeId cannot be null");
            var userId            = req.UserId ?? new EntityReference("systemuser", executionContext.InitiatingUserId);

            var systemUser = new SystemUser(userId.LogicalName, userId.Id, userId.Name);

            var platformService = Container.Resolve <IPlatformService>();

            var session = platformService.GenerateSession(executionContext, systemUser);

            var contextFactory     = Container.Resolve <ITransactionContextFactory>();
            var transactionContext = contextFactory.CreateTransactionContext(executionContext, contextRecordId);

            var serviceFactory = Container.Resolve <ITransactionServiceFactory>();
            var service        = serviceFactory.CreateTransactionService(executionContext);

            var transactionType = service.GetAvaialbleTransactionTypes(executionContext, session, transactionContext).Where(r => r.Id == transactionTypeId.Id).FirstOrDefault();

            if (transactionType == null)
            {
                throw TransactionException.BuildException(TransactionException.ErrorCode.TransactionTypeNotFound);
            }

            var transaction = service.NewTransaction(executionContext, session, transactionContext, transactionType);

            IUIPointer uiPointer = null;

            if (transaction.CurrentStep.Type.IsUIStep)
            {
                uiPointer = transaction.CurrentStep.GetUIPointer(executionContext, transaction);
            }

            if (uiPointer == null)
            {
                // When current step is not a UI step, navigate the process forward which will execute process steps
                // until a UI step is found or the process is blocked by a validation issue.
                var step = transaction.NavigateForward(session);
                if (step.Type.IsUIStep)
                {
                    uiPointer = step.GetUIPointer(executionContext, transaction);
                }
            }

            if (uiPointer == null)
            {
                // At this point something is wrong so navigate the user to the transaction error form.
                uiPointer = new UIPointer
                {
                    UIDefinition = "TransactionError",
                    UIRecordId   = transaction.Id,
                    UIRecordType = "ccllc_transaction",
                    UIType       = eUIStepTypeEnum.DataForm
                };
            }

            executionContext.Trace("Adding UI Pointer to output parameters.");
            executionContext.OutputParameters["UIPointer"] = uiPointer.Serialize();
        }