Loop & Repeat Step in WebMethods

 Loop & Repeat :

In webMethods, you can implement loops within integration processes or services to repeat a set of actions or steps based on certain conditions. Loops allow you to automate repetitive tasks and iterate over collections of data. There are different ways to implement loops in webMethods, depending on the specific requirements of your integration scenario. Here are two common approaches:

  • Iterate over a List or Array:
    • If you have a list or array of elements that you want to process individually, you can use a Loop step. The Loop step iterates over each element in the list and executes the specified set of actions for each iteration.
    • Within the Loop step, you can access the current element using a temporary variable and perform the necessary operations on it.
    • You can define the conditions for entering and exiting the loop, such as the number of iterations, specific values, or conditions on the elements being processed.
  • Iterate using a Repeat step:
    • The Repeat step allows you to repeat a set of actions a specified number of times. It is useful when you need to perform a certain action a predetermined number of times, regardless of data elements.
    • You can configure the Repeat step with the desired number of iterations or use a variable to dynamically control the number of repetitions.
    • Inside the Repeat step, you can define the actions to be executed during each iteration
  • Both approaches can be combined with conditional statements, such as if-else conditions, to control the flow within the loop and make decisions based on specific criteria. Additionally, you can use variables to store and manipulate data during each iteration.
  • By incorporating loops into your webMethods integration processes, you can automate repetitive tasks, efficiently process collections of data, and streamline your overall integration logic.Now it’s time to have some exercises, hope you will enjoy it😊

    Exercise -1 : Write a flow service that takes a list of inputs stored in a stringList variable and returns the same data as output, populating it into another stringList variable using loop and repeat.
    Ans :
    Let’s do it using both Loop and Repeat😊!
    Loop :
    • Create a flow service with the name transformationUsingLoop.

       

    • Create input stringList variable with name inputList and output stringList variable with name outputList.

       

    • Now add one loop step and provide a descriptive comment to improve code readability and understanding.

       

    • In the properties panel, we have two variables: inputArray and outputArray. To read the data from inputList, which requires iteration, we should assign the inputList variable to the inputArray section. Similarly, to copy the data from inputList to outputList, we should assign outputList to the outputArray section. This allows us to properly handle the data flow between the two variables.

       

    • Now we can add one map step inside the loop, When adding a map step inside the loop, you will notice that the symbols for the inputList and outputList variables have changed from stringList to string. This occurs because we are now operating within the context of the loop, where individual items are processed one at a time. As a result, the variables are represented as singular strings within the map step.

       

    • To perform the mapping, we aim to directly copy the data from the inputList to the outputList.

       

    • To streamline the pipeline, we can include a map step outside the loop and drop the inputList variable since it is no longer needed in the subsequent stages of the process.
    • Please run the flow service and provide the inputs as shown in the screenshot below to execute the service successfully.

       

    • Boom here is the result😊.
  • Repeat :
    • Create a flow service with the name transformationUsingRepeat.
    • Create input stringList variable with name inputList and output stringList variable with name outputList.

       

    • Now add one repeat step and provide a descriptive comment to improve code readability and understanding.

       

    • In the provided screenshot, you’ll notice an exit property that needs to be set to “success” to ensure that the flow repeats when the child steps are successful.
    • We have an additional property called “count” which determines the number of times the task should be repeated.
    • In the count property, specify the length of the inputList so that the task will be repeated for that exact number of times.
    • Before the repeat step, invoke pub.list:sizeOfList. Map the inputList to the fromList parameter in the pub.list:sizeOfList service, and subsequently drop the fromList variable as it is no longer needed.
    • Copy the size variable and paste it within %% symbols as the value for the count property of the repeat step. This ensures that the task is repeated the exact number of times stored in the size variable.

       

    • Since there is no loop available, we need to access each index of the inputList individually to retrieve its corresponding value in order to perform the task.
    • To address the issue of accessing the index number, we can initialize an index variable with an initial value of 0. In the last step of the repeat process, we can increment the index variable to obtain the exact value from the list based on the current iteration.
    • Insert a map step after the pub.list:sizeOfList step and before the repeat step. Within this map step, create a variable named index and set its initial value to 0. This will initialize the index variable for further use within the repeat process.
    • To copy the data from the inputList to the outputList, we can leverage the pub.list:appendToStringList service. This service allows us to append the values from the inputList to the outputList iteratively, taking advantage of the index value we obtained earlier.

       

    • Map the inputList to the fromItem parameter of the pub.list:appendToStringList service. Since inputList is of list type and fromItem is of string type, it is necessary to explicitly provide the index to specify which value to extract. This ensures that the correct value from inputList is appended to the outputList.
    • Map the outputList directly to the toList parameter in the pub.list:appendToStringList service, and subsequently drop the fromItem and toList variables as they are no longer required in the pipeline. This allows for a simplified and streamlined data transfer from inputList to outputList.

       

       

       

    • To specify the index from which the fromItem parameter will retrieve the value, right-click on the line connecting inputList and fromItem, and select the option “Set Index.” This will enable you to explicitly define the index for fetching the desired value from the inputList.
    • In the row index of inputList, specify the index value by enclosing it within %% symbols. This allows for dynamic retrieval of the desired value based on the provided index during the mapping process.
    • To increase the value of the index variable, add a map step after the pub.list:appendToStringList step. In this map step, invoke pub.math:addInts as a transformer, mapping the index variable to num1 and hardcoding num2 as 1. Then, map the resulting value back to the index variable to increment it. This ensures that the index value is increased after each iteration.

       

    • To eliminate any unnecessary variables, include a map step after the repeat step and drop all variables except for outputList. This ensures that only the required outputList variable is retained, removing any extraneous data from the pipeline.

       

    • Let’s proceed to test the service by running it as a flow service. In the input list, please provide the values 1, 2, 3, 4, and 5, then click “OK” to initiate the execution. This will allow us to observe the outcome of the service with the given input.

       

  • Congratulations! We have successfully achieved the desired output. I hope you found the process enjoyable and satisfying.To obtain the source code, simply click on the download button below to initiate the package download. This will allow you to access and save the code files for further reference😊.

    Download Package

     

 

1 thought on “Loop & Repeat Step in WebMethods”

  1. dolor rem voluptate qui et suscipit quo est consequatur id exercitationem repellat vero voluptatibus molestiae dolorum quibusdam et at sed. perspiciatis corrupti explicabo cum placeat laudantium dolor

    Reply

Leave a Comment