My First Retool Code

I am trying to run my first Retool Code which is shown below. The code is largely developed by AI.

The error code at the bottom says that “Email04 is not defined”.

I have a table “Email04” but I don’t know how to connect it to this app.

Could someone tell me how to connect the table.

Mike

Hi Mike,

The error Email04 is not defined happens because in Retool, you can’t directly reference a table name (like Email04) inside a JavaScript query. Retool JavaScript queries only have access to data that comes from other queries or components, not directly from your database tables.

Solution:

  1. Create an SQL query to fetch your table data

    • Go to the Query Editor in Retool.

    • Create a new resource query (choose your database connection).

    • Write this SQL:

      SELECT * FROM Email04;
      
      
    • Name this query something like getEmails.

    • Run it to make sure it returns your data.

  2. Use that query’s data inside your JavaScript query
    Now in your JS code, replace Email04 with getEmails.data, like this:

    function sendEmails() {
      const emails = formatDataAsArray(getEmails.data);
      emails.forEach(email => {
        const { ToFirstName, ToEmailAddress, EmailContent } = email;
        sendEmail(ToEmailAddress, `Hello ${ToFirstName}`, EmailContent);
      });
      return 'Emails sent successfully!';
    }
    
    return sendEmails();
    
    
  3. Run the SQL query before the JS query

    • Either set your JS query to “Run after” the SQL query

    • Or call it directly in code if needed.

1 Like

Thank you so much for your answer,

I think I have set things up as you outlined.

The error message I am getting is:

Maximum Cell Stack Size Exceeded.

Mike

This usually means your function is recursively calling itself without a stopping condition , in your case, SendEmails() is calling itself inside its own loop.

The line

SendEmails(ToEmailAddress, `Hello ${ToFirstName}`, EmailContent);


calls the same SendEmails() function again recursively which keeps going until the JavaScript call stack runs out of memory.

Solution:
You need a separate function to handle sending an individual email.

function sendSingleEmail(to, subject, content) {
// Replace with your actual email-sending logic
console.log(Sending email to: ${to});

}

function SendEmails() {
const emails = formatDataAsArray(GetEmails.data);

emails.forEach((email) => {
const { ToFirstName, ToEmailAddress, EmailContent } = email;
const subject = Hello ${ToFirstName};
sendSingleEmail(ToEmailAddress, subject, EmailContent);
});

return 'Emails sent successfully!';
}

return SendEmails();

I must apologize that I am not able to fill in the section about “Replace with your actual email-sending logic”. I am still learning the JavaScript language and don’t know where to start with this.

I have added some screens of where I am now.

Mike

Try replacing your existing code with this updated version. Since I don’t have full details about your email04 table structure, I couldn’t provide the exact column mappings but this version should work correctly for you.

function sendSingleEmail(to, subject, content) {
// Replace this with your actual email-sending logic
console.log(Sending email to: ${to});
console.log(Subject: ${subject});
console.log(Content: ${content});
}

function SendEmails() {
const emails = formatDataAsArray(GetEmails.data);
emails.forEach((email) => {
const { ToFirstName, ToEmailAddress, EmailContent } = email;
const subject = Hello ${ToFirstName};
sendSingleEmail(ToEmailAddress, subject, EmailContent);
});
return 'Emails sent successfully!';
}

Also, I wanted to share that I offer Retool development services on Fiverr. I’d be happy to help with this or any future Retool projects you may have.

Here’s my Fiverr gig link: https://www.fiverr.com/s/yv59Qy0

Best regards,

1 Like