Skip to main content
Solved

Numbering Newly Created Fields Each Time Script is Initiated

  • June 6, 2022
  • 2 replies
  • 29 views

Forum|alt.badge.img+1

Hello Community!

I have the below script that will create X number of records based on the Episode Count, but I would like to tweak it to auto-number those episodes each time. For instance, Show 1 has 5 episodes, then each episode record would be numbered 1, 2, 3, 4, 5

//Tables

let projects = base.getTable('Shows');
let episodes = base.getTable('Episodes');

//Record Input

let record = await input.recordAsync('Please select a project', projects);

//Record variables

let quantity = record.getCellValue('Ep Count');
let order = record.id;

//New record array

let recArray = [];

//Iterate by quantity

if(quantity > 1){
    for(let i=0; i < quantity; i++){
        //Push to the array
        recArray.push({
            fields: {
                'Shows': [{id: order}],
            }
        });
    }

    //Create new records
    await episodes.createRecordsAsync(recArray);    
}

Any insight would be so incredible appreciated :pray: Thank you!

Best answer by Kamille_Parks11

Sounds like your Episodes table should have a Number field.

        recArray.push({
            fields: {
                'Shows': [{id: order}],
                'Episode Number': i + 1
            }
        });

its i + 1 and not just i because your loop starts i at zero, and I’m assuming you don’t want “Episode 0” to be a result.

Also FYI your script will throw an error if you ever try creating more than 50 episodes at once.

2 replies

Kamille_Parks11
Forum|alt.badge.img+27
  • Brainy
  • 2679 replies
  • Answer
  • June 6, 2022

Sounds like your Episodes table should have a Number field.

        recArray.push({
            fields: {
                'Shows': [{id: order}],
                'Episode Number': i + 1
            }
        });

its i + 1 and not just i because your loop starts i at zero, and I’m assuming you don’t want “Episode 0” to be a result.

Also FYI your script will throw an error if you ever try creating more than 50 episodes at once.


Forum|alt.badge.img+1
  • Author
  • New Participant
  • 1 reply
  • June 6, 2022

Kamille - you are amazing!

This worked perfectly. Thank you so much for the assist here!