K2 Workflow: Custom functions
Recently I ran into a scenario where I had to repeat the same expression in various activities and it would have been really time-consuming had I not run into creating custom functions. To give an example, suppose I had to extract the first name from the Participant Name which is being displayed as “FirstName, LastName”. Then I would have to write an expression similar to:
First item(Split(Replace(input, <spaces>, <empty string>), ‘,’,)
Basically I first replace any spaces with empty string and then split it comma delimited and then extract the first item. Imagine doing this in each activity over and over. Luckily, you have a way to save your own custom function and the way to do it is very simple. When you click on the option to create an expression, click on the little icon as shown in the below link:
Check the box to save this custom function. What this does is that it will save this function under the “Saved Functions” section.
Now when you have to reference this function in any other activity, all you have to do is to drag and drop your custom function and you don’t need to repeat the function again and again! Isn’t that cool!
But wait a min, how do I get to specify what the input function values should be. It is such a pain to again drill down to the very nested function to specify the input values. This is where it can be tricky. For my requirement, I had to replace the Participant Name and since this is found on every activity, i didn’t need to substitute the function input values with anything, just use it with what it was already taking.
Another option is to pre-define the input variables, for example, suppose you have to do:
Sum(Square(x), Cube(x)) in multiple places, then define a data field x and set the value of x to what you want it to be before you invoke your custom function.
Making changes to your custom function
OK, so you were able to create your custom function. Now what if I have to change it? Pretty simple.. double click on any place where you referenced it, make the necessary changes and then as shown in the link, check the “Save function configuration”. If you want to use the same name, it will ask if you want to replace it. Click on Yes. This change will now apply across all places where this function is referenced.
Deploying changes to another environment
When you deploy your changes to another environment, you will notice that your function does not show up in Saved Function section. However, this does not mean that your K2 process will fail. What happened is that the logic is referenced inline for each activity. So if you want your custom function to show up as a Saved function in this new environment, open up any activity where you used the custom function, double-click on the function and click on the “Save function configuration”. The custom function will now show up in the Saved Functions section.
Hope you find this as helpful as I did, sure saved a lot of time generating the same expression over and over…..