Skip to main content
Solved

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

  • July 15, 2020
  • 7 replies
  • 77 views

Brent_del_Rosar
Forum|alt.badge.img+8

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.

Best answer by Kamille_Parks11

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.

7 replies

Kamille_Parks11
Forum|alt.badge.img+27
  • Brainy
  • 2679 replies
  • Answer
  • July 15, 2020

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.


Brent_del_Rosar
Forum|alt.badge.img+8
  • Author
  • Known Participant
  • 10 replies
  • July 16, 2020

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


Kamille_Parks11
Forum|alt.badge.img+27

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?


Brent_del_Rosar
Forum|alt.badge.img+8
  • Author
  • Known Participant
  • 10 replies
  • July 17, 2020

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.


Kamille_Parks11
Forum|alt.badge.img+27

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

Brent_del_Rosar
Forum|alt.badge.img+8
  • Author
  • Known Participant
  • 10 replies
  • July 17, 2020

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.


Forum|alt.badge.img+1
  • New Participant
  • 2 replies
  • April 12, 2024

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.


This code doesn't work.