コード例 #1
0
        /// <summary>
        /// Executes the WorkFlow.
        /// </summary>
        /// <param name="crmWorkflowContext">The <see cref="WorkFlowActivityBase.LocalWorkflowContext"/> </param> which contains the
        /// <param name="executionContext" > <see cref="CodeActivityContext"/>
        /// </param>
        /// <remarks>
        /// For improved performance, Microsoft Dynamics 365 caches WorkFlow instances.
        /// The WorkFlow's Execute method should be written to be stateless as the constructor
        /// is not called for every invocation of the WorkFlow. Also, multiple system threads
        /// could execute the WorkFlow at the same time. All per invocation state information
        /// is stored in the context. This means that you should not use global variables in WorkFlows.
        /// </remarks>
        public override void ExecuteCRMWorkFlowActivity(CodeActivityContext executionContext, LocalWorkflowContext crmWorkflowContext)
        {
            crmWorkflowContext.TracingService.Trace("Workflow started");

            if (crmWorkflowContext == null)
            {
                throw new ArgumentNullException("crmWorkflowContext");
            }

            try
            {
                //Todo: active only
                //todo: exclude a record. so sibling distribution is possible
                //todo: Many-to-many
                //todo: FetchXML
                var wfRef = DistributedWorkflow.Get <EntityReference>(executionContext);

                if (wfRef == null)
                {
                    crmWorkflowContext.TracingService.Trace("Distributed Workflow Input is null.");
                    throw new NullReferenceException("Distributed Workflow");
                }

                // Get parent record ID
                var parentUrl = ParentRecordUrl.Get(executionContext);
                if (String.IsNullOrEmpty(parentUrl))
                {
                    throw new NullReferenceException("ParentRecordUrl");
                }
                var parser = new DynamicUrlParser(parentUrl);

                var relationshipName = RelationshipName.Get(executionContext);
                if (String.IsNullOrEmpty(relationshipName))
                {
                    throw new NullReferenceException("RelationshipName WF Input Parameter");
                }

                var keys = GetKeys(parser.GetEntityReference(crmWorkflowContext.OrganizationService), relationshipName, crmWorkflowContext.OrganizationService);

                this.Distribute(wfRef.Id, keys, crmWorkflowContext.OrganizationService, crmWorkflowContext.TracingService);
            }
            catch (FaultException <OrganizationServiceFault> e)
            {
                // Handle the exception.
                crmWorkflowContext.TracingService.Trace("Exception: {0}\r\nInner Exception {1}", e.Message,
                                                        e.InnerException);
                throw e;
            }

            crmWorkflowContext.TracingService.Trace("Workflow ended");
        }
        /// <summary>
        /// Executes the WorkFlow.
        /// </summary>
        /// <param name="executionContext" > <see cref="CodeActivityContext"/>
        /// <param name="crmWorkflowContext">The <see cref="WorkFlowActivityBase.LocalWorkflowContext"/> which contains the
        /// </param>
        /// <remarks>
        /// For improved performance, Microsoft Dynamics 365 caches WorkFlow instances.
        /// The WorkFlow's Execute method should be written to be stateless as the constructor
        /// is not called for every invocation of the WorkFlow. Also, multiple system threads
        /// could execute the WorkFlow at the same time. All per invocation state information
        /// is stored in the context. This means that you should not use global variables in WorkFlows.
        /// </remarks>
        public override void ExecuteCRMWorkFlowActivity(CodeActivityContext executionContext, LocalWorkflowContext crmWorkflowContext)
        {
            if (crmWorkflowContext == null)
            {
                throw new ArgumentNullException("crmWorkflowContext");
            }

            try
            {
                var wfRef = DistributedWorkflow.Get <EntityReference>(executionContext);
                if (wfRef == null)
                {
                    crmWorkflowContext.TracingService.Trace("Distributed Workflow Input is null.");
                    throw new NullReferenceException("Distributed Workflow");
                }

                // Get initial record reference
                var initialRecordUrl = InitialRecordUrl.Get(executionContext);
                if (initialRecordUrl == null)
                {
                    throw new NullReferenceException("InitialRecordUrl");
                }
                var parser           = new DynamicUrlParser(initialRecordUrl);
                var initialRecordRef = parser.GetEntityReference(crmWorkflowContext.OrganizationService);
                if (initialRecordRef == null)
                {
                    crmWorkflowContext.TracingService.Trace("Cannot convert from the record URL {0} to a entity reference", initialRecordUrl);
                    throw new NullReferenceException("initialRecordRef");
                }

                var relationshipName = RelationshipName.Get(executionContext);
                if (String.IsNullOrEmpty(relationshipName))
                {
                    throw new NullReferenceException("relationshipName");
                }

                var keys = GetKeys(initialRecordRef, relationshipName, crmWorkflowContext.OrganizationService);

                this.Distribute(wfRef.Id, keys, crmWorkflowContext.OrganizationService, crmWorkflowContext.TracingService);
            }
            catch (FaultException <OrganizationServiceFault> e)
            {
                // Handle the exception.
                throw e;
            }
        }