This site is in read only mode. Please continue to browse, but replying, likes, and other actions are disabled for now.

⚠️ We've moved!

Hi there!

To reduce project dependency on 3rd party paid services the StackStorm TSC has decided to move the Q/A from this forum to Github Discussions. This will make user experience better integrated with the native Github flow, as well as the questions closer to the community where they can provide answers.

Use 🔗 Github Discussions to ask your questions.

Is it possible to use multiple when statements in orquesta/how is the order they are worked on?

Hi,

I wanted to build a orquesta workflow that has two (multiple) “when” statements that can all be true and should be executed. For now I was not successful. Is there any order how the “when” statements are worked on? I tried like in the code below and got the error

YaqlEvaluationException: Unable to evaluate expression ‘<% ctx(regex_result[0]) = “12” %>’. NoMatchingFunctionException: No function “#indexer” matches supplied arguments

For me it looks like the “regex_rseult” variable I created before is not available during the execution of the second “when” statement.
If I exchange the second when statement for testing purposes by “when: <% succeeded() %>”, it is executed but the following tasks are failing, because variables from there first statement were not defined.

tasks:
  Task1:
    action: some.action
    input:
      ticket_id: <% ctx().ticket_id %>
    next:
      - when: <% regex_match(result().result.Ticket.Article[0].Body, "^Some content.*") %>
        publish:
          - article_dict: <% result().result.Ticket.Article %>
          - regex_result: <% regex_substring(result().result.Ticket.Article[0].Body, '.*Details:(\d{1,3}) (.*)') %>
      - when: <% ctx(regex_result[0]) = "12" %>
        publish:  
          - some_value: "test123"
        do: Task2
  Task2:
    action: core.http

Any ideas?
Thanks.

@wingi The variables published in the first when branch are isolated to that specific branch and not accessible in the second when clause. You are probably thinking that these statements run sequentially, however they are instead run in parallel so state across the branches is not shared.

Does this make sense?

If you need to access that regex_result variable you’ll need to transition to another task and access it there.

Thanks for your response. Ok this is understood now, so I changed the code like this:

tasks:
  Task1:
    action: some.action
    input:
      ticket_id: <% ctx().ticket_id %>
    next:
      - when: <% regex_match(result().result.Ticket.Article[0].Body, "^Some content.*") %>
        publish:
          - article_dict: <% result().result.Ticket.Article %>
          - regex_result: <% regex_substring(result().result.Ticket.Article[0].Body, '.*Details:(\d{1,3}) (.*)') %>
      - when: <% regex_substring(result().result.Ticket.Article[0].Body, '.*Details:(\d{1,3})') = "12" %>
        publish:  
          - some_value: "test123"
        do: Task2
  Task2:
    action: core.echo message="<% ctx(article_dict) %> +++++ <% ctx(some_value) %>"

If I do it like this, both when clauses are true and executed successful.
But in Task2 I can now only access the variables which are included in the when clause which also includes the “do” step. In the example above only “some value” is available in Task2, “article_dict” is unknown.
I though that all variables are published now and can be accessed in the second task. If I access both variables in the output part to check for the value, they are successfully there.

output:
  - Article: <% ctx().article_dict %>
  - SomeValue: <% ctx().some_value %>

I also tried if it works if I have a step in between and use the values in step 3. Also not successful.

By the way, is there a possibility to cascade when clauses? Any else pathes available?

@wingi Each when clause is a completely separate and independent branch, context is not shared between them. In your workflow above your first when clause publishes variables, but that happens independently of your second when clause. This is why task2 doesn’t see the value for article_dict being set.

No, there is no else option for when clauses either.

Ok thanks. What would be your solution for following two use cases I try to solve?

Description:
First task is executed.
If something is contained in the result I want to publish a variable which can be used in the later steps of the workflow.
If something else is contained as well, I want to publish a additional variable which can be used in the later steps of the workflow.

Use Case1:
Both conditions can be met or only one.

Use Case2:
Condition two should only be checked if condition 1 is fulfilled.

Any possibility to fulfil this in the next part of the first task or do I need to create additional “decision” tasks including joins and so on?

I thought if I have two when clauses (understood now that they are checked in parallel and not serial) and they are true, the variables are published and are “globally” available for the following steps of the workflow as they can also be used in the output part of the workflow.

Have you tried using the YAQL select on the publishes?
For your first case that you might be able to do it with the YAQL switch function on the publish, so that from task1 you go to task2 without specifying any when. But then in the publish use the yaql switch so the variable is set to your value “test123” if the condition is met, or null if it’s not - see https://yaql.readthedocs.io/en/latest/standard_library.html#switch.