Hey-
I will spell this out as simply as I can!
I've got 2 tables.
Table 1 has a linked field titled Possible Hosts
The Possible Hosts field contains records that are automatically linked via automation. [the automation basically finds potential hosts for the event request based on certain criteria such as guest count, amenities, etc and the automation will then add those records as linked records] SO FAR SO GOOD!
My question is: I'd like to now sort the linked records so that the records [aka Hosts] with the highest number of 5 star reviews show up first. We have that data point in the linked table of Hosts.
The reason why I need this is because we want to then automatically send the top five Possible Hosts [based on number of 5 star reviews located in the field titled Host Score] to the guest via automation so that they can choose from the best hosts. There will be times when there are dozens of linked records from the Host table but ultimately we only want to send the top 5 options to the guest. I understand that a script can help with this.
Here's what I have so far:
-------------------------------------
let table = base.getTable("Event Requests");
let result = await table.selectRecordsAsync();
let linkedRecords = result.records.map((record) => record.getCellValue("Possible Hosts"));
for (let i = 0; i < linkedRecords.length; i++) {
let linkedTable = base.getTable("Hosts");
let linkedRecordsResult = await linkedTable.selectRecordsAsync({
filterByFormula: `FIND('${result.records[i].id}', {Possible Hosts})`
});
let sortedLinkedRecords = linkedRecordsResult.records.sort((a, b) => {
return b.getCellValue("Host Score") - a.getCellValue("Host Score");
});
let updatedRecord = {
id: result.records[i].id,
fields: { "Possible Hosts": sortedLinkedRecords.map((record) => { return { id: record.id }; }) }
};
await table.updateRecordAsync(updatedRecord);
}
-------------------------------------The ERROR message I get is:
TypeError: Cannot assign to read only property '0' of object '[object Array]'
at main on line 10
I'd TRULY appreciate your help with this. Thanks in advance 🙂