Introduction
Building workflows in K2 is great. We can drag and drop events onto the canvas and click through wizards that tell us what to do. We can set up line rules, tasks and so on with just a mouse, which is fantastic as it’s quick and easy. We can also deploy a workflow by following a simple wizard.
But when we want to change something in an email, or change the form a task is pointing to, it means opening up the solution, making the changes and redeploying. And then only new instances of that workflow pick up those changes.
So what do we do? Well, we do what I tell all my clients: we make everything in the workflow that could be dynamic, dynamic. We store the URL of a form in a data store somewhere, and the same for email content or business rules like escalation values. We basically unchain the workflow from the data. We free it, so we can make these types of changes from outside the workflow. We don’t have to go back into the workflow, make the change and redeploy. We just go to a simple form, make the change, and any workflow pointing to that value automatically picks it up. Sounds brilliant, doesn’t it?
Over the years my colleagues and I have slowly been putting a framework, or pattern, together. It doesn’t have a fancy name, so throughout this article I am just going to call it the framework.
The framework
The framework is designed to make workflows adaptable to change without having to redeploy them each time. It’s also designed to make building a workflow quicker, and to give everyone involved, from developers and administrators to users, a complete overview of what is happening.
Some of the features of the framework are:
- Headless forms. Task forms are dynamic, so at run time we can choose which form the task will get.
- Externalised process data. External data fields are created outside of the workflow, which makes workflows more efficient and lets the data be deleted once the instance has finished instead of persisting. Key data for the process type is also externalised, so it can be changed without redeploying the workflow.
- Audit trail. Every workflow has an audit trail so users can keep track of important changes within the flow of an instance.
- Workflow registration. Every workflow is registered, so users, developers and admins can see which workflows are running, whether they are related to another workflow, and how complete an instance of a workflow type is.
Components of the framework
- Process management: externalised data fields, configuration data and time slots
- Swagger definition library
- Script library
- Form library
To use the framework within your workflow, we need to set up a few things inside the workflow and also outside it.
Setting up a workflow to use the framework
Create key data fields
The first thing we need to do is create the three main data fields that every workflow using the framework should have. All three are strings.
- DataFieldId will hold the external data field key
- K2FID will hold the key value for the registered process instance
- ProcessTypeId holds the key for the type of process
There will probably be a fourth data field not mentioned above, which will be the primary key for the main business data.
Create references
To make life easier when it comes to designing and developing workflows, the framework recommends using references to create a shortcut to a SmartObject method and its properties. To create a reference, go to the data tab (the third one), right-click on References and follow the instructions.
The references we need to create are:
- Configuration Data: SmartObject K2F.SMO.ProCon, method GetProcessConfig, parameter ProcessTypeId (data field). This contains values for the process type.
- External Datafields: SmartObject K2F.SMO.Datafields, method Get Datafields, parameter DataFieldId (data field). This contains data for the instance of the process.
Like the data fields, you may also have a third one for your main business data.
The Setup activity
Every workflow that uses the framework must have an activity called ‘Setup’. This activity has a number of things to do:
- Register the instance of itself, to help keep track of the instance
- Create the data field key, so we can use external data fields
- Start auditing the key stages of the workflow
Within your workflow, create an activity called ‘Setup’.
Getting the process type ID
In the Setup activity, drag in a Data Event and give it the name ‘Get Process TypeId’. The source is the K2F.SMO.ProcType SmartObject, method GetTypeByProcessName, with the process name as the parameter and ProcessTypeId as the return property. The destination is the ProcessTypeId data field.
Now we have the process type ID, the workflow can get all the framework information relating to this workflow type.
Creating a data field key
In the Setup activity, drag in another Data Event and call it ‘Create DataField Key’. The source is K2F.SMO.DataFields, method Create Key, return property DataFieldKey. The destination is the DataFieldId data field.
DataFieldId is what we will use to group the process instance’s data fields together.
Creating a folio
In the Setup activity, drag in a Data Event and call it ‘Set Folio’. The source is free text and the destination is [Context Browser].[Workflow Context Browser].[Process Instance].Folio.
Registering the instance of a workflow
Registering the instance against the workflow type lets the framework keep track of how many instances of a particular type are running, and make sure the correct details and other key values are being used.
Create a SmartObject event inside the Setup activity and name it ‘Record Workflow’. It uses the K2C.SMO.ProcInst SmartObject and the Register Process Instance method, with these parameters:
- pParentProcessId (number): 0, unless this workflow is being called via IPC or a case action, in which case the parent process instance ID is required
- pProcessInstanceId (number): ProcessId from the process instance in the context browser
- pStatus (text): Active
- pViewFlow (memo): View Flow from the process instance
- pStartDate (date): ProcessStartDate from the process instance
- pBusinessKey (text): 0, unless the primary key of the main business data is known
- pProcessTypeId (GUID): the ProcessTypeId data field
- pProcessId (number): 0
- PFolio (text): ProcessFolio from the process instance
- pOriginator (text): ProcessOriginatorFQN from the process instance
- pDataFieldKey (GUID): the DataFieldId data field
The returned ID is assigned to the K2FID data field.
Creating an audit entry
Now that the instance is registered against the process type, we need to make an entry in the audit log to show that the instance has started. Create a SmartObject event inside the Setup activity and call it ‘Add to Audit’. It uses K2F.SMO.Audit and the Add Audit method:
- pDetails (text): ‘Starting [name of workflow]’, or whatever free text is appropriate
- pProConfigId (number): 0
- pProcessInstanceId (number): ProcessId from the process instance
- pCreatedDate (date): ProcessStartDate from the process instance
- pCreatedBy (text): ProcessOriginatorFQN from the process instance
The ‘Add to Audit’ event can be copied and pasted into other activities in the workflow. You just need to change pDetails and pCreatedDate.
The Setup activity should now look like this:
The workflow is now set up to use the framework.
Client events with the framework
With the framework, the form’s URL is dynamic and stored externally, so the form can be changed at any point without going back into the workflow. To set this up:
- Drag a Default Client Event onto the canvas or into an activity
- Give your event a name
- Check ‘Task Item URL’
- Add the K2F.SMO.Client SmartObject’s ClientEventURL method, passing pClientEventName (ActivityInstanceName from the activity instance in the context browser) and pProcessTypeId. It returns FormUrl.
- Make a note of the name of the activity you have put the client event in
- Link the form to the client event in the form library (see ‘Adding a form to a task’ below)
Updating the framework with a workflow instance’s status
The framework likes to keep track of the status of the registered workflow. This lets us do reporting and offer additional functionality based on its status. To update the framework with a status change:
- Create a SmartObject event
- Select the K2F.SMO.ProcInst SmartObject
- Select the Update Status method
- For the input mapping pStatus, enter in free text what the status is
- For the pId input parameter, use the K2FID data field
Like the audit event, this can be copied into other activities where the status needs to be updated.
Dynamic escalation and start rules
When there needs to be an escalation on an activity or process, we can use the framework to make the time frames dynamic. To set up the escalation:
- Click on the escalation icon on the activity
- Click Next and give the escalation a name
- Select a rule template. In this case I am going to select ‘Escalate After’
- Click Next
- Click in Days to bring up the context browser
- Add the K2C.CMN.SMO.Time SmartObject’s Read method. For pTimeId, use the Configuration Data reference, returning ProcessConfigValue, with the filter ProcessConfigName = TaskTime. It doesn’t have to be ‘TaskTime’; it can be whatever you have called the reference. The return property is Days.
- Repeat the last two steps for hours, minutes and seconds, changing the return property to match. For example, if you are on hours, the return would be Hours.
- Make a note of the name you passed into ProcessConfigName, for example ‘TaskTime’
You can also do the same for a start rule.
The End activity
Every workflow must have an ‘End’ activity. In this activity we delete all the external data fields relating to the instance, update the status of the process instance and add a final entry to the audit table for the instance.
Deleting external data fields
- Create a SmartObject event
- Select the K2F.SMO.DataField SmartObject
- Select the Delete Data Fields method
- It will ask for pDataFieldKey, so assign it the DataFieldId data field
- Complete the event. When it runs it will delete all the data fields created for this instance, so the workflow cleans up data it no longer needs.
Audit trail and status
The End activity also needs an entry in the audit to say the workflow has finished, and the framework’s process instance status needs to be updated to ‘Complete’.
Creating an external data field
- Drag a SmartObject event onto the canvas
- Call the event ‘Create DataField’ or something similar
- Find the K2F.SMO.DataField SmartObject in the context browser
- Select the Create Data Field method
- Supply the method with pDataFieldDescription (a description of the data field), pDataFieldKey (the data field key we created earlier), pDataFieldTitle (the name we are giving the data field), pDataFieldValue (its value) and pProcessInstanceId (the ID of the process instance)
- Click Next and Finish
We have now created an external data field. Repeat the steps for any others you need.
Using an external data field
To use an external data field in the event or line rule you are in:
- Open the context browser
- Go to the data tab (the third tab)
- Expand References
- Expand External Datafields
- Drag DataFieldValue into the section where you want the value
- The filter window opens. Click Add
- In Left, select DataFieldTitle from the drop-down list
- The operation should be ‘=’
- In Right, enter the name of the data field you want the value of
- Click Next
- Make sure DataFieldValue is selected and click Next
- Make sure ‘Return a single item’ is checked and click Finish
We have now called the external data fields to get the value for a particular data field.
Line rules
We can also use the framework for line rules and other workflow logic.
- Right-click on a line
- Give the line an appropriate name, prefixed with ‘ln’, for example ‘lnVotingAge’
- In the label, prefix it with ‘LR’ followed by what the rule is doing
- Open the rule and click Add
- Open the context browser for the first variable
- Expand References, then Configuration Data
- Drag ProcessConfigValue into the Variable section
- In Filter, click Add
- Select ProcessConfigName and the ‘=’ operation
- In Right, enter the keyword for the value, for example ‘AgeLimit’
- Click Next, Next again, then Finish
- In the second variable, put in your business data as normal
We now have a dynamic line rule where both the business data and the value it’s compared against come from outside the workflow.
Managing the framework
Adding a form to the form library
All forms, whether SmartForms or HTML5 forms, must be registered so the framework can use them in workflows.
- Go to the form K2F.ADM.SMF.ProcessManager
- Click Add on the form library
- Enter the name of the form
- Enter the complete URL of the form
- Tick the checkbox
- Click Save
The form is now registered and can be used.
Adding a form to a task
For default client events to get a form in a workflow, we need to link the form and client event together.
- Click on Client Event
- Search for the form you want in the form library
- Give the client event name the same name as the activity the client event is in
- Click Save
We can now use this form in the workflow as described above.
Swagger definition library
The framework supports managing the Swagger definitions of the REST broker. It will do this in two ways:
- A Web API handler that accepts posts. When the global registry changes, it posts to the service with the new host address and application name. The service finds the correct Swagger definition in the library, updates it with the new host address, exports the definition file to the Swagger folder on the server and then updates K2 with the new definition. This one is still on the to-do list.
- In the portal you can view all the Swagger definitions, add new ones and edit old ones. Once created in the library, a Swagger JSON file is created on the K2 server.
To add a new Swagger file:
- Click ‘Add Swagger Definition’
- A form loads where you enter the definition name, the host address and the definition itself
- Click Submit. This saves the definition in the library and creates its JSON file on the server.
- You will still need to create the service instance manually using the Service Object Tester tool
Script library
Some SmartForms may need additional scripts, such as the K2C.CMN.VWI.Footer view. As with the workflows, instead of hard-coding the script into the view and checking the view out and in every time there’s a change, the script library lets us make the change from the management tool without the risk of breaking the view or form.
From the script library you can add and edit scripts. When you have finished, click Save.
Registering a process
- Deploy the process. This can be done at any time during the workflow setup, but must be done before the following steps.
- Go to the form K2F.ADM.SMF.ProcessManager
- Click ‘Register Process Type’
- In the pop-up window, enter the workflow name, a description and a display name
- Click Submit. We have now told the framework about this workflow.
We can now start adding configuration data about the workflow.
Process configuration data
In the process configuration form we can add and edit a workflow’s configuration data. You get to this form either by clicking on the process type or by adding a new process type.
Adding time configuration
This is the time configuration used for the escalations and start rules mentioned above.
- Click ‘Add Time’
- The time configuration window opens
- Enter a name and description
- Select a value from the drop-down list for each of the time sections
- Click Submit
To use the time frame in a workflow, we now need to link it to the configuration data.
Configuration data
Configuration data holds information that will be needed for every instance of a particular workflow.
- Click Add
- Enter a description of the config data being added
- Enter the actual value
- Enter a name for the config data
In this example we are adding the link to the time frame we created earlier.
As you can see, the framework makes workflows flexible and lets us build apps quickly and easily. Now that it’s been introduced, next time we will look at what else it can do and build a workflow using it.