Help with data insert

I am trying to insert data into three tables from one form, two of which are working fine, the 3rd, however I can not get the data to format correctly, even after scouring these forums and trying every combination of linking/formatting, etc., regardless of what I try = /.

My form is made up of three parts: initial section with a few text inputs; then a form repeat below it with two select inputs plus an add button; then a sub/nested(?) form within that first repeat containing two more inputs and another add / delete button. Upon submit I currently have a server action that has a single db insert containing the text fields from the first section, then below it a Repeat which contains a second DB insert for which the expression references the array in POST array and references the unique ID from the single db insert, then a second Repeat within the first one doing essentially the same thing, referencing the second POST array and grabbing the unique ID from its respective index from the first Repeat.

Like I said, the data from the first repeat form/server action inserts just as expected with each value within its correct column, and referencing the correct info the main table, but from the second repeat the data comes in as an array that I can not reformat so that the data goes to its intended place. The value it is returning looks like this:
{“percentTotal”:"",“parentIndex”:[“0”,“1”],“user”:[“1”,“3”],“percentage”:[".70",".60"]},

for which I am trying to convert to a normal row/column format, but cant figure out how to either split these apart correctly, or send them to the server action in a different format altogether.

The thing that I think is causing the main issue is that the sub-repeat action is running through the entire array of all of the sub repeats every time it runs the first one instead of just the ones that correspond with the first repeat, but I cant figure out how to control this.

I feel like I am making the solution more complicated than it needs to be, however, I am not having any luck figuring out what that needed solution is =/. If screen shots would be helpful, or more information is needed let me know and I will include, but I think for someone with even a little bit of experience the this is probably enough info to point out where I may be stuck.

Yes Screen shot are always the best way to troubleshoot. My question is tho, why do you need a repeat if you are inserting in 3 different tables? Did you try inserting the data without using a nested repeat step?

Well, now that you say that, I’m actually not sure that I do need a repeat. I think I had it in my head by doing that it would sort of “auto reference” the two corresponding tables, i.e. repeat runs and inserts its row into the table then outputs its ID into the repeat for the sub repeat items to use for FK, then circle back around and do the same thing for the remainder of the items - as a repeat implies lol. As Im typing this out though, now you do have me questioning if this is a wasted step, and if the sub items can get the correct ID differently, which I’m sort of thinking they can. I will still post some screenshots in just a minute following this post so that you have a better idea as to what I am working with here, but Im also going to see if I can achieve this same thing without the repeat altogether - if that works I will let you know!

This is probably more than needed (sorry lol), but its all what I thought might be relevant. Let me know if there is anything I missed that would be helpful too…

comm 3

Going out on a limb here with this old trick I learned. I totally did not test this out real time due to the fact that I did not have time to replicate your form setup on the frontend but however on the backend I could easy create a simple static replication of your backend setup.

I suggest duplication your current Server Action then give this setup a try. Whole goal here is to try and catch the second insert identity while try to avoid 3 nested repeats hence the “While loop”
image-1


image-2

image-3

image-4

image-5

image-6

image-7

1 Like

Wow that was very thorough - thanks a ton! I will give that a try here in a few and see if that nets any success; reading through it it makes sense how it would work, but well see if the reality is so lol.

Also, when doing some reading last night I found some other suggestions to similar issues saying that the input field needs to have a dynamic attribute Name formatted like this (obviously replaced with the relevent variables): record[{{$index}}][product_id. However, each PUT variable already has this format under linked field, so im unsure if i still need to do that, or if maybe there has been a software update since that post was written and this already being present in the linked field means I it now does this automatically. Do you happen to have any insight on that?

1 Like