Export to csv only if query is not empty

I have a server action, and it contains 2 steps:

the export CSV file get’s it data from the databasse query.

Now I wonder, is it possible to export to a csv file only when the query has results (= not empty)?

You could use a condition step with the count formater and then > 0

After that put the export csv in the THEN step

1 Like

Hi Duh,
I tried it but it doesn’t work. I probably did something wrong…?
image

Your expression is wrong. You need to use the count formatter in the condition.

Ah! Found that, changed it and it works!

But…
When I open this server action in a browser I get this error message:
{“code”:0,“file”:“C:\xampp71\htdocs\dmxConnectLib\modules\export.php”,“line”:33,“message”:“Data is empty.”,“trace”:"#0 C:\xampp71\htdocs\dmxConnectLib\lib\App.php(195): modules\export->csv(Object(stdClass), ‘csvExport’, NULL)\n#1 C:\xampp71\htdocs\dmxConnectLib\lib\App.php(127): lib\App->execSteps(Object(stdClass))\n#2 C:\xampp71\htdocs\dmxConnectLib\modules\core.php(91): lib\App->exec(Object(stdClass), true)\n#3 C:\xampp71\htdocs\dmxConnectLib\lib\App.php(195): modules\core->condition(Object(stdClass), ‘’, NULL)\n#4 C:\xampp71\htdocs\dmxConnectLib\lib\App.php(159): lib\App->execSteps(Object(stdClass))\n#5 C:\xampp71\htdocs\dmxConnectLib\lib\App.php(127): lib\App->execSteps(Array)\n#6 C:\xampp71\htdocs\dmxConnectLib\lib\App.php(116): lib\App->exec(Object(stdClass), false)\n#7 C:\xampp71\htdocs\dmxConnectLib\lib\App.php(94): lib\App->exec(Object(stdClass))\n#8 C:\xampp71\htdocs\dmxConnect\api\_cron_export_users_hm1.php(8): lib\App->define(Object(stdClass))\n#9 {main}"}

This is probably because the action is executed anyway before the condition?

The action cannot be executed before the condition. What’s the exact expression you are using in the condition?

Click the wand to the right

Right click and navigate to count


then a operation

in code view looks like this
Screenshot 2022-01-26 at 21.45.42

1 Like

Alright. Somehow my code looked like this:
export_users1w.count() + ‘> 0’

Works great now!
Thank you all for your help :slight_smile:

BUt I still don’t understand why the database query is not executed before the condition…
Because it is there before the condition right?

image

The steps are executed in the order they are added in the server action. First the query, then the condition, then the export.

So now the query gets executed twice right?

You have the output off which means it stays to the action, you need to have the output on to see the results in the browsers XHR and in your pages

You can just disable the output option for the query, it’s not needed. You only need the output if you want to pass the data to the front end/page.

1 Like

Yes I understand. I did already.
But just for my understanding how Wappler works:

image

First it executes the query (for no reason really here)
Then it sees the condition and executes the query again (for the second time) and counts the number of items the query returns and if it is more than zero it continues to the next step (exporting the results)

Is this right?

No, it executes the query once on the first step only. The second step uses whatever is returned from the first one.

1 Like

Thank you Teodor, that is what I was unsure about.
Happy to hear this. Now I understand the logic.