Help

Re: Using a script to add values from one field to another

3336 1
cancel
Showing results for 
Search instead for 
Did you mean: 
Jens_Schott_Kn1
4 - Data Explorer
4 - Data Explorer

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

31 Replies 31

@Mohd_Abdulatef, yes, as per @kuovonne, the error is saying that the two multi-select fields don’t have the same options, which is required for this particular script to work.

Adam_Finley
4 - Data Explorer
4 - Data Explorer

Hey Everone! Similar to @Jens_Schott_Knudsen1 above, I’m wondering if I could get some scripting help. It’s super similar to her request (but not similar enough for me to figure out on my own…).

I’m looking for a script that will take a numerical value from one “Added Quantity” and add it to a numerical value in “Current Quantity” … then clear the “Added Quantity” cell.

Is anyone able to give me a hand with this one? I can’t crack the code and am not much of a script’er :frowning:

Thanks!

  • Adam

Hi @Adam_Finley, were you able to find a solution to this? I have a similar issue.

Thanks,
Erica

Hey!

I did. I ended up solving this with three automatons. The first automation takes the input value from Cell 1 and adds it to Cell 2. The second automation takes the Cell 2 value and copies it to Cell 3. The third automation clears Cell 1 and re-sets it back to zero. So when you input a value in Cel 1, at the end of the automation, it is added to Cell 3, Cell 1 is cleared and ready for a new input value :slightly_smiling_face:

Such a cool example and an excellent script for an Airtable Script App introduction.

Thanks to your post - Airtable scripts are all starting to make sense to me. So much so that I modified your original script to remove a “null” string bug and also amended the output with the copied notes detail.

// set the table
let notesTbl = base.getTable("Notes");
// get the table records
let notes = await base.getTable("Notes").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")
    //clear the History cell for first entry - removes "null" string
    if (notesHistory == null)  {
      notesHistory = ""
     }
    // 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 "${notes}" for record ${record.name}`) //print notes in debug.
        // 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": ""
        })        
    }
} here

Oh man, I’m going to lose hours… days? to Airtable scripts! So cool!

@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") : ''

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.

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") || ''

Brilliant. Thanks!

jp logo.png