Building a Workflow with K2 Five

This ‘How To’ goes through the steps of building a simple workflow using the new workflow designer in K2 Five.

K2 Five is a complete redesign of the web based workflow designer and it also forces the developers to rethink how they develop their workflows.  The new designer is not just aimed at citizen developers like the previous version of the web based designer found in K2 Blackpearl, but  now at  professional developers as well.

K2 Five Workflow designer has all the power of K2 Studio, but with the benefit of being all web based, so there is no need to install client tools on a developers PC or having to install Silverlight plugin  on a compatible browser as it is all now HTML 5.

 

Case Study

GreenEggs Studio needs a simple annual leave request solution to replace their existing paper based system. They have decided to use K2 to build the solution because of it’s rapid development and low / no code proposition. As it is their first application in K2 they are going to keep the process small and simple.  They also want it to get the leave request out into the business as quick as possible so they are taking an agile MVP approach so they can iterate or add features based on user feedback instead of assumptions.

The process map looks like this

Leave Request

As you can see it’s a very simple process.

  1. An employee fills out and submits the annual leave form.
  2. Their line manager receives an email asking them to review the request
  3. If the line manager declines the request, the employee receives an email saying it has been declined and the status is updated
  4. If the line manager approves it, the employee receives an email saying it has been approved and the status is updated

 

Getting Started

Simple workflow

So lets get started with designing a workflow with K2 Five.  We are going to build a simple workflow to create annual leave requests.  The Smartforms and SmartObjects have already been built.

  1. Go to the K2 Five DesignerDashboard
  2. Click on ‘Workflow’
  3. The workflow design wizard will load up and ask to enter in a name and the location of where you want the workflow to be saved to.workflowwizard
  4. Lets give the workflow a name like ‘HR.WKF.AnnualLeave’, the way I have named the workflow  is that so we can identify what solution the workflow belongs to. In this case the ‘HR’ solution.workflowname
  5. Now lets choose the location, click on the blue ‘workflow’ link underneath the workflow name. A window will popup showing a list of folders.
  6. Select the ‘HR’ folder’ and click on ‘Ok’. If there is not a folder there already then click on ‘Create Folder’ and enter in the name ‘HR’.folder
  7. On ‘Ok’, the folder window will close and the folder location under the name will say ‘HR’folderchange
  8. Now click on ‘Create’ button, this will now take us into the new design canvas.create
  9. When the canvas loads up, we are presented with a smart assist helper to give us a brief introduction into the new workflow designer. You can click on the left and right arrows to view the helpful clips and then click on close after you have finished.welcome
  10. Welcome to the new K2 Five workflow Designer 🙂 welcome1
  11. The Smart Assist is always present within the new designer and when you are new to the designer it will suggest what to do next. You can either just click ‘x’ and it will close the smart assist for this workflow or you can click ‘Got It’ and it will assume you know what you are doing. In this case I am going to just going to click on ‘x’ to close it.
  12. Once the smart assist is closed the first thing you will notice is now simple the designer now looks.  There are a couple of things that are very different if you have used K2’s previous workflow designers both web and studio.  Activities, steps and events are gone and now replaced with ‘Shapes’ So throughout the rest of this guide you will notice I will be using  term ‘Shape’ instead of event. So for example ‘I am going to drag the ’email shape’ onto the canvas instead of the email event’
  13. So on the canvas we start with the ‘Start shape’ and you can see it has put a maker where we can place the next shape.start
  14. So now that we have a canvas to build the annual leave process on. On the lefthand side of the canvas we have a toolbox of all the shapes that the workflow can carry out like creating a task, sending an email etc..  To view the shapes click on ‘>’ to expand the toolbox.
    expandtoolbox
    Click to expand

    toolbox
    Expanded Toolbox
  15. The first shape we are going to drag onto the canvas is a Task. Drag the task shape taskicon into the placeholder on the canvas. The canvas should look like the image below. A ‘!’ explanation mark will be displayed against the task shape, don’t worry about that, K2 is just letting us know it’s not yet configured.taskcanvas
  16. Now Click on the Task shape on the canvas and now the righthand side configuration panel icons will change to show the task configurable options.
  17. Click on ‘<‘ on the configuration panel to expand the options.taskconfig1
  18. The first tab in the task configuration options is where we can give some instructions to the person(s) receiving the task about what the task is about. It is also where we can say what actions the person(s) interacting with the task can perform.
  19. So lets add some basic instructions “Please review the details of the leave request and either  click on ‘Approve’ or ‘Decline’instructions
  20. Next we are going to add the actions for the task. For the standard actions like ‘Approve and ‘Reject’, K2 have made it easy for us by giving us them the default options which we can just click on to create the actions.defaultactions
  21.   So lets just click on ‘Both’, the actions will appear and K2 Smart Assist will appear showing how to create action paths. Click on ‘x’ close to close the Smart Assist.defaultactions1
  22. Now click on the next tab in the configuration options, here we can choose which form will be used for the task.  We can choose from the following types
    1. Basic Task Form, K2 autogenerates a form which is presented to the user to choose a specific action
    2. Smartform, we can select an existing Smartform to use from the library
    3. Custom, we can enter in a custom form by putting in the URL of the form we are going to use.
    4. No form, it is possible to have a task that has no form for the user to go to.
  23. For now we are going to use ‘Basic Task Form’, we will go back later and change it to a specific form.basicform
  24. Next we choose who will receive the task, so we need to click on the next tab to choose the recipient.recipeint
  25. By default the task will goto the person who submits the leave request. So we need to change this to be their line manager. Click on ‘Originator’ and select ‘Originator Manager’ from the list. Now the task when generated will go the manager of the person who submitted the leave request.recipeintLinemANAGER
  26. Looking at the process map, the line manager also needs to receive an email notification to say that they have a leave request to complete. Click on the next tab down with letter icon. Here we can decide if the line manger will get an email notification. We can either use the standard notification email or we click on ‘Customise Notification’ and create a custom notification email which can include actual business data that is relevant to the task. For now we are just going to use the standard notification.NOTIFICATION
  27. For this walkthrough we will leave the other options for another time. We have now configured a basic task for the line manager to receive once a member of their team submits a leave request.
  28. Double click on the word ‘Task’ replace the text with ‘Line Manger Review’linemangerreview
  29. Now to create the decision branches on the workflow, left click and drag from the bottom of the task shape to create a new line and placeholder.linemangerreview.actionsPNG
  30. Now drag the blue diamond shape (decision shape) bluediamond into the placeholder. K2 will then pick up we have two actions from the previous shape and will automatically create the decision outcomes based on those actions.linemangerreview.action1
  31. Now lets deal with the ‘Rejected’ decision branch.
  32. In the process map, when a leave request has been rejected, we need to send an email to the employee who submitted the leave request. In the toolbox on the left hand side drag the email shape emailshape on to the canvas. Your canvas should look the below imagerejectedemail
  33. Now left click on the  orange dot next to ‘Rejected’ and drag it on to email shape we just dragged onto the canvas.rejectedemail1
  34. Now lets configure the email, first of double click on the ‘Send Email’ text and enter in ‘Reject Email’
  35. Click on the email shape again and expand the configuration panel on the righthand side.configurerejectemail
  36. We don’t need to change ‘To’ as it is already set to ‘Originator’ which is the person who submitted the leave request form.
  37. In the Subject enter in the text ‘Leave Request Status’
  38. In the Body section for now just enter the following text ‘Hi, Your Leave request that was submitted to your line manager has been declined.’ We will come back to this later on and enter in some business data that relates to the actual leave request. But for phase 1 we will keep it very simple.configurerejectemail1
  39. Now  lets do the ‘Approved’ decision branch, like with the rejected branch drag another email shape from the toolbox  onto the canvas. approvedemail
  40.  Now drag the orange ‘Approved’ dot approved dot to the email the email shape.approvedemail1
  41. Click on the email shape we just dragged onto the canvas and then expand the email configuration panel on the righthand side of the canvas.approvedemail2
  42. In the Subject enter ‘Leave Request Status’ and in the Body section enter in the following ‘Hi, your line manager has approved your leave request’approvedemail3.PNG
  43. Now click on the next tab in the email configuration panel, here we can change the name of the email shape from ‘Send Email’ to ‘Approve Email’. We can also use this section to enter in additional notes about the email shape.approvedemail4.PNG
  44. In the name section, remove the text ‘Send Email’ and change it to ‘Approve Email’approvedemail5
  45. We have now configured the email for when the leave request has been approved by the line manager. The canvas should now look like the image below.approvedemail6
  46. Now to finish off the workflow we now need to add a ‘End’ shape. In the toolbox of shapes it is the Red Square end. Drag this on to the canvas underneath the other shapes. It should look like the image below.end1
  47. Now we can connect the ‘Approve Email’ and ‘Reject Email’ shapes to the end. Like when connecting the decision branch to the email shape. Just left click and drag from the bottom of the email shape to the end shape. Your canvas should now look like this.end2
  48. Now we have completed a very a simple version of the leave request. The leave request gets submitted. The line manager of user who submitted the leave request then receives an email asking them to approve or reject the leave request. The user who submitted the leave request then will get an email telling them of the decision that was made by their line manager.

