Once I sorted out the Record ID Variable, it seemed to work but I still get some lines out of order. At the moment I am using this code -
// get recordId that triggered automation from
// input variables
let recordId = input.config()['recordId'];
// Change this name to use a different table
let peopleTable = base.getTable("ClinicianMonth");
let registrationsTable = base.getTable('Appointments');
// Prompt the user to pick a record
// If this script is run from a button field, this will use the button's record instead.
let record = await peopleTable.selectRecordAsync(recordId)
// get cell with linked records
let linkedRegistrations = record.getCellValue('Appointments');
if (linkedRegistrations) {
for (let registration of linkedRegistrations) {
// get the full linked record info
let registrationRecord = await registrationsTable.selectRecordAsync(registration.id);
registration['date'] = registrationRecord.getCellValue('Start time')
}
// sorts list by date from earliest date to latest date
linkedRegistrations.sort((a, b) => (a.date > b.date) ? 1 : -1)
// remove the added date attributes after sorting
for (let registration of linkedRegistrations){
if (registration.date) {
delete registration.date;
}
}
// output.inspect(linkedRegistrations)
await peopleTable.updateRecordAsync(
record.id,
{
Appointments: linkedRegistrations
}
)
}