Custom Code Airtable Record Update

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

Hi Felix,

Unfortunately you just can’t do it without having a severe security breach.
You’re exposing Airtable credentials inside a frontend custom code => this is a severe security breach as it can be visible by anyone having access to your app.

Softr is really not to be used to send data to a database using custom code only.

Thanks Matthieu for your reply!
I know this is not ideal. Is there a way to put only CSS on top of a Softr native button within Softr or is there any other alternative?

Yes, what do you want to do exactly, what is the issue with the current native buttons?

There is nothing wrong with the actual native button. But on top of the native button, I’d like to have a list of criteria and spinning (loading) wheels before each list item, which turn into checkmarks afterwards. This whole element should be in a 50:50 container (see screenshot below).

As far as I understood all the Softr elements, this is not possible without custom code. I also could not find an option to stack several elements on top of each other in one 50:50 container.