Using SmartObjects inside the leave request workflow

  1. The leave request has a SmartObject,
    1. HR.CMN.SMO.LeaveRequest, this holds the leave request details like leave dates, number of days, status, who created it and when.leavesmo
  2. So now lets edit the workflow we created earlier to use the data from the SmartObject HR.CMN.SMO.LeaveRequest. To do this we need to add a variable to hold the LeaveRequest number. This is so the workflow can find the correct leave request details.
  3. Click on ‘Start’ shape and then click on the configuration panel on the righthand side of the canvas. Then click on ‘<+>’ symbol in the top right corner as circled in the image below.datafield
  4. This will expand the ‘Context Browser’ for the canvas. The context browser will show any references, variables and environment fields that are currently being used in the workflow. It also gives you access to workflow details and workflow functions.
  5. As this is the first time we have used the context browser in this workflow. We just a brief description of what references and variables are.  Under Variables click on ‘Add Variable’.datafield1
  6.  A new variable called ‘New Data Field’ is created. Click on the text ‘New Data Field’ and delete the text and replace it with the following text ‘LeaveRequestId’.datafield2datafield3
  7. The next thing we need to do is change the data type of this variable. At the moment it is set to ‘Text’ as we need the variable to store a number click on ‘…’ and click on ‘Settings’.datafield4
  8.  We are now in the settings for the variable, under ‘Data Type’ you will notice it says   ‘Text’.datafield5
  9. Click on the drop down list that says ‘Text’ under ‘Data Type’ and choose ‘Number’ and click on ‘Ok’.datafield6
  10. We have now created and configured a variable to hold the related Leave Request Number. We now need to tell the workflow which data to retrieve. To do this we need to create a short cut to the SmartObject ‘HR.CMN.SMO.LeaveRequest.
  11. Expand the shape toolbar on the lefthand side of the canvas and click on ‘Basic’.REFERENCE
  12. We will now see all the shapes under the ‘Basic’ category.  The shape we want is called ‘Create Reference’.REFERENCE1
  13. Click and drag it on to the canvas. REFERENCE2
  14. Click on the ‘Create Reference’ shape and then expand the configuration panel on the righthand side of the canvas. The first thing you will notice that we have to do is select a SmartObject.createreference
  15. Click on ‘Select SmartObject’, this will bring up a window to select a category where the SmartObject is stored.createreference1
  16. The SmartObject we want is under ‘HR’ – ‘SmartObjects’. Click on ‘HR’ , then click on ‘SmartObjects’ and then select the SmartObject ‘HR.CMN.SMO.LeaveRequest’ and click on ‘OK’.createreference2
  17. Now that we have selected the SmartObject, we now need to select which method the reference will use to get the correct data.  Click on ‘Select Method’ and select ‘Load’.createreference3
  18. Because we have selected the ‘Load’ method, it requires some input data to retrieve the correct row of data. In this case the load method requires that a value is passed in the property named ‘Id’.createreference4
  19. If the ‘Context Browser’ is not expanded click on ‘<+>’ symbol in the top right corner of the panel. We will now see the variable we created earlier, now drag that into the box labeled ‘Type / Drop’ next to the property labelled ‘ID’. It should look like the image below.createreference5
  20. Now that we have configured the reference, to retrieve data where the value of the property ‘ID’ is the same as the value of the variable named ‘LeaveRequestId’. If you  you know SQL we are essentially doing this ‘Select * from HR.CMN.SMO.LeaveRequest where ID = @LeaveRequestId‘.
  21. Click on the next tab in the configuration panel and under ‘Name’ replace the text ‘Create Reference’ with ‘Leave Request Details ‘.createreference7createreference8
  22.  Click on ‘>’ to close the configuration panel’, we have now created a short cut to the leave request data. We now need to link the reference into the workflow so the workflow will retrieve the correct data before the task for the Line Manager is created.
  23. On the canvas, disconnect the line going from the ‘Start’ shape to the ‘Line Manager Review’ shape. As shown in the image below.createreference9
  24. Now drag the reference shape named ‘Leave Request Details’ in between the shapes ‘Start’ and ‘Line Manager Review’. Then connect the line from the ‘Start’ shape to the ‘Leave Request Details’. Then left click and drag a line down from the bottom of the ‘Leave Request Details’ shape to the top pf the ‘Line Manager Review’ shape. Your canvas should now look like the image below.createreference10
  25. Now that the workflow has access to the leave request details that was entered in the submitted leave request form we can now give some context to the emails that are sent out as part of the workflow.
  26. Lets start with the email notification to the line manager of the person who submitted the leave request. Click on the task shape labeled ‘Line Manager Review’. Expand the shape’s configuration panel and click on the notification tab (4th tab down). It should look like this.leavemanagerreviewEmailData
  27. To edit the notification click on ‘Customise Notification’.
  28. Now we can configure what information the email notification will contain. Click on the context browser icon ‘<+>’.leavemanagerreviewEmailData1
  29. Now under ‘References’ we will see the reference we created earlier.  Under the reference section click on ‘HR.CMN.SMO.LeaveRequest’. This will expand the reference and we will now see all the properties.leavemanagerreviewEmailData2
  30. All these properties can be used in the email notification. Lets start with the ‘Subject’ for the email. In the box under ‘Subject’ enter the following text ‘Leave Request from ‘ and then from the list of properties, drag employee into the box so it’s after ‘Leave Request from ‘. As shown in the image below.leavemanagerreviewEmailData3
  31. Now that we have done the ‘Subject’, we can now edit the body of the email.
  32.  We can just drag in the properties that you want to be shown in the email. In this example I have added the following properties to the body
    1. Employee
    2. Start Date
    3. End Date
    4. Number of Days
  33. I haven’t deleted any of the information that was already in the email. The body of the email should look like the below image.leavemanagerreviewEmailData4
  34. If you want to expand the editor for the body of email click on ‘Open Text Editor’. This will  you a full on text editor to edit the email body.leavemanagerreviewEmailData5
  35. Click on ‘Close Text editor’ to close the editor, once finished. We have now configured the notification email to contain data from the leave request. To help the line manager make their decision on whether to approve the leave request.
  36. We can also do the same for the ‘Approve’ and ‘Reject’ Email. Click on the ‘Reject’ email shape and expand the configuration panel and then drag across any of the reference properties into the email Subject and Body.  rejectedemailData
  37. Do the same for the ‘Approve’ email shape.
  38. Now we have configured all the email notifications to contain relevant data about the leave request.
  39. The next thing we need to do is update the status of the Leave Request, once the line manager has approved or rejected the request so it can be recorded against the request.
  40. To do the update, expand the shape panel and click on ‘Basic’ and then drag the ‘SmartObject Method’ shape on to the canvas.UpdateStatusUpdateStatus1
  41. Click on the ‘SmartObject Method’ shape and then expand the configuration panel on the righthand side of the canvas.UpdateStatus2
  42. Like in setting up the reference, we need to select the ‘SmartObject’ that the shape is going to call. Because we have already used the SmartObject ‘HR.CMN.SMO.LeaveRequest’ will appear already in the list when you click on ‘Select SmartObject’.UpdateStatus3
  43. Now that we have selected which SmartObject we are going to use, we now need to select the correct method. Click on ‘Select Method’, this will show us a list of methods that we can call from the SmartObject. Because we are doing an update we will need to select the ‘Save’ method.UpdateStatus4
  44. Now we need to select which properties we are going to update. Under properties click on ‘+’  and click on the  ‘ID’ and ‘Status’ checkboxes in the list.smopatch
  45. In the box next to ‘Status’ enter in the text ‘Approved’ and  drag the variable ‘LeaveRequestId’ into box next to ‘ID’.patch
  46. As we don’t need to map any return properties  from the SmartObject click on ‘Do not map outputs’ under ‘Output Mappings’.
  47. Click on the 2nd tab in the configuration panel and replace the text ‘SmartObject Method’ under Name with ‘Update Status To Approved’UpdateStatus8
  48. Click on ‘>’ to close the configuration panel and your canvas should look like the below image.UpdateStatus9
  49. We need to link this SmartObject shape into the workflow ‘Approved’ path. To do that just drag it on top of the ‘Approve Email’ shape. UpdateStatus10
  50. You can then choose which order the shapes are stacked by moving them up down the list. The shape at the top of the stack will execute first and then once it has finished, it will then move on to the next shape in stack.
  51. Once you are happy with order of the shapes, your canvas should look like this.UpdateStatus11
  52. Repeat steps 40 – 51 and instead of ‘Approved’ in the status box, enter in ‘Declined’patch1
  53. Change the name of the shape to ‘Update Status To Declined’UpdateStatus13
  54. Drag the shape onto the ‘Reject Email’ shape.UpdateStatus14
  55. Now we can deploy, to do that click on ‘File’ and then click on ‘Deploy’. deploy
  56. You will know it has been deployed Successfully as you will get a message saying it has been successful. deploy1

