Can't get emails to output properly

I have received some code from a helpful retool developer to output emails but I can’t get it to work. See below.

//Create a variable called rows that retrieves the table1 data
var rows = table1.data;

function runQuery(){

//Use a for loop to increment through the rows entries one by one,
//printing the name and email.

for(let i=0; i< rows.length; i++){
var ist = i;

var tlname = rows[i]?.ToLastName;
var tfname = rows[i]?.ToFirstName;
var emailad = rows[i]?.ToEmailAddress;
var emailcont = rows[i]?.EmailContent;
var flname = rows[i]?.FromLastName;
var ffname = rows[i]?.FromFirstName;

localStorage.setValue ('mist', ist);
localStorage.setValue ('mtlname', tlname);
localStorage.setValue ('mtfname', tfname);
localStorage.setValue ('memailad', emailad);
localStorage.setValue ('memailcont', emailcont);
localStorage.setValue ('mflname', flname);
localStorage.setValue ('mffname', ffname);
     
console.log("i is: = ", ist);
console.log("To Last Name is: ", tlname);
console.log("To First Name is: ", tfname);
console.log("Email Address is: ", emailad);
console.log("Email Content is: ", emailcont);
console.log("From Last Name is: ", flname);
console.log("From First Name is: ", ffname);

EmailForm2.trigger();
}

}

//Execute the function runQuery()
runQuery();

The table for the data is below.

This generates 4 mails with each Run but they are all for the last row of the table.

Any thoughts?

Mike

You might need to add an await on the trigger to send the e-mail so that it completes the EmailForm2 query before proceeding to the next value in the loop. What I think you might be seeing is the loop execution completing for the 4 table rows (which sets the local storage value) before the first iteration’s email is sent.

EmailForm2.trigger() –> let emailResp = await EmailForm2.trigger()

I added a variable declaration for the response so you can log and inspect the query data for each iteration as well.

1 Like

Are you still working on this, @mdsmith1?

Darren:

Yes.

Well actually I have stopped working on it.

I am able to get variables saved if they are text but not if they are dates.

Mike

I think @pyrrho's recommendation is a good one - you might need to explicitly await the results of one trigger before continuing the loop. Note that you'll need to slightly change the function signature in order to make it work though:

async function runQuery() {
  // body of function
}