One of the great new features of Metastorm BPM is looping.
Why do we want to do this? There is a lack of list processing functions that we used to have in previous versions. Some of our systems have code that relies upon those list processing functions. Without them the functionality will have to be re-engineered. What can we do about it?
We have added our list processing functions to our library, but is there a simpler way that is more generally useful? Yes there is.
Metastorm will happily process lists, and has an internal type called 'List'. All we really need is a way to convert tab and other separated strings into Metastorm lists. These are converted to Text and Memo fields when assigned, so we can store them as this and recreate our List object when we need to use it later. What we are actually missing here is a 'List' type variable to store the Lists we create. That would make this redundant. Until we do, we have to implement our own.
Creating our List
Create a List in code
Firstly we pass in the variable. Next, we are assuming a tab delimited string here, we split it into an array. That's it. That's all we need to do to create a List object!
Here we have promoted the function to the Expression Builder so that we can reuse it easily.
It is worth noting that SelectSql() returns a list, but not quite like this. We may deal with that in a future instalment, as it is not quite as simple as the list from a listbox.
Using our List
Using our List
Now we want to get to the point of this and use the list. Here we select a set of Folders from a listbox, and then edit each in turn, all in the same session. We keep a count of the element used. Each time around we set the Folder Id of the next one and process it until we have done them all.
We have two simple processes.
The first creates a Folder
The first creates a Folder, and lets you select any number of child folders from the second process.
the second process
The second is merely a create and delete action for the child folders we will be going to select.
We have three forms.
One for the first action
One for the first action on the main process.
we select the folders
Here we select the folders from our fabricated 'child' Process. This variable will contain a tab delimited list of folder Ids.
For the Child Folder we have a plain form with a subject and three variables.
similar form for the main process
We have a very similar form for the main process, in fact with a little fancy footwork we could use the same form, but we are going to keep this simple.
Here you can see the Business Objects we have applied to the form. Most importantly for our purposes, we have the Business Object for the D_ListChild process, and we are passing in the CurrentChildId variable as the Folder id.
What we are going to do is process the list of Folder Ids we have stored, and assign each one in turn to the CurrentChildId variable in order to allow us to edit the data for that Folder.
the only code
This is the only place in either Process where we do anything in code.
calling the function
Here you can see that we are calling the function we created in the first section, and passing in the 'SelectedChildren' variables that we fill from the listbox in the first Action.
There are two things to note here:
Firstly we are able to use the square bracket "[n]" operator to get a specific item from the returned array. That is not terribly clear, but to those used to C# (or C++/C, I am going to call these Cx from now on) it is common enough shorthand. Get used to this type of shortcut in Metastorm BPM 9, as we are now in C# land, and you will have to become a 'semi-colonist' like the rest of us.
Secondly we have used the "++" operator to increment the CurrentChildNumber counter variable at the same time as use it. "--" will decrement a number, by the way, and either operator before the variable it will apply the increment/decrement before using the variable (actually a surprising number of Cx programmers are unaware of that one). I've never seen both used at the same time, but in theory it is valid!
What is nice is that these operators are implemented for Metastorm number variables.
In 'normal' Metastorm BPM style, in theory, we would have assigned the return to a variable, used the square brackets operator to get the value we wanted, and then incremented the counter by adding 1. The problem is immediately seen that once we assign the return value to a variable it will become a tab delimited string straight away, and the square brackets will not work. Hence our little shortcuts here. The alternative is to do it in C#, and we were trying to keep it simple.
decide the next chained Action
The only other thing we do with our list is decide what the next chained Action will be.
Here you can see the test is if the CurrentChildNumber counter is less than the number of items in our list we perform the Edit Child Action, otherwise we perform the 'Finish' Action. Bear in mind we are dealing with is a 'zero-based' arrays, so if it has 5 elements, element '0' is the first, and '4' is the last.
The only point to note is the ".Count" after the function call. Again, this is a shortcut. The function returns a List object, and one of the properties is Count, which is the number of items in the list.
So that's it. With one two-line function, a on-line use of that function in the 'when action starts' and an 'if-else' selection for the next chained Action, and we have implemented not only similar functionality to that we had in previous versions, it is much easier and neater.
Sure, you have to delve into C# a little, but you will have to do this anyway, so this is a good way to start.
We have presented on or two shortcuts here. There are several more in the language. To get a feel for these, try pressing "." after an object variable, or even a function call that returns an object, such as SelectSql(). In most places, especially in the C# editor of course, you will be presented with an Intellisense list of methods and properties (functions and variables). Get used to the different icons and what they represent as well, and it will stand you in good stead. In some ways I feel a short course on C# may be in order for Metastorm BPM 9 developers not already familiar with the language.