Forum Discussion

BrianDunk's avatar
BrianDunk
Qrew Cadet
21 days ago

Jinja Code to Check for Empty String

Hello, I received technical support from QB to modify a Pipeline that looks for dates when several conditions are met.  The automation searches previous records for the same Employee name, same Topic and Same Category.  If any of those records have a date in the Coaching Counsel Field Enter the most recent two into the Coaching_pipelineText field into the new record that also has the same Employee Name, same Topic and Same Category.  Currently, the pipeline works when there are available dates. However, when there are no dates I receive the Pipeline Error email.  In trying to rectify the issue with QB they state I should do the following:

The error you encountered can be addressed using a Jinja conditional statement to clear the field or to input an empty string ("") when the JSON is blank and the data from the JSON when it's available. Please refer to the article below.

The Update code currently is this:

I am unsure on how to write the Jinja If statement to accomodate for the event that the is either No dates or maybe only one date that meets the criteria.  

Would someone help me write the Jinja statment to make this work?

Brian

  • Mez's avatar
    Mez
    Qrew Cadet

    There are couple items for consideration in your statement: 1. the search step and accompanying loop; 2. definition of the most recent two; 3. into the new Employee record. The basic syntax for checking for a null or empty value would be:

    {% if b.json.data is none %} {{ CLEAR }} {% else %} {{ b.json.data }} {% endif %}

    This assumes you want to clear this field [Coaching_pipelineText] where this code is entered, even if there is existing data, and this is from the new record created for the Employee in context. 

    Thinking about those considerations, safe to assume the only records returned are those for the Employee Name, or at least in the context of the Employee, and then you have a loop after the search step for each of the records returned?

    If yes, then you could use a for loop to interrogate each of the records in that json response for that particular Employee: (basic for syntax)

    {% for rec in b.json.data %} {{ rec }} {% endfor %}

    Seeing the values you're trying to capture from the json data (returned data is 0 [zero] indexed), it would appear you're trying to grab the values associated with two different records, 0 and 1, in the response, which may or may not be associated to that particular record (array) of data (this assumes you have a loop after the search steps).

    If you don't have a loop after the search step, and you're are only returning the most recent two records, then what you have should work since the two returned records are the only associated data with this Employee. 

    To put it together, you could use something close to this: 

    {% if b.json.data[0]['59'] is none and b.json.data[1]['59'] is none %} 
      {{ CLEAR }} 
    {% else %} 
      {{ b.json.data[0]['59'].value }} ; {{ b.json.data[1]['59'].value }}
    {% endif %}

    *edit: when using 'is none' you don't need the '.value', if you do, then use == ""

    {% if b.json.data[0]['59'].value == "" and b.json.data[1]['59'].value == "" %} 
      {{ CLEAR }} 
    {% else %} 
      {{ b.json.data[0]['59'].value }} ; {{ b.json.data[1]['59'].value }}
    {% endif %}