Help

Numbering Newly Created Fields Each Time Script is Initiated

Topic Labels: Scripting extentions
Solved
Jump to Solution
804 2
cancel
Showing results for 
Search instead for 
Did you mean: 
darth_flitcraft
4 - Data Explorer
4 - Data Explorer

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!

1 Solution

Accepted Solutions
Kamille_Parks
16 - Uranus
16 - Uranus

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.

See Solution in Thread

2 Replies 2
Kamille_Parks
16 - Uranus
16 - Uranus

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.

darth_flitcraft
4 - Data Explorer
4 - Data Explorer

Kamille - you are amazing!

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