Run one of two queries depending on condition

I have a JS query that should iterate through rows of a table and trigger one of two SMTP queries depending on whether a boolean column is true or false. I've tried rewriting the query in multiple different ways and no matter what I do, it's triggering the same SMTP query for all records (though some ways of writing the query will only trigger the first query and some will only trigger the second). It also will not run if a table row is not selected prior to starting the query.

This is the closest I can get:
image

I'd appreciate any help in figuring this out.

Hi @LindenKel,

Do you want to trigger queries on the row selection in the table?

Yes. I want to select a row, check the first two conditions (attente and absent) and then either skip to the next row or check the third condition (provisoire) and trigger the appropriate query then skip to the next row.

This is my latest variation on the query:

image

It solves the issue where the query would only run if a table row was selected prior to running the query and now sends one email for each record in order, as it should, but it's still running email_confirmation for every record regardless of whether the condition is met.

Weirdly, when I edit the first record (out of 4) to make the first set of conditions (attente == 0 and absent != 1) false, the query only ends up sending one email, for one of the last two records, but if I set it back to true for the first record and set it to false for the second record, the email sends for both the first and second record even though it should skip the second, and then it skips one of the subsequent two records.

I have not figured out why the conditional logic isn't working, but I have found a workaround, which is to remove all the conditional logic from the JS query, trigger both of the SMTP queries for each row, and use the built-in 'disable query' parameter on the SMTP queries to prevent them from running if the row doesn't meet the criteria.

Hey @LindenKel! Welcome back to the community. :slightly_smiling_face:

I'm glad to hear that you found a workaround and were able to get this working!

Looking back at the examples that you've shared, it feels like the root of your issue is selectRow running asynchronously. I'm not sure if this is expected, but the better solution is to just iterate through tableInscription.data normally and dynamically pass each element into email_confirmation.trigger() or email_confirmation_provisoire.trigger() using additionalScope. There's a great guide on how to do that here.

I hope that helps! Let me know if you have any additional questions.

1 Like

Thanks for this. I admit, I don't properly understand how to use additionalScope with an SMTP query (I am learing most of this as I go).

Strangely, though, selecting the row doesn't seem to be the main problem, though I'm sure it would be more efficient to just iterate through. Once I built in a small delay, each email it was sending through the SMTP queries contained the correct data for the row, it was just ignoring all the if-else conditions and running as if the if portion were true every time.

I definitely recommend using additionalScope whenever it makes sense to do so!

It's definitely odd that the condition itself is behaving oddly. :thinking: What are the data types of the attente, absent, and provisoire columns? Are they booleans?