Linking the Smartforms to the workflow

Now that we have got business data inside the workflow,  we just need to link the  Leave Request form to start the workflow and the Line Manager Review Task to action the task and take the workflow down a specific decision path.

  1. Edit Smartform ‘HR.CMN.SMF.LeaveRequest’ and click on the ‘Rules’ tab.smartform1
  2. Now in the rules tab, click on the rule ‘When btnSubmit is clicked’ and click on ‘Edit’smartform
  3. We now need to add a rule to start the workflow when the submit button is clicked.edit
  4. Click on the ‘Actions’ tab and in the search box enter ‘Start’edit1
  5. Click on the action named ‘Start a Workflow’ it will then appear in the rule execution panel.edit2
  6. Now click on ‘select Workflow’ linkedit3
  7.  A list of workflows on the environment will be listed, expand the folder ‘HR’ and then select the workflow we have created ‘HR.WKF.LeaveRequest’ and then click on ‘Ok’.edit4
  8. Now back in the editor and then click on ‘Configure’.  In the new the window we can configure what data contains in the workflow, such as the leave request Id.edit5
  9. In the context browser expand ‘Parameters’ and drag the parameter ‘Id’ into the box labelled ‘LeaveRequestId’.edit6
  10. In the box labelled ‘Folio’ enter in the following text ‘Leave Request Number ‘ and then drag the parameter ‘Id’ into the same box. So it should look like this.edit7
  11. Click on ‘Next’ and then ‘Finish’ and then ‘Ok’. Click on ‘Finish’ in the top left hand corner to save the form.  We have now setup the Leave Request form to start the approval workflow.finishedform
  12. Now we need to edit the task form to pick up the Line Manager Review actions. This  will be the form that the Line Manager opens up to review the leave request details. The line manager will then either click on the ‘Approve’ button to approve the leave request or the line manager will click on ‘Reject’ to reject the leave request.
  13. In the designer, expand ‘HR’ and then expand ‘Smartforms’ and click on the form named ‘HR.CMN.SMF.LeaveRequestReadOnly’.editleaverequesttask
  14. Click on ‘Edit’.
  15. We are now in the Smartform editor, you will notice that the form has two buttons
    1. Approve
    2. Reject
  16. We are going to link these buttons up to the actions/decisions on the workflow.editleaverequesttask1
  17. Click on the ‘Rules’ tableaverequesttaskrule
  18. Click on ‘Add Rule’
  19. In the events tab click on the event ‘When a control on the form raises an event’buttononclick
  20. In the rule designer click on the blue ‘select control’, from the list select ‘btnSubmit’.buttononclick1
  21. Now click on the ‘Actions’ tab, in the search box enter the word ‘action’ and select the action ‘action worklist item’.buttononclick2
  22. Now click on ‘Select a worklist item’, in the pop up window expand ‘HR’ and the select the workflow we  deployed earlier on in this example. buttononclick3
  23. Expand the workflow and then click on ‘Line Manager Review’ and click on ‘OK’buttononclick4
  24. Now, that we have selected the task, we can now configure what happens when the task is actioned. Click on ‘Configure’buttononclick5
  25. In the context browser ‘expand’ parameters and drag the parameter ‘SN’ into the SN  box and in the ‘Action Name’ enter the text ‘Approve’ as this button is dealing with the ‘Approve’ Action. Click on ‘OK’.buttononclick6
  26. In the actions tab, in the search box enter ‘Close’ and click on ‘Close the browser window’ and click on ‘Ok’. buttononclick7
  27. Now when the ‘Approve’ button is clicked it will tell the workflow to go down the ‘Approve’ decision line and to close the form.
  28. We now need to do the same for the Reject button, select control ‘btnReject’ and repeat steps 19 – 27. Enter in the text ‘Reject’ into the Action Name box.buttononclick8
  29. We have now told the form to handle the response of the buttons to take the workflow down a certain path.
  30. Now we to handle what happens to the task, when form is opened.
  31. In the rule editor click on the rule ‘When the Form is Initializing ‘ and click on ‘Edit Rule’buttononclick9
  32. In the actions tab enter in the word ‘Open’ in the search box and then select the action ‘Open a worklist item’.buttononclick10
  33. Click on open worklist item and select the ‘Line Manager Review’ task, like we did in step 23.buttononclick11
  34. Click on ‘Ok’ and now click on ‘Configure’buttononclick12
  35. Expand Parameters and drag the parameter ‘SN’ to the ‘SN’ box. Click on ‘Next’ and then ‘Finish’ and then click on ‘Ok’.buttononclick13
  36. Click on ‘Finish’ and now we have completed setting up the task form, to interact with the workflow.buttononclick14
  37. All we need to do now is tell the workflow to use the task form and not automatically generated form it will create when the ‘Line Manager Review’ task is activated.
  38. Copy the form url, by clicking on ‘Runtime url’ and then copy the url from the address bar.urlurl2
  39. Now edit the workflow ‘HR.WKF.LeaveRequest’ and click on the shape ‘Line Manager Review’.
  40. Expand the configuration panel for the ‘Line Manager Review’ and click on the 2nd tab.
  41. Under form type select ‘Custom’ and in the URL box copy in the URL we copied from the step 38.url3
  42. Under Parameters click on ‘+’ , as we need to add an additional parameter, so the task form knows which leave request data to load.
  43. Under ‘Serial Number’ enter in the text ‘Id’url5.PNG
  44. Now expand the context browser, if not already expanded and drag the variable into the box next to ‘Id’ and underneath ‘Task Serial Number’ like in the image below.url4
  45. Now when the task form is opened, the form will retrieve the workflow task information and also retrieves the leave request details.
  46. Click on ‘Save’ and then click on ‘Deploy’
  47. We have now completed and finished building  our first app with K2 Five.

 

