String Concatenation in WebMethods

Manipulating string is probably the most frequent operation we need to do when transforming data. Thus, I’d like to talk a bit about string concatenation in WebMethods. The most basic way to add two strings is to use the pub.string.concat service in the WmPublic package as shown below:

Image 01. pub.string.concat service


However, this approach is way too limited. For example, to combine a Unique ID string which consists of a Prefix, an auto ID number, a Suffix with separator in between such as: WO-10012-CM, we’ll need to use at minimum 4 lines of code, and a number of temporary variables. That's crazy.

An alternative approach is to use variables substitution when assigning values to a variable as shown below. In this case, we can build a new string from an unlimited number of variables in just one line of code.

Image 02. using variables substitution


This approach doesn’t work if one of the input variables can have a Null value. In that case, it will give us an unwanted result as shown below:

Image 03. Bad result when input is Null

With Tundra library, we have a much more flexible tundra.string.concatenate service. It allows us to add unlimited number of strings in one line, and at the same time, have some other capabilities such as adding separator.

Image 04. Use tundra.string.concatenate

In the example above, I can achieve the same result in one line of code, and the tundra service is smart enough not to add the second separator when the $suffix variable is Null.

However, beware of the annoying bug with WebMethods that it doesn’t have ability to set the order of input variables. Thus, when you make an update to one input, for example, in the example below, I changed the input variable str2 to map it to a different variable, it will move to the bottom of the list, and thus, messed up the final output.

Image 05. Last updated variable is moved to the bottom

A work around is to update the other variables manually to correct the order. But from my experience, anything having more than 3 inputs will be a maintenance nightmare.

Image 06. A "quick" update messed up the order of concatenation

The more robust approach is to write our own concatenate service as part of a common service package. It can take a bit of time, but if we have a big project, it’s worth the effort in building some common service library to be reused across the entire environment. Ten minutes spending on building this service can save the whole team countless number of hours in development and maintenance.

 

Edit: Lachlan Dowding, the author of the Tundra library told me that we can use String List to work around the WebMethods bug re-ordering input strings when using the Tundra's concatenate. Thank you, Lachlan, for your very useful contribution to the community.



No comments:

Post a Comment