Help

Re: How to Query multiple records?

4207 0
cancel
Showing results for 
Search instead for 
Did you mean: 
Anthony_Pannell
4 - Data Explorer
4 - Data Explorer

I have a list >700 of customer IDs that I want to know more about. How can I query them in bulk rather doing it one by one?

7 Replies 7

Welcome to the community, Anthony! :grinning_face_with_big_eyes: Based on your wording (“query”), I’m guessing you’re coming from another database system. What exactly do you want to do? If you want to see a limited subset of the full 700+ records, you might consider making a view and using filters to narrow down the list. If that’s not it, let us know in greater detail and we’ll try to guide you.

I’m going to pick up on this question, because I have the same question.

In the API, I saw that it shows you how to get a single record, like this:

var Airtable = require(‘airtable’);
var base = new Airtable({apiKey: ‘YOUR_API_KEY’}).base(‘apptDndoI6VkAx3Ko’);

base('projects').find('recAurDC0RVIB2GFU', function(err, record) {
    if (err) { console.error(err); return; }
    console.log('Retrieved', record.id);
});

But what if you have an array of project id’s and you want to find all of them?

How would you do that with CURL and/or the JavaScript methods?

(Also, is there a place that has extensive info about how to query, using the API? Most of the tutorials I found were about using the GUI interface). Thanks!

cor
7 - App Architect
7 - App Architect

One way I can think of, while I dont know what that would look like in code is to build a FilterByFormula parameter consisting of multiple id’s at a time in an OR({id}=‘xxx’,{id}=‘xxx’,{id}=‘xxx’,{id}=‘xxx’,{id}=‘xxx’,{id}=‘xxx’,{id}=‘xxx’{id}=‘xxx’,{id}=‘xxx’{id}=‘xxx’{id}=‘xxx’)

Hopefully this gives someone an idea that will get you to an answer.

Hi @Maiya_O1 - you can only query a single record (if you have the ID) or the whole table or records (with pagination perhaps) - you can’t pass an array of IDs and get just this set back I’m afraid. As per @cor, you could do a “filter by formula” but you’re going to have to programatically form this API call from the array of IDs you have each time. A simpler approach might be to query all records and, once you have the result returned, loop through these and using the filter method, create a new array where the current iteration is included in your array of IDs. See also includes

JB

Maiya_O1
5 - Automation Enthusiast
5 - Automation Enthusiast

Thanks! That sounds like a good idea. I wasn’t sure if there was a way to get them all in one request, but @JonathanBowen indicated it’s not. So your solution sounds promising.

Thank you for letting me know. At least I know I’m not missing something. :star:

Adam_Grant
4 - Data Explorer
4 - Data Explorer

Here’s a shim you can put in your code to provide findAll behavior…

Find this part

const Airtable = require("Airtable");

/* HERE */

Airtable.configure({...});

And add the base_with_find_all function.

const Airtable = require("airtable");

base_with_find_all = (baseId) => {
    const Base = new Airtable().base(baseId);

    return (tableName) => {
        const base = new Base(tableName);
        base.findAll = function(record_ids) {
            return this.select({
                filterByFormula: "OR(" + record_ids.map(id => { return `RECORD_ID()='${id}'` })
                    .join(",") + ")"
            })
        };
        return base;
    };
}
Airtable.base = base_with_find_all;

Airtable.configure({...});

Now you can call findAll as you would the select method, passing in an array of recordIds.

const base = Airtable.base("...");

recordIds = [...];

base("table name").findAll(recordIds)
  .eachPage(function page(records, fetchNextPage) {...}, function done(err) {...});