Loop an SQL query on each identifier in the JSON array

Hello,

I am trying to build my first workflow. The final purpose is to trigger a clound function using and API but I need to get some data before into a MySQL database. I need for this to execute a SQL query for each value from an array. I want to do this in Python but I cannot trigger my SQL query and send dynamic parameter. Do you have any why ?
Here is a picture of my workflow.

Hello @AntoineOA and welcome back!

For your workflow, what you are trying to do should be handled as a function call. On the left side of your canvas you should see the function icon where you can setup the MySQL query:

In this space, you can create the SQL query you want to execute in your loop and then call that in your python code. Additionally, there is no need to use the trigger() syntax for this -- functions are called like functionName(param1, param2...)

I have included two ways of calling the loop -- first using the code block you are already using but changing the syntax and second using the Loop block component, which takes an array input and uses the alias value to refer to each iterated item.

Let me know if you want any clarification on the above!

Hello @pyrrho . Thank you for your answer.

I've made the same flow as yours but I still get errors when I run it. As I show in the first image, I need to loop over each ID nested in the JSON sample currently defined in the startTrigger.

{
  "env": "ABC-implem",
  "env_type": "IMPLEM",
  "rules": [
    {
      "id": 179,
      "rule_name": "MY TEST 1",
      "rule_type": "REF_FLIGHT_SQL"
    },
    {
      "id": 22,
      "rule_name": "MY TEST 2 ",
      "rule_type": "REF_FLIGHT_SQL"
    }
  ]
}

Here is a new screenshoot from what I get. (I also tried with {{value.id}}

I would had a JS query just after the start trigger, call it "extractRules" to transform your data and prep it for the loop.

Something as simple as return startTrigger.data.rules

Then reference that in your loop as the loop input.

Have a new SQL resource in your Loop Runner (not a function).

Then you SQL will look something like this:

SELECT rowID, json
FROM product_json_rules
WHERE {{ value.id }} 

There are probably pros and cons to this which someone more knowledgeable could chime in on.

2 Likes

As @ferret141 aptly describes, you just need to extract the data from the startTrigger and use that in your loop functions/blocks. All of these methods should work out once you are running through only the data you need to loop.

Thank you both ! It works :smiley:

2 Likes

Hello, I'm facing another problem :sweat_smile:

I can run my worflow when I have some data into the Test Json parameters but the final objective is to send this data from an app. So I trigger the workflow from the app using the native process on retool. When I check the log I have the data in the startTriggers but not in the javascript block behind. I tried with .data or .data.value or .value but nothing works.
Do you have any why ?


Hey @AntoineOA!

Glad you're making some progress, at least :slight_smile:

It looks like your code block is still just using the default values from the workflow setup. Can you post a screenshot of the code1 block?

What you should do in that block is test a return value, so if you got rid of all the stuff in that block and replaced it with return startTrigger.data you should see the same set of data from your App in both the startTrigger node and the code1 node outputs.


here is the code1 block.

If you trigger from the app, come back to the workflow and check the run history for the last run, what does the node data say (don't forget to deploy the workflow)

1 Like

The workflow was not deploy. I'm stupid sorry :dotted_line_face:
It works fine now.

Not stupid! Asking questions and learning.

If you ever need any help, Retool forums are a generally safe place to just jump in (especially if you are clearly putting in the work to understand)

1 Like

You're not alone.

I'm amazed I'm not bald yet. The number of time I've pulled my hair out only to realise I didn't deploy are too many :slight_smile: