Need help with Multiple UPDATE and INSERT Server Action Steps

I have two DB tables which when queried result in this

image

Which displays like this on a page

The data for the two lines “(3) Lease Condition” and “(5) Diminution in Value” come from this “Fees” table

If the user then makes a change to “(3) Lease Condition” and adds data to “(8) Post-Repair” like this

I then want a Server Action to process the form, UPDATE the “Fees” table when there is a change and INSERT into the “Fees” table when there is new data, and do nothing with the lines where nothing is entered.

I am really struggling to get my head around how to plan out the Server Action Steps.

I know it’s a case of submitting the form
then the Server Action will have
DB Connection
Condition
Then
UPDATE
Else
INSERT

BUT I can’t work out the detail with the repeats etc.
Can someone help? An example would be brilliant.

This is where I got up to with my Server Action

image

And this is the resultant $_POST variable list that is created automatically. Is it right that the variables should be repeated? One set under $_POST and one set under $_POST.record ?

Should this work?
Am I doing it wrong?

Does anybody have any positive and useful suggestions?

Hi Neil,

So if I understood correctly, your first condition should be a query for the existing table data, then the next step should be to check the values of the form against the queried data, if the data is not an exact match then proceed to another step whereby the data is either updated or inserted into a new record.

Does that sound like what you’re trying to do?

Hi @max_gb, I see your explanation as a bit complex especially comparing the multiple query of the existing table and the multiple records of the form submission, and then deciding whether it’s an update or insert. Or maybe I misunderstood you.

What I think I have is…

3 x records submitted by the form
In the Server Action there is a repeat which cycles through the 3 records

  • On each pass of a record there is a Condition which checks for the presence of a fee_id
  • If there is a fee_id then UPDATE that existing record
  • If not then INSERT the new record

To me that is logical and simple. But it doesn’t work. Not a good few days for a whole number of reasons. Hopefully tomorrow I can tackle this with a clear head :smile:

Hi Neil,

To help you, I need to know the answers to the following questions:

  1. In this table:


    The number of reports (the leftmost part of the table with id from 1 to 9) is static, or do they change dynamically (there may be more or less of them)?

  2. Show what your form looks like, which you use to update / insert records in the “Fees” table.

First of all I would check to make sure that you are receiving data server-side from your array. I assume this is called record. From your screenshot it looks ok.

Just an idea, for the fee_id check, could you set the fee as fee_id.default(0) on the front end so that the fee is stored as 0 by default. You could then run a condition step server side that if the fee_id is > 0 then proceed to UPDATE.

@max_gb As your message came through I was looking at fee_id has items or fee_id is not blank but your suggestion is better.
That has helped a bit. Closer than I was yesterday.

Hi @Mr.Rubi I have been playing this morning and getting closer but not there. I will let you have the info you asked for in a few minutes when I have done screenshots etc.
Cheers

Hi @Mr.Rubi hopefully this is what you were looking for

  1. The list is dynamic, comes from a DB table and can change
  2. The Server Action

    This is what is supposed to happen:
    In the Server Action there is a repeat which cycles through all the records
  • On each pass of a record there is a Condition which checks for the presence of a fee_id $_POST.fee_id > 0
  • If there is a legitimate fee_id then UPDATE that existing record
  • If not then INSERT the new record but only if there is an invoice number greater than 0
  1. Record Update

  2. Record Insert

  3. Result if
    1 existing record changed and 2 new records added


    (1) existing record not updated
    (2) 3x new records are inserted but without any data

QUESTION:
Should I be adding .toNumber() to all of these numbered fields, and if so, should I be adding them in the UPDATE and INSERT queries?

Hi @Teodor, sorry to call you in on this one but can you help?

So Neil, you have a very interesting task.

If someone knows the solution, I will also be grateful, because I also reached a dead end.

I have encountered the following problems:
1.I am Not sure that the data in the array record has the correct structure. How did you create this array? What kind of data does it contain? Can you output data using set value and see how it is returned in the browser?

2.to correctly update/insert records using the repeat in server actions, you must configure the Output fields field in the repeat properties. And at the step of updating/inserting the record, take data from there, and not from the General POST data (here you have an error). However, the problem is that I don’t know how to make an array that could have these fields set as the source in the repeat. When the data source does not have a complex structure (an array of a single field with comma-separated data), {{$value}} is usually just used. But in your case, the data in the repeat should be structured by fields. This means you must be able to configure Output fields in the repeat.

Hi Neil!

I’ve solved your problem. It was informative and useful for me.

You can see how it works here: http://howitisdone.info/arrayinsc.html

Now the solution itself.

First of all, I would recommend that you do a little optimization. This will give you a lot of advantages. The steps will be easier, the logic will be simpler, and it will be much easier for you, and there will be fewer potential errors. The optimization is that you create a record in the "Fees" table at the same time as you create a record in the first table (let it be the "Report" table). in Other words, this is done in a single server action:

  • first created an entry in the "Report" table;
  • then created a record in the "Fees" table and linked it to a record from the "Report" table (you can leave the field values empty).

This optimization will remove unnecessary logic from your step, which you are discussing here. You don’t have to create a condition where you choose to insert/update a record. You will just always update the entries, because they are already created.

I want to emphasize that this optimization is not mandatory. You can work with complex logic. But optimization will give advantages.

Now to the key questions. Namely, the correct transfer of the array from the form to the server action.

To begin with, let me explain about the record array that you use in your examples. This array is created automatically whenever a repeat is used in a server action. You can use it in General, but I will create a separate array so that you and other users can better understand the logic of interaction.

In the server action, the first step is to create an array (I called it rec) and create variables inside it with the same name as the inputs in our form:

Now I will add an optional action. However, it will help you very much in diagnosing whether everything is going well. In the server action, insert the Set Value step, which will be an array with the data source from the previously created rec array. Its task is to return the correct array to us. The settings look like this:

Now let’s turn to our form and its inputs. The names of inputs must match the names of variables in the rec array in the server action that we created earlier:
2

And most importantly. Each input must have an additional name enabled via a dynamic attribute with the following value:
3

As you can see, the expression contains the name of the array (it must match the name in the server action), followed by the dynamic index, and then the name of the input.

Great. Let’s check the work. If everything is done correctly, we will get the correct array from our server action:

If you have achieved this, then 90% of the work is done. It remains to add a repeat to the server action with the record update action. And since the data is now coming in correctly, we can configure Output Fields in the repeat:

Now in the update action we can select the correct data from the repeat:

Done! :slightly_smiling_face:

7 Likes

Wow! Great work.
I was just playing around with things but will read your post very carefully and come back to you. Many thanks for taking the time to look at this.

@Mr.Rubi that has helps a great deal. You have helped me to understand more about what I am trying to do and the tools I am using.
At least now I am getting some results which I was not before. Not quite the there yet, but at least I know what to do.
Thank you

1 Like