Skip to main content
sharmilan9331
Participating Frequently
Oct 21, 2024 10:09 AM UTC
Solved

Stop the loop if a value is found

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. 

    mikes0011
    mikes0011Answer
    Brainy
    Oct 21, 2024 1:59 PM UTC

    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 */
    )

    The Expression Guru

    6 replies

    sharmilan9331
    Participating Frequently
    Oct 21, 2024 10:16 AM UTC

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

    stefanhelzle0001
    Brainy
    Oct 21, 2024 10:30 AM UTC

    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
    Employee
    Oct 21, 2024 1:27 PM UTC

    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
    Oct 21, 2024 1:59 PM UTC

    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 */
    )

    The Expression Guru
    sharmilan9331
    Participating Frequently
    Oct 22, 2024 7:02 AM UTC

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

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

    mikes0011
    Brainy
    Oct 22, 2024 2:03 PM UTC

    thanks for confirming!

    The Expression Guru