Oct 26, 2020 06:16 AM
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
Solved! Go to Solution.
Oct 26, 2020 10:21 AM
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.
Oct 26, 2020 10:21 AM
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.
Oct 26, 2020 11:24 PM
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 😃
Oct 26, 2020 11:33 PM
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.
Oct 26, 2020 11:36 PM
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.
Oct 26, 2020 11:41 PM
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!
Oct 20, 2023 05:51 PM
This is such a clean solution! Is it possible to do the sort from Z-A instead of A-Z?
May 15, 2024 02:17 AM
@vcgptpurs did you figure out how to reverse the sorting?
Jul 06, 2024 12:33 AM
You can add reverse in script
linkedRecords.sort().reverse()