Help

Update a record in a table

Topic Labels: Scripting extentions
Solved
Jump to Solution
2076 3
cancel
Showing results for 
Search instead for 
Did you mean: 
Devon_Stanton
5 - Automation Enthusiast
5 - Automation Enthusiast

I’m not a programmer, I’m trying to update a record based on the conditions selected. I’m battling to understand the ‘await’ function.

I believe it’s a promise and that it splits the code until the conditions are fulfilled? I kept getting an error if I put the await inside the if conditions.

I’ve re-written this a few hundred times and tried looking up what other people have done and now I’m throwing in the towel.

Below is the full script, any guidance would be hugely appreciated.

// Change this name to use a different table
let table = base.getTable("Game Tracker");

// Prompt the user to pick a record 
// If this script is run from a button field, this will use the button's record instead.
let record = await input.recordAsync('Select a record to use', table);
let seats = record.getCellValue("Employees");

let segmentSize;
await table.updateRecordAsync(record, {"Segment": segmentSize})

if(seats) {
    if(seats > 300) {
            segmentSize = "Enterprise";
        } else if (seats < 300 && seats > 149) {
            segmentSize = "Upper Mid Market";
        } else if (seats < 150 && seats > 49) {
            segmentSize = "Lower Mid Market";
        } else if (seats < 50 && seats > 9) {
            segmentSize = "SMB";
        } else {
            segmentSize = "Indies"
    }

}

output.text(`The segment for ${record.name} has ${seats} is ${record.getCellValue("Segment")}`)

the output is usually ‘Null’ for the segment.

1 Solution

Accepted Solutions
kuovonne
18 - Pluto
18 - Pluto

Welcome to the Airtable community!

I agree with chechedotmx that I would normally use a formula field instead of a script for this. However, you probably have your own reasons for wanting a script.

Thank you for letting us know your background. It is helpful when providing advice.

await itself is not a function. It is a keyword that says to wait until the function has completed everything before continuing. (To get technical the code is waiting until the promise is resolved.) However, await is not the cause of your problem.

Your problem is that you are updating the record with segmentSize before you have assigned a value to segmentSize.

You need to move this line after the end of your if statement, just before the output.text statement.

await table.updateRecordAsync(record, {"Segment": segmentSize})

You should also update your output.text line to use the segmentSize variable, instead of the previous record value, as chechedotmx recommended.

Your if statement chain could also use cleaning up, but that is another issue. (By the way, I am a professional Airtable consultant, and scripting is one of my specialties. If you are interested in additional assistance, feel free to book an appointment with me.)

See Solution in Thread

3 Replies 3
SergioCodes
8 - Airtable Astronomer
8 - Airtable Astronomer

Hi there @Devon_Stanton ,

My first approach will be to do this with formula instead of a script, as I don´t see the justification of doing a script here.

Anyway.

When you get the record:
let record = await input.recordAsync('Select a record to use', table);

Imagine that in the record variable, you got a copy of whatever is in that record at that specific time,

Then you update the table with:
await table.updateRecordAsync(record, {"Segment": segmentSize})

As you can see, “record” is still the same as when you prompt the user to pick the record; it means that the update didn´t change that “record” variable.

So my first and simple option would be to use you “segmentSize” variable,

output.text("The segment for ${record.name} has ${seats} is ${segmentSize}")

But if you need the updated value to come from the updated field, you should use this code before the output:

let query = await table.selectRecordsAsync();
record = query.getRecord(record.id);
output.text(`The segment for ${record.name} has ${seats} is ${record.getCellValue("Segment")}`)

So it can “refresh” the record variable with the current data in the table.

Btw I think your “if block” should go before the “table.update”

I hope that it helps
Sergio

kuovonne
18 - Pluto
18 - Pluto

Welcome to the Airtable community!

I agree with chechedotmx that I would normally use a formula field instead of a script for this. However, you probably have your own reasons for wanting a script.

Thank you for letting us know your background. It is helpful when providing advice.

await itself is not a function. It is a keyword that says to wait until the function has completed everything before continuing. (To get technical the code is waiting until the promise is resolved.) However, await is not the cause of your problem.

Your problem is that you are updating the record with segmentSize before you have assigned a value to segmentSize.

You need to move this line after the end of your if statement, just before the output.text statement.

await table.updateRecordAsync(record, {"Segment": segmentSize})

You should also update your output.text line to use the segmentSize variable, instead of the previous record value, as chechedotmx recommended.

Your if statement chain could also use cleaning up, but that is another issue. (By the way, I am a professional Airtable consultant, and scripting is one of my specialties. If you are interested in additional assistance, feel free to book an appointment with me.)

Thank you,

I figured it out in the end.