Implement If-Then-Else Logic in WebMethods

Conditional Logic is the most important building block of any software development tool. WebMethods is not a programming language, but since we use it to build integration interface, which is also software, it means we are also programming with it. Writing a simple “If-Then-Else” condition in WebMethod is way too verbose to me though. The official tutorial on SoftwareAG teaches us to implement an if-then-else logic using the BranchSequence, and Map nodes as depicted in the sample below:


As we can see, one simple If-Then-Else condition will require at least 5 lines to achieve. A more complex Case condition with multiple branches can take up half of a screen’s real estate. This makes the code harder to read.

My preferred alternative approach is to use the “Copy Condition” when mapping variables as shown below:


If the condition specified in this “Copy Condition” is True, the value will be mapped from the left-side variable to the right-side variable. In this case, value of the $valuelist/Approved variable will be mapped to the $output variable if condition $var1 = ‘A’ is True.

To achieve the default (Else) branch, we use the NOT (!) operator such as: 

! ($var1 = ‘A’ OR $var1 = ‘W’ OR $var1 = ‘C’)

By doing this, no matter how many conditions we have, we can fit everything in one line.


 

 



No comments:

Post a Comment