Gallery
Kuovonne's Guide to Scripting in Airtable
Share
Explore
Free Scripts

Copy value from one field to another

Your donations tell Kuovonne that you value her content and want her to continue publishing.

Inspiration

When the ability to run a script from a button field was first released, I wrote a script that could copy the value from one field to another in the same record. I that script to the Airtable community, but that post has been disappeared. (You can see a reference to that post
.)

Discussion

First identify the table and fields. It is good practice to define them at the top of the script so that they are easy to change without digging through the code. Note that the source field and the target field must have matching data types, such as both being text fields or both being number fields.
const table = base.getTable("My Table")
const sourceField = table.getField("Original Name")
const targetField = table.getField("Copied Name")
Then get the record that you want to act on. When you use a button field to run the script, the script will use the button’s record instead of prompting the user.
const record = await input.recordAsync('Pick a record', table)
Get the value of the source field.
const value = record.getCellValue(sourceField)
Finally, update the original record.
await table.updateRecordAsync(record, {
[targetField.name]: value
})
Here is the full script.
// setup the table and fields
const table = base.getTable("Table 3")
const sourceField = table.getField("Monday")
const targetField = table.getField("Name")

// get the record and the source field value
const record = await input.recordAsync('Pick a record', table)
const value = record.getCellValue(sourceField)

// update the record
await table.updateRecordAsync(record, {
[targetField.name]: value
})
Setup the button field
image.png

Suggested Variations

If you are using this script to learning coding, consider making these variations of the script:
Convert the script to use .
Convert the script to use the active table and prompt the user to ask for the source and target fields.
Put the script (except for defining the table and fields) in a “main” function. If the user does not pick a record, have the script end gracefully instead of throwing an error.
Have the script copy the value only if the target field is empty.
Adapt the script to use getCellValueAsString() so the script will work with a text-based target field, and a non-text based source field, such as a single-select or number.
Adapt the script to work with a source and target field that are different types, such as a text field and single-select field. See the for more details.
Convert the script to copy the values for all records in the active view. Update the records in batches instead of one at a time.
The content in this guide is free, but creating it takes time and money. If you like this content, .

Share
 
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.