Hello All,
As someone who knows nothing about JavaScript, I am trying to write a script that creates a one-to-many link between two tables. I found some code elsewhere on the forum and have modified it slightly.
I have a “Facilities” table that lists out each facility from which we will be collecting data. I also have a “Results” table that has the results from all data collection at every facility. (This table is quite large and new records are added everyday.) The common link between these two tables is the Facility ID. I would like each Facility to have all their results linked (one-to-many relationship) in the “Facilities” table.
Currently, I am able to run the following code and return a one-to-one link. I’m assuming that the output gets rewritten each times it goes through the FOR loop, resulting in only one output. Is there a way to modify the code to have it output multiple links instead of just one?
Here is the code that I am using:
//Define the Facilities table and query
let facTbl = base.getTable("Facilities");
let facQuery = await facTbl.selectRecordsAsync();
//Define Results table and query
let resTbl = base.getTable("Results");
let resQuery = await resTbl.selectRecordsAsync();
//Loop through the records of Results table
for (let resRecord of resQuery.records) {
let tpID = resRecord.getCellValue("TruePani ID");
//Loop through the records of Facilities
for (let facRecord of facQuery.records)
if (facRecord.getCellValue("TruePani ID") === tpID) {
let sourcesOld = facRecord.getCellValue("Sources");
facTbl.updateRecordAsync(facRecord, {
'Sources': [{id: resRecord.id}]
});
}
}
Thank you!
Sam