In this blog, we will show how to use the “Plan per slot (No destination)” feature in K2 to loop through a list of items and execute a set of events for each item in a K2 workflow – this is a pattern required for older versions of K2.
K2 Automated Testing Software
rnBenchQA allows full test automation of K2, including fully automated K2 SmartForms and K2 Workflow testing. It promotes test driven development for K2 and ensures continued quality assurance for K2 solutions. Easily apply changes to test cases to accommodate changes to K2 apps and ensure all apps are regression tested to avoid defects and assure continuous quality.
The previous versions of K2 don’t have the for each functionality, and this blog will illustrate how to loop through items in a K2 workflow.
We will use a simple leave request process to illustrate the solution. Each employee has a number of leave days, and when the New Year starts, they gets a number of days for the year.
Here is an example of a business policy for leave allocation –
- Employed at company for less than 5 year = allocated 20 days
- Employed at company for more than 5 year = allocated 25 days
We will assume a database table exists that has the employee’s details and their allocated leave totals.
Let’s create the solution –
First we need to create the SmartObject that will retrieve the data and make this available in the K2 workflow as part for a “Get List” SmartObject method.
Let’s assume we need to create a workflow instance for each of the records above. We will use a K2 IPC event to start a process instance for each of these records. We will have a parent K2 workflow that will instantiate multiple instances of a child workflow process. To do this –
Create a parent K2 workflow process named EmployeeWF and add the following data field
- EmployeeID
Create a child K2 workflow process named LeaveDayUpdateWF with the following data fields –
- EmployeeId
- Age
In the EmployeeWF, change the destination rule in advance mode. Set the destination rule option to “Plan per slot (No destination)”. This will allow us to start an instance of each item from the SmartCbject GetList method.
Ensure the ID is selected from the SmartObject Get List method
4 – Destination Rule Configuration
Click Finish.
Next, let’s configure the IPC event that will call the child workflow process – this will happen for each result returned in the Get List SmartForm method –
Select the process name and set the folio. The value of the “ID” will be available in the ActivityInstanceDestInstanceData K2 context value and can be used to create a meaningful folio number.
Again, the Employee ID is available in the ActivityInstanceDestInstanceData K2 context, so this value can be passed into the child workflow process.
To configure the parent process to wait until all the child process instances are completed, configure the succeeding rule to ensure that all the instances have completed.
In summary, the blog shows how to use an alternative way to loop through a set of items retuned by a K2 SmartObject Get List method and start a process instance for each; waiting for all instances to complete before the parent continues.