Apr 12, 2021 01:14 AM
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:
Solved! Go to Solution.
Apr 12, 2021 01:35 PM
Let’s try to change
payload: { "records": JSON.stringify(productsData) }
to
payload: JSON.stringify({ "records": productsData })
Apr 12, 2021 01:35 PM
Let’s try to change
payload: { "records": JSON.stringify(productsData) }
to
payload: JSON.stringify({ "records": productsData })
Apr 12, 2021 08:42 PM
Thank you very much! It’s solved this issue.