Using a script to add values from one field to another
Non-developer type here! My team is using Airtable as a task management tool Each week we run a report, which includes a long text field (“Details Field”) with details on the work performed on a specific task during that week. To retain a record of the content that’s appeared in the Details Field, I wonder if it would be possible to write a script that would copy and clear the values of the Details Field and cumulatively add them to a field (“History Field”). The History Field would then eventually contain a list of all the updates that’s been put into the Details Field over time (and ideally with date stamps to next to the text that was copied at a given time).
Thanks so much for your feedback on this!
Jens
Page 2 / 2
Hi @Jens_Schott_Knudsen1 - try this script:
// set the table
let notesTbl = base.getTable("Notes");
// get the table records
let notes = await notesTbl.selectRecordsAsync();
// loop through the records
for (let record of notes.records) {
// set variables to the record values
let notes = record.getCellValue("Notes");
let notesHistory = record.getCellValue("Notes History")
// set variable for today's date
let now = new Date().toLocaleDateString("en-GB");
// define the newNotes variable with starter text (includes the date)
let newNotes = "======= Copied " + now + " ======= ";
// only run on records which have notes
if (notes) {
output.text(`Copying notes for record ${record.name}`)
// build the newNotes value from the notes...
newNotes = newNotes + notes;
// ...and the notes history
newNotes = newNotes + notesHistory;
// update the notes history value and reset the notes value
notesTbl.updateRecordAsync(record, {
"Notes History": newNotes,
"Notes": ""
})
}
}
WARNING:
Quickly put together so please try this out on a copy of your base and check that it works for you before you put it into production!
It takes the text from the “Notes” field and prepends it to the “Notes History” field with a date stamp:
JB
Hi guys!
I found your article and its very powerful script.
Was looking for it for a long time.
Im trying to use it as an automation - when Notes is updated, tu run this scrip.
But I have the next log issue:
TypeError: output.text is not a function
at main on line 21
do you have any ideas why it does’t want to run in automation ?
P.S. if tu use the same script via Scrip app - it works great!
Hi guys!
I found your article and its very powerful script.
Was looking for it for a long time.
Im trying to use it as an automation - when Notes is updated, tu run this scrip.
But I have the next log issue:
TypeError: output.text is not a function
at main on line 21
do you have any ideas why it does’t want to run in automation ?
P.S. if tu use the same script via Scrip app - it works great!
output.text is not available in automation scripts, only scripting app. Because there is no user looking at a screen when an automation runs, there is no need for output.
You can comment out that line or you can change output.text to console.log.
@Karlstens - good spot. We can actually simplify this further with a ternary:
let notesHistory = record.getCellValue("Notes History")
//clear the History cell for first entry - removes "null" string
if (notesHistory == null) {
notesHistory = ""
}
could be:
let notesHistory = record.getCellValue("Notes History") ? record.getCellValue("Notes History") : ''
I’m a bit late to this but IMO short-circuiting here would be preferable both in terms of performance and readability:
let notesHistory = record.getCellValue("Notes History") || ''
output.text is not available in automation scripts, only scripting app. Because there is no user looking at a screen when an automation runs, there is no need for output.
You can comment out that line or you can change output.text to console.log.
Brilliant. Thanks!
I think I’m looking for a script similar to this but can’t figure out how to adapt it.
I have a ‘link’ column and I’d like to run a script that whenever is finds a word like ‘amazon’ within the link, it populates my ‘vendor’ column which is a multi-select column with the ‘amazon’ vendor.
Does that make sense?
Is there a way to use the same script, but instead of strings, it adds integers or numbers into the new field (which is also an integer rather than string)?