Help

Sort linked records automatically, methods?

Solved
Jump to Solution
4035 8
cancel
Showing results for 
Search instead for 
Did you mean: 
Kris
6 - Interface Innovator
6 - Interface Innovator

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 Solution

Accepted Solutions
Kamille_Parks
16 - Uranus
16 - Uranus

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.

See Solution in Thread

8 Replies 8
Kamille_Parks
16 - Uranus
16 - Uranus

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.

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.

Good thinking :slightly_smiling_face: 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!

vcgptpurs
5 - Automation Enthusiast
5 - Automation Enthusiast

This is such a clean solution! Is it possible to do the sort from Z-A instead of A-Z?

@vcgptpurs did you figure out how to reverse the sorting?

You can add reverse in script

linkedRecords.sort().reverse()