Help

Script to duplicate a record with select fields only or create new item based on another

Topic Labels: Extensions
Solved
Jump to Solution
6036 7
cancel
Showing results for 
Search instead for 
Did you mean: 
Brent_del_Rosar
6 - Interface Innovator
6 - Interface Innovator

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.

1 Solution

Accepted Solutions
Kamille_Parks
16 - Uranus
16 - Uranus

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.

See Solution in Thread

7 Replies 7
Kamille_Parks
16 - Uranus
16 - Uranus

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.

That worked beautifully! This may be asking too much, but is there a way to add the link to the base item? @Kamille_Parks

What do you mean by link, the url for a particular record, or updating the value of a link-record field?

@Kamille_Parks either really, but a link-record field was what I had in mind.

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})
})

Thank you! I just want to have a link to the base item.

This code doesn't work.