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
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.
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
Suggested Variations
If you are using this script to learning coding, consider making these variations of the script:
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