Bulk PATCH 10 entries via Google App Script

Hello!

I am trying to sync prices and stocks for several products between Google Sheets (source) and Airtable (destination). Thanks to other posts’ answers in this community I could make a script that updates products one by one. But I would like to reduce the number of requests by adding 10 products at a time on a single request.

I tried several things but I cannot make it work. I guess it’s related to the way the script parses the data.

Here is a simplified version of the script covering only 10 products:
function bulkSendData() {

  const url = "https://api.airtable.com/v0/XXX/";
  const dataSheet = SpreadsheetApp.getActive().getSheetByName("XXXSheetName");
  var productsData = [];

  //Step 1 get the data on Google Sheets
  for (r=2; r<11; r++) {
    var singleRecord = {
      "id": dataSheet.getRange(r,1).getValue(),
      "fields": {
        "Cost": parseInt(dataSheet.getRange(r,2).getValue()),
        "Stock": parseInt(dataSheet.getRange(r,3).getValue())
      }
    }

    productsData.push(singleRecord);

  }

  //Step 2: Create the request
  var options = {
    method: "PATCH",
    headers: {
      "Authorization": "Bearer XXXApiKey",
      "Content-Type": "application/json"
    },
    payload: {
      "records": JSON.stringify(productsData)
    },
    muteHttpExceptions: true
  }

  //Step 3: Send the request
  UrlFetchApp.fetch("https://api.airtable.com/v0/XXXBaseName/XXXTableName",options).getContentText();

Hopefully, someone with more experience here can grasp the issue at first glance.

Thank you for reading. :slight_smile:

Let’s try to change

payload: {
      "records": JSON.stringify(productsData)
}

to

payload: JSON.stringify({
      "records": productsData
})
1 Like

Thank you very much! It’s solved this issue.

This topic was solved and automatically closed 3 days after the last reply. New replies are no longer allowed.