Slack and Simple K2 Integration

One of the big messaging apps for team collaboration is Slack and  is perfect platform to demo how to integrate K2 into it. This will be the first article on how we go about doing this.  Slack is free to use and so is great to demo with and it has an ever growing list of third party integration plugins to play with.

So before we start with the demo,what would we expect a K2 slack plugin to behave? I believe it behave in the following way.

  1. Send notifications of tasks that we have to actiontask
  2. Be notified of when task has been completed and what the action was or the workflow has moved to a certain stage or when there is a workflow escalation.
  3. To be able to action a task from inside Slackhomepage_example_hiretron
  4. To be ask Slack what are my outstanding tasks or what is the status of a certain workflow

So lets starts with points 1 and 2 and deal with simple notifications.

 

Building a Simple messaging app for Slack

Lets start with a simple example, where we can K2 notification messages to Slack, whether its a public message , message to a particular group or a message to an individual person.

First of all we need to sign up for Slack and create a team, which you can down from here . Now we have a slack team, we just need to here  to get access to API for Slack.

api_slack

We are starting simple, so click on “Incoming webhooks” and then click on the link ‘ incoming webhook integration

introwebhooks

Building the web hook url

  1. Choose the channel you want to send the messages to, don’t we will be able to override this later on.

introwebhooks12. Click on the green button ‘Add incoming Webhook integration’

3. You can how see your web hook url, copy that.

introwebhooks24. Further down you can also customize the actual message. I have opted for a K2 look.

introwebhooks35. Click on ‘Save’, we have now created are web hook for incoming messaging.

Slack endpoint assembly

Now we have the web hook, we can how write some code, so K2 can use it. We are going to use a endpoint assembly for this. So we are going to create a class that will take the endpoint and allow us to pass in a message, a optional username and optional group.

private static void PostMessage(Payload payload)
 {
 Encoding _encoding = new UTF8Encoding();
 Uri endpoint = new Uri("web hook here");

string payloadJson = JsonConvert.SerializeObject(payload);
 
 using (System.Net.WebClient client = new System.Net.WebClient())
 {
 System.Collections.Specialized.NameValueCollection data = 
new System.Collections.Specialized.NameValueCollection();
 data["payload"] = payloadJson;

var response = client.UploadValues(endpoint, "POST", data);

//The response text is usually "ok"
 string responseText = _encoding.GetString(response);
 }
 }

Simple code for posting a payload of information to the web hook url

public static void PostMessage(string text, string username = null, string channel = null)
 {
 Payload payload = new Payload()
 {
 Channel = channel,
 Username = username,
 Text = text
 };

PostMessage(payload);
 }

The actual public static method, we will be creating a SmartObject from and then using inside a workflow.

We can then build the solution and take the dll  and now tell K2 about it using the ‘Endpoint Assembly broker’. If you don’t know how to do that view my previous post on creating an Endpoint Assembly.

K2Service

Now just build a SmartObject that uses the service instance you just created.

Slack SMO

We can test it in the SmartObject tester

smotester

When it executes we get this response in Slack

smoslack

No that the SmartObject has been created, we can now use this method inside a workflow

Workflow

I am just going to use a simple workflow for this, that has one task and two actions.

test-workflow

We just going to use a SmartObject event to call the Slack notification SmartObject to send a message to the destination user when a task is generated and then a message to the originator when the task is approved or rejected.

SlackSMOEVENT1

SlackSMOEVENT2.PNG

SlackSMOEVENT4

We do something similar for the approve and reject activities, except we put in the originator name and the message is that the task has been approved or rejected depending on the activity.

When we run the workflow the destination user gets this message in Slack

task

With a link to the task, when they action the task the originator will get this message

task1

Next time we will expand on this by making the notifications more advanced and by allowing the user to ask questions about K2.

The source code for this example can be downloaded from here

Testing a workflow inside a Smartform

So I have posted a few articles on testing in K2, we have looked at Unit Testing,  Testing Smartforms and then just recently more examples of testing a workflow.  After writing that article I realised that there was something missing. What about building a workflow testing tool inside of a Smartform. As it all well and good that we can write unit tests for testing a workflow, but that relies on the workflow developer being able to write code and being that the whole idea of K2 is to build low code , no code solutions. Then there should be away of doing it without the developer having to write code.

There are a number of tools out in the market and they do absolutely fantastic job of testing workflows without having to write code. But I wanted to see if it could be done inside a Smartform.

So my challenge is to build a Smartform App that would test a workflow, with out the tester having to write any code.

The app had to do the following

  1. Start any workflow and check that it has started
  2. Check that tasks have been created and the correct number
  3. Action a task
  4. Checking the task has been completed
  5. Check that the workflow has finished
  6. Check the workflow has gone down the direct path.

The app should all be in Smartforms and should be easy to use.

So lets see how I got on.  So i am going to start with the finished tool and then i will take us through it.

Testing Dashboard

The dashboard shows all the current tests that have been performed and shows the overall percentage of pass and failures.

dashboard

From here you can also click on ‘Create Test’ button to create a new test. Selecting an existing test and clicking on ‘Run Test’ will  run an existing test. Finally double clicking on a test will open up the details about the test.

