I'm trying to create a Flow that sends an email when a new sales order is added to our SQL Server database. The email is converting the SQL data to an HTML table and sending an email displaying the table within the email body.
Everything from the trigger of a new sales order being created to the email being sent is working as expect but I'm having trouble limiting the SQL results to be within the sales order that was created. First I must share that I did not have any "Top Count" set in my first test so I dealt with thousands upon thousands of emails being sent to myself this morning. I then changed Top Count to 1 thinking this would limit it to a single order, but in reality it limited the results to a single line item on the sales order even though there was multiple. I increased Top Count to 20, which is more than the line items on the order. This resulted in the email containing all 3 or the line items from the test sales order and then it proceeded to list additional line items, up to 20 rows, pulling from other sales orders.
My question is: How can I filter so that all the line items are shown from the new sales order and only from that sales order?
I have my trigger which is set off when a new item is created in my CUSTOMER_ORDER table. The flow then moves to the next step which is Executing a SQL Query. The table the query is being run against is CUST_ORDER_LINE (line items of the sales order/CUSTOMER_ORDER).
There is no Filter Query and Top Count is set to 20. I'm assuming the Filter Query being blank is my issue but I'm not sure how to filter to only show the newest sales order. The sales order number is held in the CUSTOMER_ORDER table. Is there a way to join the CUSTOMER_ORDER table with the CUST_ORDER_LINE table? Or maybe there is a better way to capture the sales order ID that caused the trigger to ensure it's the record that's used in the query results? Right now I'm just sorting the sales orders descending which I would think leaves the door open for a risk of two people saving sales order simultaneously and both triggering an email for the same order instead of the one they saved.
New to Power Automate and Power Apps. Please let me know if there's additional details that would be helpful.



