Sending tasks is easy to do in K2, but making them adaptable from outside the workflow is a bit more tricky. A colleague of mine, Bill Irvine, came up with a pattern to standardise the way tasking is developed and used. I have taken his idea and come up with my own interpretation of it, which is why I am calling this Bill’s tasking workflow pattern.

Getting started

To get started we need the following SmartObjects. I have included the SQL for the tables and stored procedures. When the framework is released it will be included as part of that.

Tables

Tasking relies on the following tables.

Task Library

Contains all the task types. It holds the ID of the form that the task will navigate to, and whether the task needs to send a notification email.

CREATE TABLE [dbo].[TaskLibrary](
[TaskTypeId] [uniqueidentifier] NOT NULL,
[TaskName] [nvarchar](50) NULL,
[FormId] [uniqueidentifier] NULL,
[TaskDescription] [nvarchar](max) NULL,
[EmailNotification] [bit] NULL,
[Escalation] [bit] NULL,
[EscalationTimeId] [uniqueidentifier] NULL,
[EmailTemplateId] [uniqueidentifier] NULL,
[Slots] [int] NULL,
CONSTRAINT [PK_TaskLibrary] PRIMARY KEY CLUSTERED
(
[TaskTypeId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

Note: if you’re not using the framework, change the FormId data type from uniqueidentifier to nvarchar(100).

Task Actions

Task Actions contains all the possible actions for a particular task type.

CREATE TABLE [dbo].[TaskActions](
[TaskActionId] [uniqueidentifier] NOT NULL,
[TaskTypeId] [uniqueidentifier] NULL,
[TaskAction] [nvarchar](50) NULL,
[TaskValue] [int] NULL,
CONSTRAINT [PK_TaskActions] PRIMARY KEY CLUSTERED
(
[TaskActionId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

Tasks

Tasks contains the destination users for the task type, and the task’s serial number against a particular task type and process instance ID.

CREATE TABLE [dbo].[Tasks](
[TaskId] [uniqueidentifier] NOT NULL,
[WorkflowType] [uniqueidentifier] NULL,
[SN] [nvarchar](50) NULL,
[Username] [nvarchar](50) NULL,
[BusinesObjectId] [nvarchar](50) NULL,
[ProcessInstanceId] [int] NULL,
[TaskTypeId] [uniqueidentifier] NULL,
[TaskAction] [nvarchar](50) NULL,
[DestinationKey] [uniqueidentifier] NULL,
CONSTRAINT [PK_Tasks_1] PRIMARY KEY CLUSTERED
(
[TaskId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

All the tables have basic CRUD methods to manage the data, and there are also some additional stored procedures to help with the tasking pattern.

SmartObject

The tasking SmartObject is called K2C.CMN.SMO.Tasking. It manages the creation of task types and their related task actions, and it also manages the destination users’ task details.

Properties

  • TaskId (GUID): destination user task ID for an instance of the task
  • ProcessTypeId (GUID): process type ID
  • Username (text): FQDN of the destination user
  • BusinessObjectId (text): primary key of the main business data
  • ProcessInstanceId (number): ID of the process instance
  • SN (text): serial number for the task
  • TaskTypeId (GUID): ID for the type of task
  • TaskAction (text): action for the task
  • TaskActionId (GUID): action ID for a particular task type action
  • TaskValue (number): the value that relates to the actual action in the workflow, either 1, 2 or 3
  • TaskName (text): name of the task type
  • FormId (GUID): form ID from the form library
  • TaskDescription (text): description of the task type
  • EmailNotification (yes/no): whether the task needs to send an email

SmartObject methods

Each method maps to a stored procedure:

  • Create Task Action (sp_TaskActionsCreate): creates a new action for the task type
  • Delete Task Action (sp_TaskActionsDelete): deletes the action for the task type
  • Get All Task Actions (sp_TaskActionsList): gets a complete list of the task actions
  • Get Task Action (sp_TaskActionRead): gets an action for a task type
  • Update Task Action (sp_TaskActionUpdate): updates an action in the task type
  • Create Task Type (sp_TaskLibraryCreate): creates a new task type
  • Delete Task Type (sp_TaskLibraryDelete): deletes a task type
  • Get All Task Types (sp_TaskLibraryList): gets all task types
  • Get Task Type (sp_TaskLibraryRead): gets a particular task type
  • Update Task Type (sp_TaskLibraryUpdate): updates a task type
  • Create Task (sp_TasksCreate): creates a new task entry
  • Delete Task (sp_TasksDelete): deletes the task
  • Get All Tasks (sp_TaskList): gets a list of tasks
  • Get Task (sp_TaskRead): gets a particular task
  • Update Task (sp_TaskUpdate): updates the task
  • Get Tasks By Type (sp_TaskActions): gets all actions by task type
  • Update Task Action (sp_TasksUpDateAction): updates the destination user task’s action
  • Task Result (sp_TaskResult): gets the result of the task
  • Create Destination Key (so_CreateDataFieldKey): creates a group key for destination users

Workflow

This version is very simple and only works for one slot, but it could be expanded to manage multiple slots.

Process data fields

The process data fields needed for this workflow are:

  • TaskId (string, in): so we know which task type
  • ParentProcessInstanceId (number, in): the parent process instance ID
  • DestinationKey (string)
  • Expire (Boolean, in): stops the start activity going to Expire; set it to false
  • ActionResponse (string, out): the action value returned

Here is the simple tasking pattern:

Activities

  • Setup: links the workflow to the framework
  • Send Email: sends an email based on a template
  • Task: creates the task based on the task type ID
  • Action 1, 2, 3: records the action returned
  • Expire: responds to the task escalating
  • End: tidies everything up based on the framework

Setup

The Setup activity does all the framework registration.

Send Email

This activity records the task with the notification service. It also calls the email service and passes in the template ID from the task type reference.

Task

This adds the serial number to the destination user record in Tasks, and the activity also has a client event. It gets the task URL from the form library, and the destination user comes from the task reference. It has three actions (1, 2 and 3).

Action 1, 2, 3

These record the action against the task by getting the action name from the ‘Get Task Action’ method, and they set the data field ‘ActionResponse’ with the action value.

End

This does the framework clean-up, such as deleting external data fields.

Notes

Remember to audit log, both for the task workflow itself and for the parent workflow, so we can record the action.

Line rules

  • LR Email Notification Required: the method ‘Is Email Required’ returns true
  • LR No Email Notification Required: the method ‘Is Email Required’ returns false
  • Start to Expire: the data field ‘Expire’ = ‘True’

References

The tasking pattern workflow needs two references: Destination and Task Type.

Destination reference

Gets a list of destination users for a particular task type.

  • In the Data (third) tab of the context browser in K2 Studio or Visual Studio, right-click on ‘References’ and click ‘Add’
  • Give the reference the name ‘Destination’
  • Select the method ‘K2C.CMN.SMO.Tasking.Get Destination Users’
  • For the input property, use ‘DestinationId’
  • Click ‘Next’ and then ‘Finish’

Task Type reference

Gets information about the task type, such as whether an email should be sent and which form to use for the task.

  • In the Data (third) tab of the context browser in K2 Studio or Visual Studio, right-click on ‘References’ and click ‘Add’
  • Give the reference the name ‘Task Type’
  • Select the method ‘K2C.CMN.SMO.Tasking.Get Task Type’
  • For the input property, use the ‘Task Id’ data field
  • Click ‘Next’ and then ‘Finish’

Building the tasking workflow

  • Create an activity on the canvas and name it ‘Setup’
  • If you are using the framework, add the related framework events. Otherwise just put a placeholder event in there for now
  • Create two more activities called ‘Send Email’ and ‘Task’
  • Create a line between ‘Setup’ and ‘Send Email’ and another line between ‘Setup’ and ‘Task’
  • Right-click on the line between ‘Setup’ and ‘Send Email’ and select Properties
  • In General properties, give it the label ‘LR Email Notification Required’
  • Click on the ‘Line Rule’ section and click on ‘Add’
  • In the rule editor, for the ‘First Variable’ open the context browser and go to the third tab
  • Expand References, expand ‘Task Type’, and select ‘Email Notification’. Either drag it into ‘First Variable’ or click on ‘Add’
  • The comparison operator should be ‘=’ and the second variable ‘True’
  • Click ‘OK’ and then ‘Finish’
  • We have now added a rule that checks whether an email notification is required
  • Do the same for the other line, except call it ‘LR No Email Notification Required’ and make the second variable ‘False’

The email event

  • Add an email event in the ‘Send Email’ activity
  • Give it a name like ‘Send Email’
  • Select ‘Originator’ and uncheck ‘Specify’ in ‘Recipient’

Task activity

In the Task activity we need to add a SmartObject event that records the serial number of the task, and we also need to add a client event.

Recording the serial number

  • Drag the SmartObject event into the Task activity
  • Give the event the name ‘Add SN’
  • Select the SmartObject method ‘Add Task SN’ from the SmartObject ‘K2C.CMN.SMO.Tasking’
  • Click ‘Next’
  • In ‘Input Mapping’, click on ‘pSN’ and then ‘Assign’
  • Expand Process Instance and select ‘Id’, type ‘_’, then expand ‘Activity Destination Instance’ and select ‘Id’
  • Click on ‘pTaskId’ and ‘Assign’, expand the ‘Destination’ reference and use Task Id
  • This saves the task serial number against the destination user

Adding the user task

  • Drag a client event into the Task activity and name it ‘User Task’
  • Check ‘Task Item URL’
  • If you’re not using the framework, open the context browser, go to the third tab, expand ‘References’, expand ‘Task Type’ and drag in ‘FormId’ (or select it and click on ‘Add’)
  • Click on ‘Next’ and ‘Next’ again
  • In Actions, click on ‘Add’. In the ‘Add Action’ window, enter 1 as the name and click on ‘OK’
  • Repeat this, adding 2 and 3 as actions
  • Click ‘Next’ and ‘Next’ again
  • In Destination Users, click on ‘Add’ and open the context browser
  • Go to the third tab, expand References, expand ‘Destination’, click on ‘Username’ and click on ‘Add’
  • Click ‘Next’ and then ‘Finish’
  • We have now created a generic task

Recording the action

  • Create a default activity and name it ‘1’
  • Drag a SmartObject event into it and name it ‘Task Action’
  • For the SmartObject method, use ‘Update Task Action’ from ‘K2C.CMN.SMO.Tasking’
  • Click ‘Next’
  • In Input Mapping, click on ‘pTaskAction’ and then on ‘Assign’, which opens the context browser
  • Go to the first tab, expand SmartObject Server(s), expand the SmartObject ‘K2C.CMN.SMO.Tasking’, then expand the method ‘Get Task Action’
  • Select the ‘TaskAction’ property and click on ‘OK’
  • This method needs some additional properties: ‘pTaskValue’ needs to be 1, and ‘pTaskTypeId’ needs to be the data field ‘TaskId’
  • Click ‘Next’ and ‘Next’ again, select ‘Task Action’ and click ‘Next’
  • Make sure ‘Return a single item’ is checked
  • Click on ‘Finish’ and then ‘OK’
  • What we have just done is get the text value of the action for this task type with an action value of 1
  • pTaskId just needs to be Task Id from the Destination reference
  • Click on ‘Finish’
  • Copy this activity, rename it to ‘2’ and change ‘pTaskValue’ to 2
  • Do the same again for ‘3’
  • These activities now record the action of the task against the destination user
  • We also need to record the action in the data field ‘ActionResponse’, so repeat the ‘Get Task Action’ mapping above in a data event for each of the three activities. This lets the parent workflow easily retrieve the action
  • Connect the outcome lines to the activities we just created
  • Last of all, create an activity called ‘End’ with a placeholder called ‘End’ and join the three activities to it

Your tasking workflow should look something like this:

The workflow could be extended to allow multiple slots and multiple destinations. The great thing about a tasking pattern is that it reduces development and testing time, and it allows actions to be added and removed dynamically without going into the actual workflow.

It also allows for generic task forms that just point to the tasking workflow. They only need the task type ID to load up the correct actions for that task type.

Below is a brief explanation of how to use it. I will go into more detail in a later article; for now it’s just about exploring the idea of a tasking pattern and how it would work.

How to use the tasking workflow

  • Add the tasking workflow to the solution
  • Create a new workflow
  • Use the SmartObject event with the ‘Create Destination Key’ method and bind the result to a string data field called ‘DestinationKey’
  • Use the ‘Create Task’ method to add the destination user, task type ID and destination key
  • Add the IPC event and point it to the tasking workflow
  • Choose synchronous
  • Map the data fields ‘TaskId’ and ‘DestinationKey’
  • For the return value, map the tasking workflow’s ‘ActionResponse’ data field to the parent’s ‘ActionResponse’ data field
  • Then use a line rule to direct the workflow based on ‘ActionResponse’