Forum Discussion

Hongkunliang's avatar
Hongkunliang
Qrew Cadet
2 years ago

Jinjia question

Hi, I am not familiar with Jinjia.  I am trying to generate several child records by using Pipeline. The 1st child will get a date "9/21/2023", 2nd one will get "9/22/2023", and so on.  However, it always give the value "9/21/2023" for all the children.  This is my Jinjia code:

{
    "to": "btib9aemmh",
    "data": [
        {% set adate=time.today%}
        {% for num in range(5) %}
        {% set adate=adate+time.delta(days=1)%}  

        {
            "7": {
                "value": "{{a.id}}"
            },
            "18": {
            "value": "{{adate|date_ymd}}"
            }
        }
        {% if loop.last == false %},
        {% endif %}
       
        {% endfor %}
    ]
}

Thanks in advance!



------------------------------
Hongkun liang
------------------------------
  • You are not incrementing the adate counter , try below

    {% set adate = adate + time.delta(days=num) %}

    Optional
    time.today will generate date at utc time . Using time zone help , I generally do 

    {{ (time.now|timezone('Asia/Calcutta'))|date_ymd('-') }}

    Hope this helps.


    All thanks @Quick Base Junkie for awesome jinja training



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

    • Hongkunliang's avatar
      Hongkunliang
      Qrew Cadet

      Hi Prashant,

      I only want to run 5 times, using the variable Adate to add one more day each cycle.

      I saw your post " Jinja using a counter inside loop".  I think I got the same issue.  Trying to approach it as you suggested, however, I am dealing with a date field, when using namespace feature, it has a syntax error. Thanks!



      ------------------------------
      Hongkun liang
      ------------------------------
      • Hongkunliang's avatar
        Hongkunliang
        Qrew Cadet

        One reason I don't use the counter is:

         here I just present a simple example.  In the actual scenario, the variable will change to weekday if the date is following into weekend. So day 1="9/23/2023", it will changed to "9/25/2023", the day2 will be "9/26/2023".  



        ------------------------------
        Hongkun liang
        ------------------------------
  • Hi, this code is based in your original code, this work for me:

    {
        "to": "btk9drq7t",
        "data": [        
            {% for num in range(5) %}        
            {% set adate=time.today+time.delta(days=num+1)%}  
            {        
                "6": {
                "value": "{{adate|date_ymd}}"
                }
            }
            {% if loop.last == false %},
            {% endif %}
           
            {% endfor %}
        ]
    }

    Thank you

    Marcelo Benavides Torres



    ------------------------------
    Marcelo Benavides
    ------------------------------
    • Hongkunliang's avatar
      Hongkunliang
      Qrew Cadet

      Hi Marcelo,  appreciated for the code. In your code the value of adate is re-set each cycle in the loop. However, I'd like to keep the value in the previous cycle, such as 

      Adate= Adate + time.delta(days=1)

      In my actual code, if the date is following into weekend, it will change to the next weekday.  Then the date in the next cycle will be calculated based on the value in the previous cycle.  Thanks!



      ------------------------------
      Hongkun liang
      ------------------------------
  • Just give a quick update.

    By using namespace function as Prashant 's instruction, I am able to calculate each total within each cycle and assign the value to the child table.  Thanks again for Prashant's post.



    ------------------------------
    Hongkun liang
    ------------------------------
    • PrashantMaheshw's avatar
      PrashantMaheshw
      Qrew Captain

      @Hongkun liang ,you've just committed a cardinal solution ,by saying a solution has been found but not posted it for others to stumble across this discussion :D 



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

      • PrashantMaheshw's avatar
        PrashantMaheshw
        Qrew Captain

        For the completist in me !

        {
          "to": "tablename",
          "data": [
            {% set ns = namespace(adate=(time.now|timezone('Asia/Calcutta'))) %}
            {% for num in range(7) %}
              {% set ns.adate = ns.adate + time.delta(days=1) %}
              {# Check if the date is Saturday (5) or Sunday (6) #}
              {% if ns.adate.weekday() == 5 %}
                {# If it's Saturday, add 2 days to make it Monday #}
                {% set ns.adate = ns.adate + time.delta(days=2) %}
              {% elif ns.adate.weekday() == 6 %}
                {# If it's Sunday, add 1 day to make it Monday #}
                {% set ns.adate = ns.adate + time.delta(days=1) %}
              {% endif %}
              {
                "7": {
                  "value": "{{ ns.adate }}-{{ ns.adate.strftime('%A') }}"
                }
              }
              {% if not loop.last %},{% endif %}
            {% endfor %}
          ]
        }
        


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