How to access the ID of a linked record

I’ve made this very simple little setup where I can select a name, and find the records that matches the selection. I can access the ID of a linked recored via lookups, but is there a way to directly access the ID of the linked records from my ‘Select Name’?

let viewStaff = tableContact.getView(tableContactView);
let choosePerson = await input.recordAsync('Pick a person', viewStaff);
let personID = choosePerson.id; //have to use Id's when using linked records
output.inspect(personID)

//loadReportRecords
let reportQuery = await tableReport.selectRecordsAsync();
output.inspect(reportQuery)



let reportRecordsForPerson = reportQuery.records.filter(record => {
    // Arrays in JavaScript have a method called some, which returns
    // true if at least one element in the array matches a condition
    return record.getCellValue(reportField).some(nameRecord =>
        nameRecord.id === personID
    );
});

output.inspect(reportRecordsForPerson)
reportRecordsForPerson.forEach(record => output.inspect(`${record.getCellValue("lookupName")}, ${record.getCellValue("recordID")}`))


reportRecordsForPerson.forEach(record => {
  output.inspect(record.getCellValue("Select Name").forEach(record => record.id))
})

@Kim_Trager Yes, you can access the id from the linked record cell value, like so:

let linkedRecordCellValue = record.getCellValue('Linked record field');

// If the cell is empty, getCellValue returns null
let linkedRecordId;
if (!linkedRecordCellValue) {
    linkedRecordId = null;
}

// If you know there's only one linked record per cell, it's the id of the first item
linkedRecordId = linkedRecordCellValue[0].id;

// If there can be multiple linked records per cell, you can create an array of ids instead
let linkedRecordIds = linkedRecordCellValue.map(linkedRecord => linkedRecord.id);

It looks like you’re already doing this in the last part of your snippet — does that not output the IDs you’re looking for?

1 Like

Thank you @Stephen_Suen, I think i wrote my question a little too fast.
What I’m having problems with is figuring out how to check the ID’s of the linked records in reportRecordsForPerson variable, which is an array of objects.

Instead of using having to relly on lookups to check the ID’s of the linked records, I want to have an array of all the id’s of my filtered linked records, so I can check that

let personID === nameRecord.id

I’m not sure if my question makes sense?
I can’t wait to get better at this!

Initially you wanted to check based on name (“Dan”) instead of record ID. If you still want to do that, you can.

In the array for the linked records, just get the .name instead of the .id.

let personName = "Dan";
let linkedRecordName = linkedRecordCellValue[0].name;

if (personName == linkedRecordName) {
  // it's a match
}

Thanks @kuovonne I think what I’m wondering if I can reach into another table via the linked field - a bit like a look up?

I don’t quite understand your question.

A linked record field stores only the name and id of the linked record.
To get info from other fields in the linked record, you have to do a .selectRecordsAsync() on the other table and find the record you wanted.

2 Likes

I see.
I think that answers my question, I somehow thought that I could access fields in the the other table via the link a bit like a lookup.
But makes sense that it only stores Name and ID