Sort linked records automatically, methods?

Hello Airtable family,

I’ve previously explored how to sort the linked records, finding the best approach was a manual sort using the ‘Batch Update Block.’

The purpose was so that when using our Airtable-based ordering system, when we clicked a customer record in the customer table, it would show linked orders sorted with the most recent orders at the top.

As I’m not the only one entering orders, it’s become a bit tedious and/or unreliable to use this method. I’m looking for a way to do it programmatically, either with an automation, scripting, or using even using Integromat.

In theory, I’m thinking the best approach might be an automation script that only triggers when a customers ‘linked orders’ array changes, though not sure how to set that up with views, triggers, ect. I can likely figure out the script part from pseudo code.

Any suggestions or ideas to try, I’m all ears! (Or, eyes…?)

Kris

1 Like

Setting an Automation to run when the Link field updates seems to work well for me:

Using a Script Action with a input for “linkedRecords” (which pulls the link field’s “list of name” value), you could use the script:

const inputConfig = input.config()
const linkedRecords = inputConfig.linkedRecords

linkedRecords.sort()

output.set("linkedRecords", linkedRecords)

Which will alphabetically sort the linked records by primary field value from A-Z.

Then add an Update Record step after that using the record ID from the trigger record and the output from the Script Action as the value for the link Record.

1 Like

Thanks @Kamille_Parks!

Got it working nicely. The only inefficiency seems to be that the automation runs twice: once when the linked records array is added to (with a new order), and then again after being changed from the first sort.

Shouldn’t be an issue at all and very pleased with the outcome. Thanks so much for sharing your wisdom Kamille =)

Imperfect solution: Make a view that’s filtered on a Last Modified By field where “Last Modified By IS NOT ‘Automations’”, add that view to the conditions of the Automation.

Now the Automation should only run once. However, if you have some other Automation that runs before you edit the link field, the Automation won’t run.

OR, better solution: You can switch the condition to the Automation runs on to watch a Count field of the number of linked records instead. The Automation will still resort for you, but since resorting doesn’t change the number of linked items, the Automation should only run once.

1 Like

Good thinking :slight_smile: I used a rollup field I’d created for reporting purposes. Works like a charm now, only running the automation when a linked order is created or deleted.

Much appreciated Kamille!

This topic was solved and automatically closed 3 days after the last reply. New replies are no longer allowed.