K2Five: Creating an asynchronous event

With the new release of K2 five and it’s workflow designer, we do not have the capability to write custom code in our workflow anymore. For some, this is a shocker and unbelievable. Having no code in your workflow has been a best practice for some organizations for years and as a low-code platform, it’s a nobrainer. However, there are requirements that always required code in the past, one of those Asynchronous server events.

What is an asynchrous server event?

Server Events in K2 all the event wizards that do something in the workflow but do not have any interaction with end users. These events (by default) run in a synchronous mode. This means that they execute and then continue with the workflow (to the next step, or next event). Asynchronous server events will simply not continue with the next step, until an outside source has told the K2 server to do so. This outside component would use the K2 client API.

What should it be used for?

Of course, one doesn’t want a server event to normally wait for some outside trigger. So what is this used for? There are scenario’s where you might want to off-load some work from the K2 server to another system or where you have to wait for another system to complete some task. A common example is integration with a physical mail system where K2 has to wait until a scanning solution has received back a response my snail. A lot of designers will resort to (what we call) a “polling workflow”. It is simply a workflow that runs in a loop and checks if the external system has completed it’s work based on some status field. This is a bad design and should be avoided. Of course, if you can’t do a loop and poll the external system, then how should the k2 workflow know that the external system has completed it’s work? That’s exactly where asynchronous server events come in. The idea is to tell the external system that something must be done, and that the workflow waits until the external system tells K2 that the workflow can continue.

How did we do it in the past?

Pre K2 five, you simply used a Code Server Event and used the line “K2.Synchronous = true;” to make the workflow wait. You would also have some code to call the external system and provide that external system with a SerialNumber. In K2 five, there’s no option for code, so how is this done now?

SmartObject events!

The people at K2 have considered the scenario and the pattern is always that an external system needs to be informed first before K2 start to wait. Integration with other system should be done via a SmartObject. So, the SmartObject wizard now has a simple checkbox to indicate that after the SmartObject was executed, the workflow should wait for the external event.

Workflow REST API

K2 five also hosts a new workflow REST API and this REST API has the option to complete these server events. So, all the external system needs to do is call this REST service. Of course, the old fashioned .NET client API would still work too.

Enabling these new REST API’s is super easy and can be done in the K2 management site:

How is everything connected?

Just like the tasks that K2 provides via the worklists, the Asynchronous Server Event also has a Serial Number (SN). This means that in your call of the SmartObject, you can use the SN and pass this unique number to the external system. This external system needs to store it. Configuration could look something like this:
In the above example, I just created a simple SmartObject with a few properties like ID, SN, Folio and Status.
Testing the REST API
The REST API also has a swagger definition file which allows one to test via the browser. So, testing this becomes super easy:

Conclusion

The new K2 workflow designer is a new approach to building workflows. The design team has analyzed how some features were used and found easier and quicker ways to implement the same functionality in the new designer. The above is a nice example of how concepts that required code and more customizations in the old workflow designers, now needs no code anymore and has become a lot easier. It provides a great way to integrate K2 with other systems.