Forum Discussion

MattMakris's avatar
MattMakris
Qrew Cadet
5 months ago

Creating a 'foreach' style loop in pipelines

Is there a way to do create a loop in Pipelines that runs a set number of times? For example, I want to run it x times based on user input: When a record is updated, it will make X copies of it based on a "clone quantity" field in the updated record. Thanks!



------------------------------
Matt Makris
------------------------------

5 Replies

  • Your simplest solution would be as Mark suggested in this post and utilize a helper table to build your upsert. 

    A more complex solution would be to use a for loop (in theory so don't hold me to it 100%) that might look like:  

    {% set payload = [] %} OR [%set payload = '' %}

    {% for i in range(a.counter_field) %}

    build a json object you put into the payload or append a csv row if you go the importfromcsv route

    {% endfor %}

    The idea is to set an arbitrary array or string and loop through your counter to build a collection for upsert (REST API) or CSV string to use ImportFromCSV (HTTP API). It would be very difficult to test but you could try and build the payload on the fly inside of the Quickbase Request Channel body input. 



    ------------------------------
    Chayce Duncan
    ------------------------------
    • MarkShnier__You's avatar
      MarkShnier__You
      Qrew Champion

      Thx for the mention Chayce.

      My suggestion is to use a helper table and a saved T2T import called by the Pipeline.  The T2T's run super fast, so a good user experience.



      ------------------------------
      Mark Shnier (Your Quickbase Coach)
      mark.shnier@gmail.com
      ------------------------------
  • There are two methods to do this,

    Method 1:

    You can create a jinja for each loop with in the make reqeust step. The jinja for each will create a body and you can use that body and send it to qb api to create X copies.

    Method 2: 
    Create a Parent table with just numbers, and connect that to your main table which is the child table. Replace the "Clone quantiy" field with the lookup field. Use the parent table in your pipeline, when every a record is created in the main table. The pipeline will query the parent table, and use the condition to limit the records to number entered. and a simple for each will run for every record in the parent table, and you can put any step with in the for each loop.



    ------------------------------
    ved prasad
    ------------------------------
  • DougHenning1's avatar
    DougHenning1
    Community Manager

    As others have mentioned, there are multiple ways to accomplish this.  However I believe the jinja method is much easier than the others.  In your situation the jinja doesn't need to build the payload, it just needs to create a loop.

    The regex step uses the value from the updated record in step A ("copy_count" in my example) to generate the loop:

    Hope that helps!



    ------------------------------
    Doug Henning
    ------------------------------
  • DwightMunson1's avatar
    DwightMunson1
    Qrew Assistant Captain

    This should give you a good start. To me as long as you understand a little Jinja and API this is the easiest route to go. You may have to adjust depending on what data you're creating the records with. Such as date formatting or casting to a certain data type. 

    Step A: Trigger, with field for quantity of records to create
    Step B: Make a Request
    URL: https://api.quickbase.com/v1/records
    Method: POST
    Content type: application/json
    Body: 

    {
      "to": "tableId", {# Table Name #}
      "mergeFieldId": 3, {# Your Key Field - only needed if you're going to update records, not create new #}
      "data": [ {% for x in range(a.counter_field) %}
        {
           "FID": { {# Foreign Key #}
            "value": "{{a.field_label | int}}" {# I always cast numeric foreign keys to an int #}
          },
          "FID": { {# Field Label#}
            "value": {{a.field_label}}
          },
          "FID": { {# Field Label#}
            "value": {{a.field_label}}
          },
          "FID": { {# Field Label#}
            "value": "{{a.field_label}}"
          }
        }
      {% if loop.last == false %},{% endif %}
      {%- endfor %} 
      ]
    }



    ------------------------------
    Dwight Munson
    ------------------------------