Test Details

So the test details shows the following information

test details

  1. Workflow being tested
  2. If has already been tested
  3. When it was tested
  4. Overall Pass or Fail
  5. What percentage passed and failed
  6. What was tested and whether each test passed or failed.
  7. It also shows any other tests relating to the workflow being tested.

From the test details you also have the ability to retest the test as well.

Creating a new test

From the dashboard click on ‘Create Test’ button.

test builder.PNG

  1. In the new test form, give the test a name
  2. Select a workflow that you are going to test. Once selected you will then be able to see all the activities of that workflow beneath.test-builder1
  3. Select the activities that the workflow will go throughtest-builder2
  4. The last section is where we build up the actual the tests and what results we expect from those tests.test-builder3

Building a simple test script

The workflow we have selected is a very simple one. It has the following activities

  1. Setup
  2. Task
  3. Approve
  4. Reject
  5. End

test-workflow

Task is where there is a client event which has one slot and goes to the originator.

Now that we have got the basic test details and what route we expect the test to go. We can now build are test script.

  1. Select ‘Start Process’ from the test type drop down list
  2. You will notice that the other text boxes in the row, will be pre populated with information and some will be disabled depending on what information the particular test type requires.
  3. Now we choose comparison sign we are going to use it can be either
    1.  =
    2. >
    3. >=
    4. <
    5. <=
  4. We are going to choose >
  5. In the expected result / comparison box lets enter 0, this is because if workflow starts successfully we expect a value greater than 0.
  6. So ‘Start Process’, needs to know the ‘Folio’ to give the workflow, you can either enter in something. Or if you leave it blank it will take the name we have given earlier to the test.
  7. The next parameter we need to give the test type is the process name. You can either copy the full workflow name and paste into this box or leave it and it will automatically get the workflow name.
  8. The next box is called milliseconds, you can either leave it at 0. Or you can enter in a value. This is useful as it may be your workflow is taking a while to perform an event. So you can enter in a delay to the tests to compensate for this.
  9.  Click on ‘Add’ again , we now what to check the status of the workflow, to make sure that once we started it. It is now active.
  10. So select ‘Get Workflow Status’ from the drop down list
  11. Choose ‘=’ from Sign
  12. Enter ‘Active’ in expected result
  13. We don’t need to enter in anything else
  14. Click on ‘Add’
  15. We now want to check to see if has generated some tasks
  16. Select ‘Get Task Count’
  17. Select ‘=’ in Sign
  18. Expected Result should be 1, as we know it’s only one slot and going to the originator.
  19. ProcessInstanceId, leave that how it is, as the testing framework will replace that with the actual process instance id as run time of the test
  20. Process name either enter in the full name or leave it and it will get automatically populated
  21. Next we can action the task
  22. Select ‘=’ for the sign
  23. In the expected result box enter ‘True’
  24. Where it’s says ‘Action’ we need to enter in the action we expect the test to use. So lets enter ‘Approve’
  25. Where it says ‘Serial Number’ we leave that and like the process instance id, it will be populated by the testing framework at run time.
  26. For Destination user, enter in the destination user that the task is supposed to be for.
  27. When this test type runs it will try and action the task based on the information we have given it (Task Action and Destination User) if the test is successful the actual result will be true.
  28. The last test we are going to do is to check that workflow has gone down the correct path and the correct activities were used. The test will compare the path we expect it to be. which was defined in the ‘Workflow Route’ section to the actual path taken.
  29. Select ‘Compare Activities’ from ‘Test Type’
  30. Select ‘=’ from Sign
  31. Enter in ‘True’ in expected result
  32. Leave ProcessInstanceId and Activities how it is
  33. In the Millisecond box enter ‘4000’ this will make the ‘Compare Activities’ test to wait 4000 milliseconds before starting the test. This will give the workflow time to perform it’s other activities before completing. If this test fails then increase the time. It will then pass.
  34. The test script should now look like thistest-builder4
  35. Now click on ‘Save’
  36. You will now see a read only version of the test
  37. With two buttons ‘Edit Test’ and ‘Run Test’

Running a Test

To run a test, either select a test from the test list and click on ‘Run Test’ or from the test details click on the button ‘Run Test’

Once the test has been completed, you will be displayed the results for each individual test and also the overall test.

resuts

Each individual test will show whether it has passed or failed. If it has passed the status will be true and in green and if had failed it will be false and in red.

For each test that we create we also get the overall test result.

A failed test would be when any of the individual tests has failed

fail

It will show a pie chart showing the % of pass and fail with big black x cross above it.

pass

For an overall Pass we get a tick and of course a 100%

These test results will remain until, you click on ‘Run Test’ again and this will overwrite the previous test results with the new ones.

Continue reading “Testing a workflow inside a Smartform”

K2 Management Portal: Using the K2 Scheduler

In the latest update of K2, there is now a management portal and one of the key new features of this is the ability to schedule workflows. Which up to now was only available if you had K2 for SharePoint , Appit or if you could decipher the K2 Scheduler API. Your only other option was to write a windows service that would call your scheduled workflow.

 

How to access the K2 Management portal

The K2 management portal can be accessed from here http://[k2servername]/Management

home

The management portal replaces the management console found in the K2 Work space, but doesn’t replace the work space completely.

The K2 Scheduler lives under ‘Workflow Server’ heading in the menu on the left. Expanding ‘Workflow Server’ you have a number of different options. The one we are after is ‘Schedules’ which is the fourth one down.

home1

Schedules

Clicking on Schedules shows a list of currently scheduled workflows and the ability to ‘Add’, ‘Edit’ and ‘Delete’  scheduled workflows from the scheduler

 

Creating a new scheduled workflow

To create a new scheduled workflow follow the following steps

  1. Click on ‘Schedules’
  2. Click on ‘New’
    scheduler
  3.  A form will pop up, for you to fill in
  4. Enter a name of what the scheduled workflow will be called
  5. Enter a description
  6. Enabled is checked by default
  7. Next enter the name of the workflow you want to schedule, either by entering it’s full name or by clicking on the search button to search for it
  8. Once you have found the desired workflow, it’s process data fields will load up and up have the option to populate them with values
  9. The folio can also use the date and time as default or can be edited with a value
    scheduler1
  10. Now if we scroll down the form, we can now tell the scheduler when it should run
  11. Under pattern, we can choose what the scheduling pattern should be

 

Pattern
Description
Once K2 will schedule the workflow to run just once on a certain day and time

once

Daily K2 will schedule the workflow to run every weekday or intervals of days. It also has a start date and time and can be configured to end after a number of occurrences or by a end date and time

daily

Weekly K2 will schedule the workflow to run at least once a week on a certain day. It also has a start date and time and can be configured to end after a number of occurrences or by a end date and time

scheduler3

Monthly K2 will schedule a workflow to run at least once a month, in monthly intervals. It has the ability to schedule a workflow to start on the last or first day of the month or first / last day of the week. It also has a start date and time and can be configured to end after a number of occurrences or by a end date and time

monthly

Yearly K2 will schedule the workflow to run at least once on a particular month. It has the ability to schedule a workflow to start on the last or first day of the month or first / last day of the week. It also has a start date and time and can be configured to end after a number of occurrences or by a end date and time

