Help

Bulk PATCH 10 entries via Google App Script

Topic Labels: API
Solved
Jump to Solution
1928 2
cancel
Showing results for 
Search instead for 
Did you mean: 
Nhrt
4 - Data Explorer
4 - Data Explorer

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. :slightly_smiling_face:

1 Solution

Accepted Solutions
M_m_Non_Baby_s_
4 - Data Explorer
4 - Data Explorer

Let’s try to change

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

to

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

See Solution in Thread

2 Replies 2
M_m_Non_Baby_s_
4 - Data Explorer
4 - Data Explorer

Let’s try to change

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

to

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

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