Forum Discussion

ShaneMiller1's avatar
ShaneMiller1
Qrew Cadet
2 years ago

Pipelines Jinja Sum

Hello,
I have a question pertaining to pipelines and the sum function within jinja2.
I have created a pipeline:
Step A: searching through records on table 1
for each A do:
Step B: Look Up a Record on table 2 where the Query is [key field table 2] equals [a.key field table 1]
Step C: Update Record
*for step C I am trying to update a numeric field on table 2 which is a sum of [a.paid_amount] from table 1 where the [key field table 2] equals [a.key field table 1].
I have tried putting the following in Step C for the field that is to showcase the summed value:

{% set total = namespace(value=0) %}
{% for r in a %}
{% set total.value = total.value +
(r.paid_amount|default(0, true)) %}
{% endfor %}
{{ total.value }}

and

{{ a|sum(attribute='paid_amount') }}

I keep getting this error however: Validation error: Incorrect template "{{ a|sum(attribute='paid_amount') }}". TypeError: 'Record' object is not iterable 


Any recommendations of any kind would be greatly appreciated. Thank you

------------------------------
Shane Miller
------------------------------
  • HI Shane

    Below will work .

    A much better explanation is going to come from Others , I will try in my limited Jinja

    {% set total = namespace(value=0) %}
    {% for r in range(a|length) %}
    {% set total.value = total.value + a.value|default(0,true) %}
    {% endfor %}
    {{ total.value }}​

    • To iterate through the records you need to find the number of records to sum up , range(a|length) achieves it 
    • to iterate through the records , you need to refer to each object you've searched , once your loop starts a will allow you to refer to records/objects individually with loop value so if I have 3 records , it will do a[0] , a[1] , a[2] 

    I'm a Jinja newbie myself , so I'm sure some terminology I've used is wrong to explain

    ------------------------------
    Prashant Maheshwari
    ------------------------------
    • ShaneMiller1's avatar
      ShaneMiller1
      Qrew Cadet

      Hey Prashant Maheshwari,
      I appreciate you and Don Larson's feedback. I tried your approach, but sadly it did not work, I received the same error. I then tried combining yours and Don Larson's suggestion of replacing the look up a record with a search records, it failed once more.

      My field in table 1 [paid amount] is a numeric currency field, and my field in table 2 where I am trying to get the summed values to be displayed is also a numeric currency field. Does any of this matter?

      Also, you seemed confident in saying this will work, which makes me believe I am doing something wrong. With your example, and with my field that needs to be summed, I would put this in step C (update record) inside the numeric currency field, correct?:

      {% set total = namespace(value=0) %}
      {% for r in range(a|length) %}
      {% set total.value = total.value + a.paid_amount|default(0,true) %}
      {% endfor %}
      {{ total.value }}​


      ------------------------------
      Shane Miller
      ------------------------------
      • DonLarson's avatar
        DonLarson
        Qrew Elite
        Shane,

        I built a simple test application.



        Then I did a Pipeline to search for Table One then Table Two and then Update

        Total Value = Total Value + Paid Amount

        Screen shot before Run


        Screen Shot After Run




        You can see that each child record in Table Two had $100 add to the Total Value



        Here is the YAML with the Slug x-outed

        # Jinja Sum Test
        #

        # Account slugs:
        # - quickbase[XXXXXXXX]: Pipeline Token <None>
        ---
        - META:
        name: Jinja Sum Test
        - QUERY quickbase[XXXXXXXXX] record search -> a:
        inputs-meta:
        export_fields: '"Paid Amount" <6>'
        table: '"Jinja Sum: Table 1" <bszikwt45>'
        - a<>LOOP:
        - DO:
        - QUERY quickbase[XXXXXX] record search -> b:
        inputs-meta:
        export_fields: '"Total Value" <6>'
        query: '{7.EX.{{a.id}}}'
        table: '"Jinja Sum: Table 2" <bszikxmj2>'
        - b<>LOOP:
        - DO:
        - b<>ACTION quickbase record update -> c:
        inputs:
        total_value: '{{b.total_value+a.paid_amount}}'
        ...






        ------------------------------
        Don Larson
        ------------------------------
  • Shane,

    I have been greatly vexxd by Jinja and Pipelines.  @Prashant Maheshwari and @Doug Henning have been great helps so I suspect the suggestion will solve it.

    The error code you got is not very descriptive and I have had it too many times.  I believe that I can identify one case where it comes up.

    If you try to use data from search out side of the loop then you will get that error.

    Try this structure

    Step A Search
    For Each
    Step B Search​
    For Each
          Step C Update B​

    You have a loop inside of a loop.  If Step C is outside of the Loop for B you will get that error message.

    This might also solve it with a bit less Jinja which is good for my brain.


    ------------------------------
    Don Larson
    ------------------------------
    • PrashantMaheshw's avatar
      PrashantMaheshw
      Qrew Captain
      Thank you . Very elegant solution @Don Larson ! Way more easy to follow

      @Shane Miller when I tried to create the above with a simple app and no looping shenanigans , just Jinja in itself was my focus. 

      Step A Search
      Step B - > Update Code



      ​​
      ​​​​

      ------------------------------
      Prashant Maheshwari
      ------------------------------