Return expanded Linked Table data of a field via API call


#1

Hi,
Is it possible to return the Linked table children of a table from List Rest call? At the moment a list call seems to return an array of Id’s - is it possible to expand that to include the data from the linked table? The only solution I can see is to iterate through the array of ids and make a Retrieve a Record call for each id of the Linked Field…

e.g. in Ingredient below it returns the an array with Id:recEehttf17ZzJYwq… Could that expand to include the fields from the linked table Ingredient?

{
“id”: “recxTeOfNbAhcTPYS”,
“fields”: {
“Id”: 1,
“Ingredient”: [
“recEehttf17ZzJYwq”
],
“Price”: 14.5,
“Date”: “2017-04-01T11:44:00.000Z”
},
“createdTime”: “2017-04-01T10:40:34.932Z”
}

Thanks,
Andrew


#2

A second request to the API is currently required to fetch linked records. Rather than iterating through all the associated IDs, use a single request with the “or” and “record_id” like this:

filterByFormula = "OR( RECORD_ID() = 'recXXXXXX', RECORD_ID() = 'recXXXXXX')"


#3

amazing… thanks that looks like it will solve…


#4

Any advice on how to do this in nodejs? I’m a beginner to web app development and can’t figure out how to take the existing advice for listing linked records and make it work in JavaScript… Also, do the actual record IDs need to be specified when making the second request to the API?

Any guidance would be greatly appreciated!!


#5

Probably too late for the answer to be useful for you, but below is some boilerplate code (node) that might help others. Just pass the filterByFormula param in the airtable api call. The example is straight from the API docs.

var Airtable = require('airtable');
var base = new Airtable({apiKey: 'YOUR_API_KEY'}).base('appu6DGFWol46Ftnu');

base('Episodes').select({
    // Selecting the first 3 records in Grid view:
    maxRecords: 3,
    view: "Grid view",
    filterByFormula = filterString // Cf. previous post
}).eachPage(function page(records, fetchNextPage) {
    // This function (`page`) will get called for each page of records.

    records.forEach(function(record) {
        console.log('Retrieved', record.get('Name'));
    });

    // To fetch the next page of records, call `fetchNextPage`.
    // If there are more records, `page` will get called again.
    // If there are no more records, `done` will get called.
    fetchNextPage();

}, function done(err) {
    if (err) { console.error(err); return; }
});

Where the different recXXXXXXs are the respective ids of the records from your linked base.

Keep in mind that you will have to build the filter string yourself. Something like this should work :

let filterString = '';
myArrOfIds.forEach((recordId, index) => {
        filterString += (`RECORD_ID() = '${recordId}'`);
        if (index < (myArrOfIds.length - 1)) {          
          filterString += (', ');
        } else {          
          filterString += (')');
        }      
      });

Hope it helps.
Cheers.