Skip to main content
Question

Pipelines Jinja Sum

  • January 18, 2023
  • 0 replies
  • 514 views

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
------------------------------
This topic has been closed for replies.

Forum|alt.badge.img+11
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
------------------------------

Forum|alt.badge.img+15
  • Registered
  • January 19, 2023
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
------------------------------