Forum Discussion

WilliamHunter2's avatar
WilliamHunter2
Qrew Member
11 months ago

Pipeline Logic

I have made a scheduled pipeline that will make a request to a table and send an email with the correct formatting. All of this is working correctly, however, if the table is blank the pipeline is still sending a blank email with just the field headers, how can I make a decision with the pipelines so if the table is blank it does not send an email? 

Pipeline:

I also tried to send a message saying all caught up if there were no records in the table, but then it does not format the table correctly if it is not blank.

Modified code:

<style>
table, th, td {
  border: 1px solid black;
  border-collapse: collapse;
}
th, td {
  padding: 15px;
}
</style>
<h2>Report Name</h2>
{% if a.json.data|length > 0 %}
    <table>
    <tr>
    {# Loop through the Fields object to create the table headers #}
    {% for object in a.json.fields %}
            <th>{{object['label']}}</th>
    {% endfor %}
    </tr>
    {# Loop through the records and create a row #}
    {% for record in a.json.data %}
    <tr>
    {# Loop though fields #}
    {% for field in a.json.fields %}
            {# Grab the records field data and format based on Field Type #}
            {% if field['type'] == 'percentage' %}
                  <td>{{"{:,.2f}%".format(record[field['id'] | string]['value'] | float * 100)}}</td>
            {% elif field['type'] == 'currency' %}
                  <td>{{"${:,.2f}".format(record[field['id'] | string]['value'] | float)}}</td>
            {% elif field['type'] == 'user' %}
                  {# Retrieve the user's name from the 'user' field #}
                  {% set user_data = record[field['id'] | string]['value'] %}
                  {% set user_name = user_data['name'] %}
                  <td>{{ user_name }}</td>
            {% else %}
                  <td>{{record[field['id'] | string]['value']}}</td>
            {% endif %}
    {% endfor %}
    </tr>
    {% endfor %}
    </table>
{% else %}
  <p>All Time Entries Approved</p>
{% endif %}

Original Code that works fine:

<style>
table, th, td {
  border: 1px solid black;
  border-collapse: collapse;
}
th, td {
  padding: 15px;
}
</style>
<h2>Report Name</h2>
<table>
<tr>
{# Loop through the Fields object to create the table headers #}
{% for object in a.json.fields %}
    <th>{{object['label']}}</th>
{% endfor %}
</tr>
{# Loop through the records and create a row #}
{% for record in a.json.data %}
<tr>
{# Loop though fields #}
{% for field in a.json.fields %}
    {# Grab the records field data and format based on Field Type #}
    {% if field['type'] == 'percentage' %}
      <td>{{"{:,.2f}%".format(record[field['id'] | string]['value'] | float * 100)}}</td>
    {% elif field['type'] == 'currency' %}
      <td>{{"${:,.2f}".format(record[field['id'] | string]['value'] | float)}}</td>
    {% elif field['type'] == 'user' %}
      {# Retrieve the user's name from the 'user' field #}
      {% set user_data = record[field['id'] | string]['value'] %}
      {% set user_name = user_data['name'] %}
      <td>{{ user_name }}</td>
    {% else %}
      <td>{{record[field['id'] | string]['value']}}</td>
    {% endif %}
{% endfor %}
</tr>
{% endfor %}
</table>



------------------------------
William Hunter
------------------------------

4 Replies

  • You should be able to evaluate with an if condition where json.data.length is greater than 0, then send the email. So instead of request/send email you would do request / if condition json.data.length > 0 / send email thus you'd only be sending an email when at least one record is returned. 



    ------------------------------
    Chayce Duncan
    ------------------------------
    • WilliamHunter2's avatar
      WilliamHunter2
      Qrew Member

      In the modified code this is exactly what I did. Please see the code below. However that did not work, it formatted properly when there was no data, but no longer did when there was data. Unless you mean implementing this in the pipeline and not the code in which case, I do not know how to do an if-then condition in a pipeline. Could you assist if that is what you mean? 

      Modified code:

      <style>
      table, th, td {
        border: 1px solid black;
        border-collapse: collapse;
      }
      th, td {
        padding: 15px;
      }
      </style>
      <h2>Report Name</h2>
      {% if a.json.data|length > 0 %}
          <table>
          <tr>
          {# Loop through the Fields object to create the table headers #}
          {% for object in a.json.fields %}
                  <th>{{object['label']}}</th>
          {% endfor %}
          </tr>
          {# Loop through the records and create a row #}
          {% for record in a.json.data %}
          <tr>
          {# Loop though fields #}
          {% for field in a.json.fields %}
                  {# Grab the records field data and format based on Field Type #}
                  {% if field['type'] == 'percentage' %}
                        <td>{{"{:,.2f}%".format(record[field['id'] | string]['value'] | float * 100)}}</td>
                  {% elif field['type'] == 'currency' %}
                        <td>{{"${:,.2f}".format(record[field['id'] | string]['value'] | float)}}</td>
                  {% elif field['type'] == 'user' %}
                        {# Retrieve the user's name from the 'user' field #}
                        {% set user_data = record[field['id'] | string]['value'] %}
                        {% set user_name = user_data['name'] %}
                        <td>{{ user_name }}</td>
                  {% else %}
                        <td>{{record[field['id'] | string]['value']}}</td>
                  {% endif %}
          {% endfor %}
          </tr>
          {% endfor %}
          </table>
      {% else %}
        <p>All Time Entries Approved</p>
      {% endif %}



      ------------------------------
      William Hunter
      ------------------------------
      • ChayceDuncan's avatar
        ChayceDuncan
        Qrew Captain

        Yes, I would recommend it in the pipeline itself given that in your setup you're still sending the email, just with a blank table / no records. My understanding of your original question is how to not send the email at all - so you need the pipeline to not send the actual email which means it can't be in the email action body. In this case - in the pipeline step you would do an if condition and evaluate data.length accordingly from the response to see if you should continue on to evaluate and send the email altogether. 



        ------------------------------
        Chayce Duncan
        ------------------------------