Update all Records from One Tab To Another

Hello I’m trying to write a script that would copy all records from one tab to another. Source tab is called “Activity” and the destination is called "Contacts

I think I’m almost there, but confounded on a few things.

The script cycles through each record in both tables to:

  • Make sure the emails contact email matches
  • It hasn’t already copied before, dropdown in sync column set to “added to contact”
  • Later will add the values to copy from activity to contact

I’m seem to be almost there, but here’s somethings are confusing me

  1. it seems to be looping through the activities multiple times even though activity only has one loop, why?
  2. It CRASHES when I try to write the Contact Record column in the Activity table. Says “Something went wrong. Reload the block”
  3. The update contact part does not work, nothing gets written to the contact table.
// Test

let contactsTbl = base.getTable(“Contacts”);
let activityTbl = base.getTable(“Activity”);
let activities = await activityTbl.selectRecordsAsync();
let contacts = await activityTbl.selectRecordsAsync();

for (let actRecord of activities.records) {
let actSync = actRecord.getCellValue(“Sync”);
if (actSync == null || actSync.id !==“selDjBUe7oGhNaevS”) {
// set variables to the record values
let activityId = actRecord.getCellValue(“Activity ID”);
let activityEmail = actRecord.getCellValue(“Email”);
let activityRecordId = actRecord.id;
let activityFirst = actRecord.getCellValue(“First”);
// set variable for today’s date

let now = new Date().toLocaleDateString("en-US");
    for (let conRecord of contacts.records) {
        let conEmail = conRecord.getCellValue("Email");
        let conRecordId = conRecord.id;
        if (conEmail && activityEmail) {
            if (conEmail.toUpperCase() == activityEmail.toUpperCase()) {
                        contactsTbl.updateRecordAsync(conRecord, {
                            "Activity Records" :  [{ id: activityRecordId }],
                            "First" : activityFirst
                         }) 
                         output.text(contactsTbl.name+" "+conRecord.name+" "+activityFirst);
                         activityTbl.updateRecordAsync(actRecord, {
                                //"Sync" :{ id: "selDjBUe7oGhNaevS"},
                                "Last" :activityFirst,
                                "Contact Record" :  [{ id: conRecord.id }],
                                
                         })   
                output.text(activityId+'='+conEmail+now+JSON.parse(actSync));
            }
        }
    }
}    

}