Jul 15, 2020 10:49 AM
We’re using Airtable as a PIM for clothing and we sometimes do modifications that creates a new style code. A lot of the information is the same (as it’s just a modification), but I don’t want to copy over any linked fields or certain notes on an item.
I was wondering if it was possible to create a script that would let you select an item you want to modify and create a new item with select information from the base item. The links are in the same table FYI.
Solved! Go to Solution.
Jul 15, 2020 11:34 AM
You could use a script like this:
let table = base.getTable("Table Name")
let query = await table.selectRecordsAsync()
let records = query.records
let fields = table.fields
let exclude = ["Field 1", "Field 2"]
let filteredFields = fields.filter(x => !exclude.includes(x.name) && x.isComputed == false)
let original = await input.recordAsync("Original Record", table)
let obj = {}
filteredFields.map(x => {
Object.assign(obj, {[x.name]: original.getCellValue(x.name)})
})
await table.createRecordAsync(obj)
The above script lets you add as many fields to the exclude
array as you want. When you run the script it will ask which record you want to duplicate, then it will make a new identical record with all the fields filled in except for the fields you added to exclude
.
Jul 15, 2020 11:34 AM
You could use a script like this:
let table = base.getTable("Table Name")
let query = await table.selectRecordsAsync()
let records = query.records
let fields = table.fields
let exclude = ["Field 1", "Field 2"]
let filteredFields = fields.filter(x => !exclude.includes(x.name) && x.isComputed == false)
let original = await input.recordAsync("Original Record", table)
let obj = {}
filteredFields.map(x => {
Object.assign(obj, {[x.name]: original.getCellValue(x.name)})
})
await table.createRecordAsync(obj)
The above script lets you add as many fields to the exclude
array as you want. When you run the script it will ask which record you want to duplicate, then it will make a new identical record with all the fields filled in except for the fields you added to exclude
.
Jul 16, 2020 12:33 PM
That worked beautifully! This may be asking too much, but is there a way to add the link to the base item? @Kamille_Parks
Jul 16, 2020 02:37 PM
What do you mean by link, the url for a particular record, or updating the value of a link-record field?
Jul 17, 2020 08:33 AM
@Kamille_Parks either really, but a link-record field was what I had in mind.
Jul 17, 2020 09:12 AM
In your original post you said
regardless, adjust the filteredFields.map
section to the following:
filteredFields.map(x => {
let fieldValue = original.getCellValue(x.name)
let method = fieldValue && x.type=="multipleRecordLinks" ? fieldValue.map(y => ({id: y.id})) : fieldValue
Object.assign(obj, {[x.name]: method})
})
Jul 17, 2020 10:13 AM
Thank you! I just want to have a link to the base item.
Apr 12, 2024 12:19 AM
This code doesn't work.