Script: update field of a linked record

Hi community!

I’m setting up a script so I can mark order line items as “Delivered” by ticking a checkbox on the order tab. I’ve find a really useful post where a boiler template of code was provided (can’t seem to find it back).

// Setup
const config = input.config()
const ordersQuery = await base.getTable("ORDERS").selectRecordsAsync()
const orderLinesTable = base.getTable("ORDER LINE ITEMS")
const orderLinesRecords = await orderLinesTable.selectRecordsAsync()
const checkedOrder = ordersQuery.getRecord(config.recordID)


// Collect linked tasks and find those that aren't checked
let orderLines = checkedOrder.getCellValue("ORDER LINE ITEMS")
let uncheckedTasks = orderLines.filter(linkObj => !orderLinesRecords.getRecord(linkObj.id).getCellValue("Mark As Delivered"))

// Build array of updates
let updates = uncheckedTasks.map(linkObj => {
    return {
        id: linkObj.id,
        fields: {
            "production_status": DELIVERED (PARTYSPACE) ✅
        }
    }
})

// Update task records
while (updates.length > 0) {
    await orderLinesTable.updateRecordsAsync(updates.slice(0, 50))
    updates = updates.slice(50)
}

I don’t get an error, however as a result i get script input = recordId reci2EvwplbE51f4F.
No changes where made to the desired output field.

Any tips?

Thank you

Welcome to the community, @Quentin_B! :smiley: You didn’t provide details about how you had set up this script, but you tagged your post as “Scripting app”. The script that you listed, though, isn’t designed to run in the Scripting app. It’s designed to run in an automation.

There’s a difference in how the two scripting environments treat input.config(). In the Scripting app, this is designed to provide a way for you to create a modest interface that allows the user to set certain input values for the script. In an automation, however, it’s designed to retrieve input variables that you set up when configuring the “Run a script” action. If you take a script designed to operate in an automation—like the one above—and run it in a Scripting app, it’s not going to behave as designed. If you could share more about how you installed and attempted to run the above script, we could explain more about the output that you received.

That aside, the only obvious error that I see is in this section:

        fields: {
            "production_status": DELIVERED (PARTYSPACE) ✅
        }

The fragment as written as above should lead to an error for a couple of reasons.

  1. Inserting DELIVERED (PARTYSPACE) ✅ with no surrounding characters is invalid. You need to wrap that in quotes in order for it to be seen as a string.
  2. To correctly tell Airtable that the "DELIVERED (PARTYSPACE) ✅" string is meant to represent the name of a choice in a single-select field (guessing that’s what you’re trying to do, as you didn’t describe the design of your base), it needs to be formatted per the requirements of the appropriate field type. If my single-select guess is correct, then that fragment above should look like this:
        fields: {
            "production_status": {name: "DELIVERED (PARTYSPACE) ✅"}
        }

If my single-select guess is incorrect, then please share more details about your base design, along with any more relevant info about how you have attempted to use this script.

Hello Justin,

First of all - a big thanks for your reply and welcoming.

As you state, it is indeed designed to run in an automation as it requires input.
Also, the single select input guess was correct.

Below, I provide some screenshots to visualize my base layout.


At this moment, I’ve updated the script with your suggestion. The script runs and doesn’t return an error. However, the single select field in the order line items table remains unchanged.

Kind regards

Sorry for the delay in getting back to you. My work situation has changed, and I don’t have nearly as much time to spend in here as I used to.

Without a more close examination of your setup, it’s hard to say what’s failing. I would recommend starting by inserting logging statements into your script to check the state of certain variables at key points. Because everything hinges on the updates array actually containing updates, I would start by adding:

console.log(updates)

Put this line right after the array is built and before the updates are processed. My hunch is that you’re somehow getting an empty array. If that’s the case, the next thing to check would be the data feeding into that array being built. Add more logging statements to test various things until you’ve identified the problem, at which point the solution should (I hope) be obvious.