I want to update my Airtable attachment field with a pdf that is loaded in a Google Drive folder. This is what I have so far, and I think I am almost there. There seems to be an issue (Request failed for https://api.airtable.com returned code 404. Truncated server response: {"error":"NOT_FOUND"}) in the final step (attachPDFToRecord), which I cannot figure out. Any assistance would be appreciated.
function getPDFFilesFromDrive() {
var folderId = "DRIVE FOLDER ID";
var folder = DriveApp.getFolderById(folderId);
var files = folder.getFilesByType("application/pdf");
return files;
}
function getAirTableRecords() {
var apiKey = "APIKEY"; // Replace with your AirTable API key
var baseId = "BASEID"; // Replace with your AirTable base ID
var tableName = "TABLEID"; // Replace with your AirTable table name
var headers = {
Authorization: "Bearer " + apiKey,
};
var response = UrlFetchApp.fetch(tableUrl, { headers: headers });
var records = JSON.parse(response.getContentText()).records;
return records;
}
function matchPDFsWithRecords() {
var pdfFiles = getPDFFilesFromDrive();
var records = getAirTableRecords();
for (var i = 0; i < pdfFiles.length; i++) {
var pdfName = pdfFiles[i].getName();
var storeNumber = pdfName.substr(0, 4);
for (var j = 0; j < records.length; j++) {
var record = records[j];
if (record.fields["Store#"] === storeNumber) {
attachPDFToRecord(record.id, pdfFiles[i].getId());
break;
}
}
}
}
function attachPDFToRecord(recordId, pdfFileId) {
var apiKey = "APIKEY"; // Replace with your AirTable API key
var attachmentField = "IG Attachments (NEW)"; // Replace with your correct field name
var baseId = "BASEID"; // Replace with your AirTable base ID
var tableName = "TABLEID"; // Replace with your AirTable table name
var headers = {
Authorization: "Bearer " + apiKey,
"Content-Type": "application/json",
};
var payload = {
fields: {
[attachmentField]: [
{
},
],
},
};
var options = {
method: "PATCH",
headers: headers,
payload: JSON.stringify(payload),
};
UrlFetchApp.fetch(attachmentUrl, options);
}