yearly

Interval With intervals, you can tell K2 to schedule a workflow to start by minutes, hours, days,months and years.It also has a start date and time and can be configured to end after a number of occurrences or by a end date and time

interval

12. Click on ‘Ok’, the scheduled workflow is now scheduled to start at it’s selected interval.

scheduler4

13. From here you can see when it last ran, whether it has been successful or failed and also the ability to enable and disable the scheduled workflow

scheduler5

With K2 releasing the Scheduler in the management portal, this means we can now build some interesting workflows that relies on scheduling of workflows to perform autonomous daily tasks. Such as sending out daily emails to a subscription list.

Testing Smartforms with the Smartform tester in K2 4.7

There are a number of Smartform testing tools currently on the market to allow developers to test their Smartforms. But in 4.7 there is something hidden away that is just dying to be played with. Like the developer’s best friend SmartObject Service Tester, there is now a tool that will allow you to automate testing of your Smartforms.

How to access it

To access this hidden gem of a tool, go to the K2 Designer and click on ‘show’ link at the bottom of the page and in the pop up click on System.

show-all

How you will notice in the context browser for the designer there is now some additional categories.

This slideshow requires JavaScript.

  1. Expand System
  2. Expand Forms
  3. Expand Smartform tester
  4. Click on ‘SmartForms Tester – Landing Page’
  5. Click on the ‘Runtime URL’ in the properties page
  6. The Smartform Tester Portal will now load up and we can start to build a test case

 

SmartForms Tester Portal

This is where we can manage the automating of the forms from creating new tests to running existing ones.

smartform-tester-form

K2 do give us some examples of how to use the tester portal in the ‘Examples’ folder or what they like to call ‘Suites’

So lets create a ‘Suite’

 

Creating a Test Suite

  1. Click on ‘Test Suites’
  2. A list of options will appear at the top
  3. Click on ‘New Suite’
  4. A pop up window will appear enter in the name of  what your suite is going to be called. I am going to call mine ‘Annual Leave’
  5. Click on ‘Accept’
  6. Click on ‘Refresh List’ button
  7. You will now see the suite you just created in the list

This slideshow requires JavaScript.

 

Creating  a new test

  1. Click on the suite you just created
  2. The list options above will change
  3. Click on ‘New Test’
  4. This will open a new window, from where we can build a new test case
  5. Click on smartform-tester-form25 in the Smartform section and choose the View or Smartform you want to test. In this case i am going to create a simple test to test a view
  6. When you select a view/form it will then appear in the in  top section of the testing  form
  7. Below the name of the select artifact
  8. Is the actions this where will build the test script that the portal will execute for us

 

This slideshow requires JavaScript.

Creating the automated test script

  1. In the actions section, you will see there part of a action already selected
  2. Because i have chosen a view to test. The first drop down list has ‘View’ selected. The 2nd drop down list has ‘Control’ selected.
  3. From the 3rd drop down list we can now choose a control on the view
  4. I am going to choose  the picker control ‘pkrUsername’.
  5. A 4th drop down list appears and should have ‘Change Value’ selected
  6. Text box is also available, where a value can be entered. I am going to enter in ‘sallport’smartform-tester-form8
  7. When we execute this action  the tester, will put ‘sallport’ into the picker control for you.
  8. Click on ‘Add’ to add another action smartform-tester-form26
  9. I am going to select the ‘calStartDate’ control and enter in a date and i am going to the same with for ‘calEndDate’
  10. I am going to add another action so it can ‘select’ a leave type. I am going to enter in ‘Paid leave’
  11. Last of all i am going to select the ‘Button’ control and tell it to click the button.
  12. Now that my simple test script of filling out the view and clicking on the button to submit the data. I am ready to get the tester to run it for me.
  13. But before I do that just click on the smartform-tester-form27 in the top left hand corner and select ‘Save’ from the context menu
  14. ‘Save Test’ pop window will appear, enter in a name for test and make sure the correct suite is selected and click on ‘Ok’

This slideshow requires JavaScript.

Running the test

From the test designer, we can either run the whole script or by clicking one of the actions we can test a particular line.

  1. I am going to run the whole script, so click on ‘Run All’ button. The designer will then carry out the actions line by line.
  2. A report will then be displayed telling you if it was successful or not.
  3. The actions line will also all be green and of course the data inserted into the view will be displayed and of course you can check the SmartObject that was used to save the data.
  4. You can also run the test from main testing portal , by selecting the test from the suite and then clicking on ‘Run Test’ option

This slideshow requires JavaScript.

 

So this is a basic how to use the Smartform tester, and there are other options that I have not covered such as delayed actions and modifying variables and assertions such total count of items in a list etc..

Demo forms can be downloaded from here

Conclusion

What is good about this tool it’s all built using Smartforms, so not only are we seeing some new controls that are not made available yet to in the Smartform designer, such as the context menu, the action list with is probably a more advanced list view. I hope these will made available soon.

The 2nd good thing about this is that the test data is available as SmartObjects which mean we can then use these to build rich testing reports that also take into account business data.

It’s great start to something that will hopefully be fleshed out with further updates in the future. So it would be possible to fill out the form, get a task and action it, all in one test case.

 

 

 

Bill’s Tasking Workflow Pattern

Sending tasks is easy to do in K2, but to make them more adaptable from outside the work is a bit more tricky. A colleague of mine named Bill Irvine, came up with pattern to standardize the way tasking is developed and used.  So taking his idea i have come up with own interpretation of his tasking pattern so this 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 store procedures.  When the framework is released it will be included as apart of that.

Tables

Tasking relies on the following tables

tasking-db

Task Library

Contains all the task types. It contains Id of the form that the task will navigate too and also whether task needs to send task 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 not using framework change, form id data type from unique identifier to Nvarchar(100).

Task Actions

Task Actions contains all the possible actions for that 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, 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 tables have basic CRUD methods to manage the data and there is also some additional store procedures to help with the tasking pattern

 

The tables and store procedures scripts can be accessed here 

SmartObject 

The tasking SmartObject is called K2C.CMN.SMO.Tasking, and it manages the creation of creating task types and its related task actions. It also manage the destination user’s task details

Properties

Name
Type
Description
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 instance of the process
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 Task value that relates to 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 task needs to send an email

SmartObject Methods

Method Name
Store Procedure
Description
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 a action for Task Type
Update Task Action sp_TaskActionUpdate Updates a 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 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

Data Field Name
Data Field Type
Description
In or out
TaskId String So we know what task type In
ParentProcessInstanceId Number Parent Process Instance Id In
 DestinationKey  string
Expire Boolean stops start going to expire, set to false Int
ActionResponse String action value returned Out

Simple tasking pattern

task-workflow-v2

Activities

  1. Setup – Links workflow to framework
  2. Send Email, sends email based on a template
  3. Task, creates task based on the task type Id
  4. Action 1 ,2 , 3,  records the action returned
  5. Expire , responds to the task escalating
  6. End, tidies everything up based on framework

 

Setup

The setup activity does all the framework registration

Send Email

This activity will record the task with the notification service. It will also call the Email service and pass in the template id from the task type reference

Task

It will add the Serial Number to the destination user record in tasks and the activity will also have a client event. It will get task url from the form library and the destination user will come from task reference. It will have 3 actions (1,2,3)

