Help

Conditional Sequential Numbering via Script

Topic Labels: Extensions
1180 1
cancel
Showing results for 
Search instead for 
Did you mean: 
Martin_Kranz
6 - Interface Innovator
6 - Interface Innovator

Hey there,
I am faced with a seemingly simple problem that I’m loosing far too much hair over. Bein in my late 30s, that’s something i really cannot afford any more and I am turning to the community to help here.

Basically, I need assistance with a script that generates product id’s for designs, based on the designer and a variant of that to regenerate those codes if necessary.

Our setup is a base with a table with Designs and another table with Designers - the schema for the Product IDs is Designer ID-Design ID so if our Designer’s ID is 100 and the Designs in question has the ID 23 the Product Id would be 100-23

Our backend / shop software (magento, nightmare) requires these codes to be sequential and doesn’t like gaps, so if the designs 100-23 was removed during production, we’d need to regenerate all product id’s that follow the 23, as simply omitting the design is not an option.
This is where the script comes in.

The starting point is the code here Calculate record number in particular view which I tweaked to

  1. filter the query result to show only in-progress files from this designer
  2. attempt to clear the field called Entity which holds the value 23 in my example.

I am able to filter the query fine using this script, but when trying to push the updates, it won’t succed and produces the error

TypeError: Cannot read property ‘forEach’ of undefined at line 23

This only happens when i am using the filtered array (filteredRecords) though as long as i stick to (query) they will update just fine but that will clear the values in all Designs my team is working on, so I’d need the possibility to limit this functionality to certain groups of records only.

This is the script:

// Customize these values
let tableName = "DESIGNS";
let viewName = "🔹 2. DESIGN LAUNCHES";

// Main script starts here
let table = base.getTable(tableName);
let view = table.getView(viewName);

let trigger = await input.recordAsync('Pick a record', view);

// GET FILTER VALUES FROM TRIGGER RECORD

if (trigger) {

let designer = trigger.getCellValueAsString("#h Artist Name");
let asana = trigger.getCellValueAsString("➕ Asana");

// QUERY ALL RECORDS WITH THIS LAUNCH

let query = await view.selectRecordsAsync();

 let filteredRecords = query.records.filter(record => {
let name = record.getCellValueAsString('➕ Asana');
return name == asana
  });


console.log(query);
console.log(filteredRecords);

let designs = [];
filteredRecords.records.forEach(record => {
let index = filteredRecords.records.indexOf(record) + 1;
designs.push({
    id: record.id,
    fields: {
        'Entity': null
    }
})
});

// Update all records
while (designs.length) {
await table.updateRecordsAsync(designs.slice(0, 50));
designs = designs.slice(50);
}
};

In an additional step, we’d then need to generate new id’s without the, in this example, 23, which would likely be a different script - we are already having a rollup field on the designer table which has the lowest available sequential number for each designer, and would need to simple generate new id’s from that number onwards.

Any help would be appreciated, I am new to scripting and a) worried I will be bald by the time this task has been completed and b) concerned how this experience might effect my love affair with AirtTable.

Cheers

1 Reply 1
Martin_Kranz
6 - Interface Innovator
6 - Interface Innovator

PS: the trigger record is right now chosen in the script but irl my team would use a button on the airtable ux so that they can easily and direclty regenerate the IDs for which ever design is being worked on, this is where we’d get the filter values from.