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})
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
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.
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.)
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”