Action 1, 2, 3

Will record the action  against the task by getting the action name from the ‘Get Task Action’ method and it will set the data field ‘Action Response’ with the action value

End

Will do the framework clean up, so deleting  external data fields etc…

 

Notes

Remember to audit logs (for the actual task workflow and also for the parent workflow so we can record the action e

Line Rules

Line Rule
Description
LR Email Notification Required If method ‘Is Email Required’ returns true
LR No Email Notification Required If method ‘Is Email Required’ returns false
Start to Expire Data field ‘Expire’ = ‘True’

References

The tasking pattern workflow requires two references

  1. Destination
  2. Task Type

references

Destination Reference

Gets a list of destination users for a particular task type

  1. In the  Data (3rd) tab of the context browser in K2 Studio / Visual Studio
  2. Right click on ‘References’ and click ‘Add’
  3. Give the reference a name ‘Destination’
  4. Select the method ‘K2C.CMN.SMO.Tasking.Get Destination Users’
  5. For the input property use the ‘DestinationId
  6. Click ‘Next and then ‘Finish’

reference

 

Task Type Reference

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

  1. In the  Data (3rd) tab of the context browser in K2 Studio / Visual Studio
  2. Right click on ‘References’ and click ‘Add’
  3. Give the reference a name ‘Task Type’
  4. Select the method ‘K2C.CMN.SMO.Tasking.Get Task Type’
  5. For the input property use the ‘Task Id’ data field
  6. Click ‘Next and then ‘Finish’

tasking

Building the tasking workflow

  1. Create an activity on the canvas and name it ‘Setup’
  2. If you are using framework then, add in the related framework events, else just put a  placeholder event in their for now.
  3.  Create another activities called ‘Send Email’ and ‘Task’
  4. Create a line between ‘Setup’ and ‘Send email’ and another line between ‘Setup’ and ‘Task’
  5. Right click on the line between ‘Setup’ and ‘Send Email’
  6. Select properties
  7. In General properties give it the label of ‘LR Email Notification required’
  8. Click on the ‘Line rule’ section greenarrow
  9. Click on ‘Add’
  10. In the rule editor now. In the ‘First Variable’ open the context browser expand the 3rd tab
  11. Expand references, expand ‘Task Type’
  12. Select ‘Email Notification’, either drag it into ‘first variable’ or click on ‘Add’
  13. Comparison operator should be ‘=’
  14. Second variable will be ‘True’
  15. Click ‘Ok’ and then ‘Finish’
  16. We have now added a rule that will check to see if an email notification is required.
  17. We now need to do the same for the other line we created, except call the line ‘LR No email notification required’
  18.  Second variable for this rule should be ‘False’

This slideshow requires JavaScript.

The email event

  1. Add an email event in the ‘Send Email’ activity
  2. Give it a name like ‘send email’
  3. Select ‘originator’ and unchecked ‘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’

  1. Drag the ‘SmartObject Event’ into the task event
  2. Give the event a name ‘Add sn’
  3. Select the SmartObject method ‘Add task sn’ from the SmartObject ‘K2C.CMN.SMO.Tasking’
  4. Click ‘Next’
  5. Now in ‘Input Mapping’ for the ‘PSN’ click on ‘Assign’
  6. Expand Process Instance from and select ‘id’
  7. Type in ‘_’
  8. Expand ‘Activity Destination Instance’ and select ‘Id’
  9. Click on ‘pTaskId’ and ‘Assign’
  10. Expand the ‘Destination’ reference and  use Task Id
  11. This will now save the task serial number against the destination user.

This slideshow requires JavaScript.

Adding user task

  1. Drag a ‘Client Event’ in to the task activity
  2. Name it ‘User Task’
  3. Check ‘Task Item URL’
  4. If using framework follow this route
    1. dd
  5. If not using framework follow this route
    1. Click on eclipse
    2. Go to the 3rd tab expand ‘References’
    3. Expand ‘Task Type’
    4. Drag ‘FormId’ or select ‘Form Id’ and click on ‘Add
  6. Click on ‘Next’ and ‘Next’ again
  7. In Actions click on ‘Add’
  8. In the ‘Add Action’ window, enter 1 in name and click on ‘Ok’
  9. Repeat this adding 2 and 3 as a action
  10. Click ‘Next’ and ‘Next’ again
  11. In Destination users click on ‘Add’
  12. Click on eclipse,
  13. In the context browser
  14. Click on 3rd tab
  15. Expand References
  16. Expand ‘Destination’
  17. Click on ‘Username’ and click on ‘Add’
  18. Click ‘Next’ and then ‘Finish’
  19. We have now created a generic task

This slideshow requires JavaScript.

Recording the Action

  1. Create a ‘Default Activity’ and name it ‘1’
  2. Drag a SmartObject Event into the event
  3. Name it ‘Task Action’
  4. For ‘SmartObject  Method’ use ‘Update Task Action’ from ‘K2C.CMN.SMO.Tasking’
  5. Click ‘Next’
  6. In Input Mapping, click on ‘pTaskAction’ and then on ‘Assign’
  7. Click on eclipse, it opens the context browser
  8. Go the 1st tab, expand SmartObject Servers
  9. Expand the SmartObject ‘K2C.CMN.SMO.Tasking’
  10. Expand the method ‘Get Task Action’
  11. Select the ‘TaskAction’ property
  12. Click on ‘OK’
  13. This method requires some additional properties.
  14. ‘pTaskValue’ property needs to be 1
  15. ‘pTaskTypeId’ property needs to be the datafield called ‘TaskId’
  16. Click ‘Next’ and ‘Next’ again
  17. Select ‘Task Action’ and click ‘Next’
  18. Make sure ‘Return a single item’ is checked
  19. Click on ‘Finish’
  20. Click on ‘OK’
  21. What we have just done is get the text value of the action for the task type and of the action value of 1.
  22. pTaskId just needs to be Task Id from the destination reference
  23. Click on ‘Finish’
  24. Copy this Activity and rename it to ‘2’
  25. and change ‘PtaskValue’ to ‘2’
  26. Repeat steps 24 and 25 and change the ‘1’ to a ‘3’
  27. These activities will now record the action of the task against the destination user
  28. We also need to record the action in the data field ‘Action Response. So do to steps 9 to 23 in a data event for each of the 3 activities. This will allow the parent workflow to easily retrieve the action.
  29. Connect the out come lines to the activities we just created
  30. Create an activity called
  31. Last of all create a activity called ‘End’ with a placeholder called ‘End’ and join the 3 activities to the end activity.

 

Your tasking workflow should look something like this

taskingwkf.PNG

 

Download workflow, SmartObject from here

 

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

It also allows there to be generic task forms that just point to the tasking workflow and they just need the task type id to load up the correct actions for the task type.

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

How to use the tasking workflow

To use the tasking workflow follow these simple steps

  1. Add the tasking workflow to the solution
  2. Create a new workflow
  3. Use the SmartObject event with ‘Create destination key’ method and bind the result to a data data field called ‘Destination key’ of type string
  4. Now we need to use the ‘Create Task’ method to add the destination user, task type id and destination key
  5. Add the IPC Event and point it to the tasking workflow
  6. Choose synchronous
  7. Map the following data fields ‘Task Id, Destination Key’
  8. For the return value map ‘Action response’ data field of the tasking workflow to the parent ‘Action response’ data field
  9. Then use a line rule to direct the workflow based on the ‘Action response’

 

 

 

 

 

 

How to: For Each Event

Over the last couple of weeks people have asked me how to use the for each loop event in K2. So I thought i would put this quick demo together.

The demo, that i am going to do is going to loop through a list of subscribers and update their status from ‘Not Sent’ to ‘Sent’.

Getting Started

I have created a simple Smart box  SmartObject for demo purposes that has the following properties

  1. 1. Name of type string
  2. Email of type string
  3. Id of type Auto number and Key
  4. Status of type string

smo

I have inserted 3 rows of data with the status of ‘Not Sent’. We will use this SmartObject to use the ‘For Each’ event to loop through the data.

Creating the workflow

for-each-simple

Adding the ‘For Each’ event

  1. Open K2 Studio or K2 for Visual Studio ( I am going to use K2 Studio)
  2.  Create a new workflow solution called ‘For Each Demo’
  3. Rename the workflow from process 1 to ‘ForEachWorkflow’
  4. Once the workflow canvas has loaded up
  5. Drag the ‘for each’ event on to the canvas
  6. In the event wizard, give the event the name ‘Loop demo’
  7. Click on Source ico icon, so the drop down list disappears  and open the context browser
  8. In the 1st tab (Environment) , expand ‘SmartObject Services’
  9. Go to the SmartObject you want to use and expand the list method you want to use
  10. Drag the property that you want to use, normally the primary key into the event box
  11. The SmartObject wizard loads up, click ‘Next’ and ‘Next’ again
  12. In ‘Select a return property’ make sure the ‘id’ value is selected.
  13. Click ‘Next’, make sure ‘Return all Results that match filter’ is selected.
  14. Click ‘Finish’
  15. For the reference name, enter in the name ‘Dummy Data Reference’
  16. For the index name, enter in the name ‘Dummy Index’
  17. Click on ‘Finish’

This slideshow requires JavaScript.

Interacting with the for each item

  1. Back in the canvas, you will notice there are now two line rules coming out of the activity
  2. Drag the placeholder event on the canvas and name both the event and activity with the name ‘End’
  3. Drag the ‘line’ named ‘No more items’ to the end activity
  4. Create a new activity on the canvas and name it ‘Status Change’
  5. Drag the SmartObject Event into the newly created activity
  6. In the event wizard for the SmartObject event, name it ‘Update Status’
  7. Select the update method for the SmartObject that you used with the ‘for each’ event
  8. Click ‘Next’
  9. For the ‘ID’ property, click assign
  10. In the context browser go to the 3rd tab (Data) and expand ‘Item references’ and then expand ‘ Loop demo reference’
  11. Click on ‘Id’ property and click on ‘Add’
  12. Click on ‘Status’ and click ‘Assign’ and enter in the text ‘Sent’
  13. Click on ‘Next’ and ‘Next’ again and then ‘Finish’
  14. Drag the remaining line named ‘Next item’ to this activity and then create a new line from this activity back to the ‘for each’ activity.

This slideshow requires JavaScript.

That is a simple example of using the ‘For each’ event. Anything that follows the ‘Next item’ line is in side the loop and has the context of the current row the loop is on. You can have multiple activities in this part and the workflow  will move on to the next item once it has completed everything in that section. You must always have a line going back to the activity with the ‘for each’ event in.

Other things to remember

  1. Choose 1 property to return in the for each event. Always best to use a primary or foreign key
  2. Remember to select ‘Return all Results that match filter’ as it will return the complete list back and it doesn’t matter if there is no filter applied.

 

Next step using references with the ‘For Each’ Event

So we have created a simple workflow with a simple for each loop. But what if we wanted to get more data from the for each event and then possibly use it in a line rule.

In the next example, the workflow is going to check the current status of the item it is looking at. If it’s status is already ‘Sent’ it will ignore it and move on to the next item in the list and if the status is ‘Not Sent’ it will then update the status as normal

I am going to use the same workflow as before

Creating the reference

  1. In the context browser, go to the 3rd tab (data)
  2. Right click on ‘References’ and click on ‘Add’
  3. In the ‘SmartObject Method Reference’ click on ‘Next’
  4. In name, give the reference a name , for my example i have used ‘Dummy data’
  5. In SmartObject Method, select a read type method that uses value you have selected in the for each as the input parameter. I am pointing my reference to the read method.
  6. Click ‘Next’
  7. In input mapping, click on Assign and select the id from the item reference for the for each loop.
  8. Click ‘Next’ and ‘Finish’
  9. We have now created a reference to get the related details based on the current row of the ‘for each’ event.

This slideshow requires JavaScript.

 

Editing the ‘for each’ line rule for ‘Next Item’

Now that we have the reference setup, we can edit the line rule for ‘Next Item’

  1. Right click on ‘Next item’
  2. Click on ‘Properties’
  3. ‘Line General Properties’ will load up. Click on  greenarrowthe green arrow for line rules
  4. Click on ‘Add’
  5.  In rule editor, select ‘And’ from ‘Boolean Operator’
  6. In ‘First Variable’ click on eclipse
  7. In the context browser,  go the references in the 3rd tab
  8. Expand References, expand your reference you created in the last section
  9. Drag ‘Status’ into the ‘First Variable’
  10. The ‘SmartObject Wizard ‘ pops up, click on ‘Status’
  11. Click ‘Finish’
  12. Select ‘<>’ from ‘Comparison Operator’
  13. In the ‘Second Variable’ enter ‘Sent’
  14. Click on ‘Ok’
  15. Click on ‘Finish’
  16. We now need to create another rule that is similar to the ‘Next Item’ rule but where the ‘Status’ is equal to ‘Sent’

This slideshow requires JavaScript.

Creating a new line rule for ‘Next Item’ and where status is equal to ‘Sent’

  1. Right click and drag a line from the ‘For Each’ activity to ‘Update Status’
  2. Then drag the line from ‘Update Status’ to ‘For Each’. (We have to do this as line can’t go back on it self without being connected to another activity first)
  3. Right click on the line and click on ‘properties’
  4. In ‘Label’ enter ‘New Item and status is sent’
  5. Click on greenarrow and then click on ‘Add’
  6. In the Rule editor in ‘First Variable’
  7. Click on eclipse and expand the process data fields
  8. Click on ‘Dummy index result’ and click ‘Ok’
  9. Select ‘=’ for the operator
  10. In Second variable enter in ‘True’
  11. Click ‘Ok’
  12. Click on ‘Add’
  13.  In rule editor, select ‘And’ from ‘Boolean Operator’
  14. In ‘First Variable’ click on eclipse
  15. In the context browser,  go the references in the 3rd tab
  16. Expand References, expand your reference you created in the last section
  17. Drag ‘Status’ into the ‘First Variable’
  18. The ‘SmartObject Wizard ‘ pops up, click on ‘Status’
  19. Click ‘Finish’
  20. Select ‘<>’ from ‘Comparison Operator’
  21. In the ‘Second Variable’ enter ‘Sent’
  22. Click on ‘Ok’
  23. Click on ‘Finish’

 

This slideshow requires JavaScript.

 

Download example here