Dear community,
I despair on that one:
I want to update a record (timestamp - Airtable Date format) on a detail page, when a button is clicked with custom code.
I am not a pro coder, but I already tried so many thingevery timetime, something else is not going through. Most of the time, the recordID can not be found. If the record ID can be found, the API key is not correct.
I have the feeling, this should work way easier?
What do I miss?
document.addEventListener(“DOMContentLoaded”, function () {
const button = document.querySelector(“#_ngcontent-ng-c4092286435”);
const recordSection = document.querySelector(“section#item-details4”);
if (!button || !recordSection) {
console.error("Button or record section not found.");
return;
}
const recordId = recordSection.getAttribute("data-recordid");
if (!recordId) {
console.error("Record ID not found in the section.");
return;
}
button.addEventListener("click", async function () {
console.log("Button clicked. Sending request to update Airtable...");
const baseId = "appXXX";
const tableId = "tblXXX";
const columnToUpdate = "Trigger new Product title generation";
const airtableApiKey = "patXXXXXXXXXXX";
const url = `https://api.airtable.com/v0/${baseId}/${tableId}/${recordId}`;
const requestBody = {
fields: {
[columnToUpdate]: new Date().toISOString()
}
};
try {
const response = await fetch(url, {
method: "PATCH",
headers: {
"Authorization": `Bearer ${airtableApiKey}`,
"Content-Type": "application/json"
},
body: JSON.stringify(requestBody)
});
const data = await response.json();
console.log("Airtable response:", data);
if (response.ok) {
console.log("Update successful!");
} else {
console.error("Error updating Airtable:", data);
}
} catch (error) {
console.error("Network error:", error);
}
});
});
I highly appreciate any help!
Cheers Felix