Apr 19, 2021 05:17 PM
I’ve connected a check-in check-out template to a Softr page and we can update inventory from either side, which is great. However I’m trying to add a new item which has a quantity of 24 however, when I jump over to Airtable there aren’t 24 individual records. Is there a way to configure the base so that when we add a new item with more than 1 quantity the base automatically creates a record for each individual item? Thank you!
Apr 19, 2021 06:23 PM
This could be done with an Automation with a “Run a script” action:
{Quantity}
field is greater than 1Name: quantity, Value: the trigger record's value for the quantity field
Name: recordID, Value: the trigger record's record ID
const inputConfig = input.config()
const table = base.getTable("Name of table")
const query = await table.selectRecordsAsync()
const record = query.getRecord(inputConfig.recordID)
const fieldValues = {
fields: Object.fromEntries(table.fields.filter(x => {
return !x.isComputed
}).map(x => {
const value = (!record.getCellValue(x)) ? null : (["singleSelect", "singleCollaborator"].includes(x.type) && record.getCellValue(x).id) || (["multipleSelects", "multipleCollaborators", "multipleRecordLinks"].includes(x.type) && record.getCellValue(x).map(y => {return {id: y.id}})) || record.getCellValue(x)
return [x.id, value]
}))
}
let quantity = Math.max(0, inputConfig.quantity - 1)
let recordValues = new Array(quantity).fill(fieldValues)
while (recordValues.length > 0) {
await table.createRecordsAsync(recordValues.slice(0, 50))
recordValues = recordValues.slice(50)
}
^ that will duplicate the record for however many times to equal the quantity minus 1. This will leave the original record how it is with Quantity still at 24 or whatever.