Checking to see if record already exists to decide whether to update or insert records

I need to check and see if a record already exists in a table in a conditional server action to decide whether to perform a database update or datatbase insert action. Can’t figure out the best syntax. Any advice?

Thanks,
Rick

See https://docs.wappler.io/t/check-if-a-database-record-already-exists-before-inserting-a-new-entry/2847

You will have to start with a table query to look for the value

Use a server connect conditional based on {{query1.length}} (not in data picker, needs to be added manually, returns false or number of records)

True leg will be update query (record present)

else will be insert query (value not present)

1 Like

thank you.

Just putting {{query1}} in the IF part of condition will work too.

1 Like

Has issues with that @nshkrsh which is why I recommend adding .length, more reliable

Oh. Haven’t encountered yet.
Any example of where it didn’t work for you?

See this thread.

The issue in this topic is related to count. Here, we just need to check if anything is returned or not… which should work fine with {{query1}} in the condition.

1 Like

No, if you read the thread fully you will see my comment

“Don’t actually need the number, just if any returned or none so i can redirect on empty query”

I was using count to try and get around the {{query1}} issue.The final solution was to use {{query1.length}} as recommended by Teodor

Oh yes. Thats correct.

@Teodor can you please shed some light on why {{query1}} would fail in the if condition?
I haven’t seen it yet, but Brian has experienced this issue. I would update my code too if {{query1.length}} is the way to go.

I don’t think there is an issue with the condition checking {{query}} vs {{query.length}}
The condition with an expression {{query}} works perfectly fine.

No results returned from the query:

Results returned from the query:

You can see the ELSE step is running perfectly fine when there are no results, setting the results to the value i’ve added there 0

I think Brian’s problem was somewhere else.

1 Like