Help Creating a Script to Populate a Junction Table with Linked Records

Hello.

I’m trying to write a script that loops through all of the records in the table MyTable1, and for each record in MyTable1 loops through all of the records in table MyTable2, and at each iteration checks to see if table MyTable3 contains a record where Field1 is a link to the current iteration’s record in MyTable1 and where Field2 is a link to the current iteration’s record in MyTable2.

I’m essentially trying to automate populating a junction table where MyTable3 contains every possible combonation of linked records from MyTable1 and MyTable2.

There’s a great example of how to something similar in another thread (Automatically Populate Junction Tables!), but it wasn’t quite what I was looking for.

The script I’ve written so far is below. I wrote as comments the steps of the script that I’m unsure which functions to use to accomplish that step, and was hoping that someone might be able to help me out with this.

Thanks in advance!

let tableMyTable1 = base.getTable("MyTable1")
let queryMyTable1 = await tableMyTable1.selectRecordsAsync();
let recordsMyTable1 = queryMyTable1.records

let tableMyTable2 = base.getTable("MyTable2");
let queryMyTable2 = await tableMyTable2.selectRecordsAsync();
let recordsMyTable2 = queryMyTable2.records

let tableMyTable3 = base.getTable("MyTable3");
let queryMyTable3 = await tableMyTable3.selectRecordsAsync();
let recordsMyTable3 = queryMyTable3.records

for (let recordMyTable1 of recordsMyTable1) {
    for (let recordMyTable2 of recordsMyTable2) {
        
	// If recordsMyTable3 contains record where Field1 = recordMyTable1 and Field2 = recordMyTable2
	// Then nothing
	// Else create record in recordsMyTable3 where Field1 = recordMyTable1 and Field2 = recordMyTable2

    }    
}

for (let recordMyTable3 of recordsMyTable3) {

	// If recordMyTable3 Field1 = null or Field2 = null
	// Then delete recordMyTable3
	// Else nothing

}