Numbering Newly Created Fields Each Time Script is Initiated

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!

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.

4 Likes

Kamille - you are amazing!

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

This topic was solved and automatically closed 3 days after the last reply. New replies are no longer allowed.