Help

Upcoming database upgrades. Airtable functionality will be reduced for ~15 minutes at 06:00 UTC on Feb. 4 / 10:00 pm PT on Feb. 3. Learn more here

Zapier code to run each step in the Zap for each individual record in a view or search

3187 1
cancel
Showing results for 
Search instead for 
Did you mean: 

This loops through all the records from a view or search and performs the following steps for each record.

It is intended to run after something else triggers it.

It returns all fields of the records as well (as long as you include them in your query), so that you can use them as ingredients in the following steps of the zap without having to do a separate lookup.

TEdit_Step___Zapier

Here is the code where you can copy and paste it.

var settings = {
  'method': 'GET',
  'headers': {
    "Authorization": "Bearer <API KEY HERE>",
  },
};
fetch('http://api.airtable.com/v0/APP_NAME_HERE/TABLE_HERE/?QUERYS_GO_HERE', settings)
.then(function(res) {
  return res.text();
})
.then(function(body) {  // body is the raw return from the API call
  body.toString();
  console.log();
    var jsonData = JSON.parse(body);
    var length = jsonData.length;
    var output = [];
      for (let i = 0; i < jsonData.records.length; i++){    // This for loop processes each record and adds it to the array
        output.push(jsonData.records[i].fields);
      }
  callback(null, output);
})
.catch(callback);
1 Reply 1

This is awesome. When I tried to replicate for a relevant use case, I found that I had to replace var length = jsonData.length with var = Objects.keys(body).length full example below. But seriously, this helped me so much!

var settings = {
  'method': 'GET',
  'headers': {
    "Authorization": "Bearer YOUR_API_KEY",
  },
};
fetch('https://api.airtable.com/v0/YOUR_APP_ID/YOUR_TABLE_ID/?view=YOUR_VIEW_NAME_(REPLACE_ANY_SPACES_WITH_%20)', settings)
.then(function(res) {
  return res.text();
})
.then(function(body) {
  body.toString();
  console.log();
    var jsonData = JSON.parse(body);
    var length = Object.keys(body).length;
    var output = [];
      for (let i = 0; i < jsonData.records.length; i++){
        output.push(jsonData.records[i].fields);
      }
  callback(null, output);
})
.catch(callback);