Conditional Insert (How To)

I have a situation here where I have two tables. (Only showing relevant columns in examples.)

Client Details: 'Current Job Coach Name' (text Value) - Update
Job Coach History: 'Job Coach Name' - Conditional Insert

The Client Details table should always contain the most recent job coach entered.
The History Table is a running record of all job coaches the client has had.

So on my form I would like to update the job coach name in table 1 and add a new record to table two. That part is easy.

What I need to do is only insert a record into the history table if it is different than the Client Details table. As it is, everytime the client details form is edited it also creates an unnecessary record in the history table. I need the insert to only insert if the job coach value is changed.

I hope I explained that well. Thanks for any help you can offer. Much appreciated.

Hello @brad what about a single query where you can check if the job coach value is the same or not?

Then the condition should be something like: $_POST.JobCoachFromClientSide!=singlequery[0].JobCoachFromHistoryTableValue
Then-> insert
Else -> nothing

1 Like

Thanks @franse ,

Good idea on the single query. I never thought of that. The problem I would have is writing the condition. As far as I know the expression builder is not available in the server connect action editor. And I'm not very knowledgable in coding.

But your example condition does make sense. Thank you so much. I am sure I will have more questions as I work this out.

:clinking_beer_mugs:

1 Like
  1. Right click -> Operation

  2. Then select != (red) and the value to compare (green)

I don't know how is your db, but you can also check with count():

Imagine:

  1. You have this coming from the client side:

  2. Make a query to check if record is exactly the same:

  3. Use the condition to check if query is equal or greater than 1 with query.count()>=1:

The count method confuses me a little. But I think your screenshots may have pointed me in the right direction.

I may have to have a couple beers to process this. Again thanks so much for your time. You have been very helpful.

1 Like

When you have a query and this condition query.count()>=1: then the condition will be true only if the query returns at least one record.
In this case, if no match (0 results) then it will return false

Anytime, have a nice weekend :clinking_beer_mugs:

1 Like

Ah, I think I get you on the count. However that will bring up any matching records. A client could have coach A for a few months then change to coach B and then change back to coach A.

I think your initial suggestion will work. I am working on that now. Your screenshots are a huge help. Thank you so much. I am sure I will be marking that as the solution shortly.

Your first suggestion worked like a charm! It's amazing. I love Wappler.

I owe you big time, @franse ! Thank you!

1 Like