Linking/appending records script

I realize this is constantly asked in variations but I cannot find a solution to the issue.
Most auto-linking scripts replace the contents of the field, in lieu of appending the new linked record to the field.

I’ve attempted to concatenate the array of IDs, but that throws an error on the field write.

//Define the company table and query
let cmpTbl = base.getTable("Speaker Calls");
let cmpQuery = await cmpTbl.selectRecordsAsync();

//Define contact table and query
let cntTbl = base.getTable("All Interested");
let cntQuery = await cntTbl.selectRecordsAsync();

//Loop through the records and find the Contact ID
for (let record of cmpQuery.records) {
    let cmpid = record.getCellValue("Campaign Name");
    let cmpid2 = record.getCellValueAsString("mem");
let cmptest = record.getCellValue("Scouted");
            console.log(cmpid2);







    //Loop through linked table and match ID values
    for (let cntRecord of cntQuery.records) {
        if (cntRecord.getCellValue("Campaign Name") === cmpid) {
            //Update field
    let cntid2 = {id: cntRecord.id};
    let cntid3 = cntRecord.getCellValueAsString("Member ID");
            console.log(cntid3);

  //Update field
  let full = cmpid2 + ", " + cntid3;
              console.log(full);

            cmpTbl.updateRecordAsync(record, {
                'Scouted': [full]
            });
        }
    }
}```

In general, in Javascript you do not concatenate arrays, you “push” new values into the existing array. That would look like:

existingArray.push(newValueToAddToEnd)


let full = cmpid2 + ", " + cntid3;

^ I believe this forms a string. Record link fields must be passed an array of objects, and you’re passing an array containing a single string. What you want to do is:

let full = [cmpid2, cntid3]

and then your cmpTbl.updateRecordAsync() would be passed:

(record, {'Scouted': full})

However, this assumes that cntid3 returns a proper value in the form of {id: "rec12345..."}. If cntRecord.getCellValueAsString("Member ID") returns just "rec12345..." then full would need to be:

let full = [cmpid2, {id: cntid3}]

Neither cmpid2 nor cntid3 appear to be in the proper form. Both appear to be strings.

@Trent_Warrick See the documentation for the cell read and write formats for a linked record field. They are very different from the formats used outside of scripting.

Ah, I was looking at cntid2, which is in proper form, not cmpid2. Very similarly named variables.

Regardless this solution should work. I toyed with passing strings and arrays to the field.

Array seems to be the proper solution. :smiley: thank you!