Help

Re: Sorting Linked Records via Script

1243 0
cancel
Showing results for 
Search instead for 
Did you mean: 
Travis_Lozier
5 - Automation Enthusiast
5 - Automation Enthusiast

Hi community!

I know this question has been asked before but I’m having trouble sorting linked records. I’ve got a project management app that I need to sort updates, showing the most recent update at the top of field. I’m currently able to do this via a manual Batch Update but I would like to create a script that does this automatically.

I found the below script on another thread but it is not working form my base. This was originally posted by @Omer_Frydman and looked very promising.

I have a tabled called “Action Plans” that contains projects. I have another table called “Action Plan Updates”, that contains project updates. I have a linked field in the “Action Plans” table that links records to the “Action Plan Updates”. These are currently displayed oldest to newest. The way I would like the script to work is when a new project update is entered to trigger the sort to happen upon submission. I’m very new to scripting and open to learning. Any ideas on ways to do this via a script or how to edit the below script? All insights are very much appreciated! Thanks. Travis

// Set a trigger: when a record is updated -> watch the linked records field
// Set an action: run a script
// You should set the record ID of the triggering record as an input variable

// Grab the triggering record's ID
let inputConfig = input.config();
let recordID = inputConfig.recordID;


let table = base.getTable("tableName"); // Load table
let queryResult = await table.selectRecordsAsync(); // Get all records in the table
let record = queryResult.getRecord(`${inputConfig.recordID}`) // Find the one with the ID from the input

// Get the linked records array
let linkedRecordsArray = record.getCellValue("linkedRecordsField");

// Sort the linked records array
// Assumes the linked recorods' key is a date
linkedRecordsArray.sort(function(a,b){
  return new Date(b.name) - new Date(a.name);
});

// Update the triggering record with the sorted linked records array
table.updateRecordAsync(record, {
    "linkedRecordsField": linkedRecordsArray
})
1 Reply 1
Martin_Kopischk
7 - App Architect
7 - App Architect

Just to be sure, and my apologies if this is a silly question, but you have replaced the example table and cell name literals ("tableName" and "linkedRecordsField" – only the ones in ASCII double quotes) by the actual ones in your base ("Action Plans" and "<whatever your linked field is called>"), right? Because if not, that is your error right there.