Saturday, 16 November 2019

BPF




  static void Main(string[] args)
        {
            ClientCredentials clientCredentials = new ClientCredentials();
            clientCredentials.UserName.UserName = "";
            clientCredentials.UserName.Password = "";
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
            organizationService = (IOrganizationService)new OrganizationServiceProxy(new Uri(""), null, clientCredentials, null);
            AdvanceBusinessProcessFlow(organizationService);

        }

        private static void AdvanceBusinessProcessFlow(IOrganizationService service)
        {
            // Retrieve all process instances
            RetrieveProcessInstancesRequest instanceRequest = new RetrieveProcessInstancesRequest
            {
                EntityId = new Guid("bceb89f8-4808-ea11-a811-000d3af02cd4"),
                EntityLogicalName = "new_student"
            };
            RetrieveProcessInstancesResponse instanceResponse = (RetrieveProcessInstancesResponse)service.Execute(instanceRequest);

            // First record is the active process instance
            Entity activeProcessInstance = instanceResponse.Processes.Entities[0];
            var activeProcessId = activeProcessInstance.Id;
            var activeStageId = new Guid(activeProcessInstance.Attributes["processstageid"].ToString());

            // Retrieve the process stages in the active path of the current process instance
            RetrieveActivePathRequest activePathRequest = new RetrieveActivePathRequest
            {
                ProcessInstanceId = activeProcessId
            };

            // System.ServiceModel.FaultException exception occurs here
            RetrieveActivePathResponse activePathResponse = (RetrieveActivePathResponse)service.Execute(activePathRequest);

            string activeStageName;
            int? activeStagePosition = null;
            int stageCount = activePathResponse.ProcessStages.Entities.Count;

            // Iterate through all process stages and identify active stage
            for (int i = 0; i < stageCount; i++)
            {
                if (activePathResponse.ProcessStages.Entities[i].Attributes["processstageid"].ToString() == activeStageId.ToString())
                {
                    activeStageName = activePathResponse.ProcessStages.Entities[i].Attributes["stagename"].ToString();
                    activeStagePosition = i;
                }
            }       

            // Auto-advance active stages of BPF to last stage so that BPF can be auto-finished
            while (activeStagePosition < stageCount - 1)
            {
                // Retrieve the stage ID of the next stage to be set as the active stage
                var newActiveStageId = (Guid)activePathResponse.ProcessStages.Entities[(int)++activeStagePosition].Attributes["processstageid"];

                // Retrieve the process instance record to update its active stage
                ColumnSet columnSet = new ColumnSet();
                columnSet.AddColumn("activestageid,traversedpath");
                Entity retrievedProcessInstance = service.Retrieve("new_testbpf", activeProcessId, columnSet);
                // Update active process stage
                retrievedProcessInstance["activestageid"] = new EntityReference("new_testbpf", newActiveStageId);
                service.Update(retrievedProcessInstance);
            }
        }

No comments:

Post a Comment