cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Workflows' Send Email Task: Cannot reference looping variable in Jinja For-Loop

calogajac
Frequent Guest

I'm attempting to run a task in a workflow where I send an email with certain information collected in a Javascript step that runs before it. It's important to note that the Javascript step result returns a number of Javascript structs with variables in them. Since I want to send an email for each of these structs, I am using the looping option in the Task. The looping variable in this case is called "teams" (referenced as {{ _.teams }}). 

 

It seems that for the most part when it comes to pulling values from this struct in the "Subject" and "To" fields, referencing this variable works just fine. However, I am using a Jinja for-loop in the content and am running into issues.

 

It's important to note here that within each struct/item I'm looping through is a list of structs, with each struct containing further information. This list of structs is called "resourcesToCorrect", and I'm referencing it in the content as {{ _.teams.resourcesToCorrect }}. Printing out the struct in the content works just fine and returns the data, but because I'm not doing any formatting, it's unreadable. Hence why I'm wanting to loop through this list, so that I can format the information in a way that is easy to read by the receiver. 

 

This is where I'm running into issues. I'm using the Expression Reference document to set up a for-loop in this case. Based on the documentation, it seems like I should be setting up my loop like so:

 

{% for resource in {{ _.teams.resourcesToCorrect }} %}
- {{ resource.name }}
{% endfor %}

 

Unfortunately, when I try to run this step, I get the following error:
"Error evaluating 'content' in input. expected token ':', got '}'
Use the expression preview in edit mode to review the input."

Screenshot 2025-03-24 at 1.58.46 PM.png

I'm not sure why it's expecting ":", as this is not suggested in either the Dynatrace or Jinja documentation. With that said, I realize Dynatrace does not have examples for using a loop item in a Jinja for-loop. 

 

What have I done wrong here?

1 REPLY 1

The Jinja docs seem to indicate that the double curly braces are akin to a 'print' statement that would be used to insert a string to the actual output.

  • {{ ... }} for Expressions to print to the template output

As such, I think the fix may be to not use the double curly braces inside of the for statement.

{% for resource in _.teams.resourcesToCorrect %}
- {{ resource.name }}
{% endfor %}

This is also how the examples are written in Dynatrace's docs.. The output of previous workflow steps is referred to directly without being enclosed in the double curly braces.

Featured Posts