Skip to main content
sharmilan9331
October 21, 2024
Solved

Stop the loop if a value is found

  • October 21, 2024
  • 6 replies
  • 1 view

For a list of integers, I need to check the desired value by calling a service. I would like to stop calling the service, only I get the desired output. Any suggestions, please. 

    Best answer by mikes0011

    This could potentially be one of the few times that it's helpful to call an expression rule recursively.

    Pattern: rule!RULE_myFunction(list of integers)

    if(
      (list index 1 meets condition),
      [Return Value],
      rule!RULE_myFunction(ldrop(list of integers, 1))  /* pass in the same array, but with the first element removed */
    )

    6 replies

    sharmilan9331
    October 21, 2024

    This needs to be calculated in an expression rule on load. 

    stefanhelzle0001
    Brainy
    October 21, 2024

    In Appian there is only a foreach(). And this cannot be interrupted.

    But using the reduce() function, while still iterating on all list items, you can decide whether to call your service based on previous results.

    https://appian.rocks/2022/08/29/complex-algorithms-in-appian/

    somasundaram.d
    October 21, 2024

    There is no break concept in Appian.

    But using reduce, call the service for list of integers. For every iteration append the desired output to a list and check if the list contains your desired output. If the list contains the output just return null instead of calling the service.

    mikes0011
    mikes0011Answer
    Brainy
    October 21, 2024

    This could potentially be one of the few times that it's helpful to call an expression rule recursively.

    Pattern: rule!RULE_myFunction(list of integers)

    if(
      (list index 1 meets condition),
      [Return Value],
      rule!RULE_myFunction(ldrop(list of integers, 1))  /* pass in the same array, but with the first element removed */
    )

    sharmilan9331
    October 22, 2024

    Thanks [mention:b45a4f6a20b14a008e4e0ec732a8bf98:e9ed411860ed4f2ba0265705b8793d05] for your suggestion. It worked.

    [mention:a11f77a729fb4db2af76b09948583328:e9ed411860ed4f2ba0265705b8793d05] [mention:57fc948ec4c44d7fbda6e5509555d197:e9ed411860ed4f2ba0265705b8793d05]  thanks for your time. 

    mikes0011
    Brainy
    October 22, 2024

    thanks for confirming!