Skip to main content

Create several records from a number

Turn a number into records. Enter 12 units on a development, and Tape creates 12 unit records for you with a short script and a loop, once per development.

Written by Leo

Some records imply others: a development with 12 apartments needs 12 unit records. This recipe reads the number from a field, builds a list from 1 to that number with a short script, and loops over the list to create one record per entry. It also marks the development, so changing the number later doesn't create another set.

Automation editor with the finished recipe: trigger When a record is updated and Number of units has changed in Developments, filter Where Number of units > 0 And Units created Is empty, and Execute a script with one line that builds the list from development_field_number_of_units_decimal_value and saves it in Script output 1, followed by Loop actions for each Script output 1 with Create a record in Units setting Title to Unit and Script output 1 and Development to the development's Name

Note:

  • Limits apply to loops. A run lasts three minutes at most and can use up to 1,000 actions. Large numbers need several runs. Each Create record in the loop counts as one action. The script and the loop itself don't count.

  • Each change can create more records. The marker field in this recipe prevents duplicates. Don't skip it.

What you'll need

  • A database app Developments with the fields Name (text), Number of units (number), and Units created (text).

  • A database app Units with the text fields Title and Development.

  • To build this automation, you need to be a Workspace admin of the workspace the app is in and have Full access to both apps. Anyone who can edit the automation can add Execute script. There's no separate permission for scripts.

Create a record for each unit

The actions of the recipe: Execute a script saving the list in Script output 1, Loop actions for each Script output 1 with Create a record in Units setting Title to Unit and Script output 1 and Development to the development's Name, and below the loop Update the current record setting Units created to Yes
  1. Open the Developments app and click the automations icon at the top right (Create and view automations).

  2. Select New automation, then select When a record is updated.

  3. In the trigger, select the field name. Tape preselects one of the app's fields. Select Number of units under FIELDS, then select the preselected field again to remove it.

  4. Under And if the following conditions are met, select Add filter. Pick Number of units, then > (greater than), then 0. Select Add filter again and pick Units created, then Is empty.

  5. Select Add action → Execute script. Replace the example code with one line that builds the list and saves it in Script output 1. Where the length goes, use the Number of units field. In the script editor, type the record name followed by _field_, here development_field_, and pick the field from the suggestions. enter accepts a suggestion: var_script_output_1 = Array.from({ length: development_field_number_of_units_decimal_value }, (_, i) => i + 1);

  6. Select Add action → Loop actions. Tape preselects Script output 1 next to for each.

  7. Inside the loop, select Add action → Create record. Open the app dropdown after in and select Units.

  8. Select Add field, pick Title, and enter "Unit ". Then type @ and select Script output 1 under VARIABLE. Inside the loop, it holds the current number.

  9. Select Add field, pick Development, and type @ to insert the Name of the development.

  10. Below the loop, outside it, select Add action → Update record. Open the field dropdown, pick Units created, and enter "Yes".

  11. Select Create automation.

The result

When you enter 12 in Number of units, Tape creates Unit 1 to Unit 12 in Units, each with the development's name, and sets Units created to "Yes". Later edits of the number don't create more records, because the filter requires Units created to be empty.

Variations

  • Use a relation field. If Units has a relation field to Developments, use Set record to link each unit to the development instead of copying the name.

  • Start from creation. Use When a record is created if the number is always filled in when the record is made.

  • Add the same values to every unit. Add more rows with Add field inside Create record, for example a Status.

  • Create more than one run can handle. Work through the number in batches. Learn more about batches →

Learn more

FAQs

Can I loop without a script?

Yes, if you already have a list, such as collected records or a field that holds several values. A script is only needed to build a list from a number.

Why did the loop create nothing?

If no record was created, it could be because:

  • The list was empty, so the loop ran zero times. Check the number in the field.

  • The filter stopped the run, for example because Units created already had a value.

  • The script failed. Open Runs, select the run, and check the script step.

Can I undo it?

Created records stay until someone deletes them. To remove them, select the units in the Units database app and delete them together. Tape asks Delete selected records? before it deletes them.

Are you a developer using the Tape API for automations?

The automation API has its own documentation for developers. Full reference: custom_code in the developer docs →

Still have questions? Find answers and get help from community experts in the Tape community →

Did this answer your question?