Using new id on current page

I have a page with a form which can be used either to insert a new record or edit an existing one. If the button displaying the page passes an URL parameter with a record id, the form is populated with the relevant data and a button to update the record is displayed. If no URL parameter is passed, a blank form is displayed, and there is a button which inserts the new record. This all works as I want.

I’ve now added several modals which display records from related tables with buttons to insert pairs of ids into a join table - there are a series of many:many relationships between the main record and several other tables. This also works fine, but with one problem. Obviously I can’t add the related records until the main record has been created and given an id. I would like the button which inserts the new record to then get the id of the newly-created record and use it an URL parameter - in this way the insert record form will become an edit form and the user can add the related records.

I’m sorry for the slightly complicated descriptions. Basically, I just want to access the identity value in App Connect (to use in the URL parameter). Below is the Server Connect setup. It all works, except the matter of passing the identity value back to the page. As you can see, I’ve tried with a variable and a session. Using a PHP session, I can see the correct id is put into the session variable, but it’s not available to select from within App Connect as far as I can see. I’ve also tried adding a repeat after the insert as this seems to be necessary sometimes to access variables, but it didn’t help. I would be grateful for any suggestions.

image

Solution is on Wappler YouTube channel.

just use the identity returned but the insert query

2 Likes

Thanks @Hyperbytes, that was very helpful. I thought I was looking for the id variable in the right place, and your video confirmed it. However, I think the difficulty I was having was that the variable didn’t appear.

I think this is a bug - I expect the issue is that that I used the Set Value within a Condition. I entered the variable name manually and it worked fine. As you can see, it’s not available to select however.

image

I think I’ve mentioned a similar issue before where variables are set within repeats.

Did you remember to check the insert query output box?

Hi Tom,

The problem in your case is that you are using a combined server action for both insert and update record.
As the output of such action is conditional, Wapplers data pickers can’t know which output you need, so they can not populate the results for you.

This is also not the best design, we advice to make separate server actions per functionality. Avoid too much conditionals as those can be confusing and make things only complicated. Just make a separate insert and update actions - just doing one thing as they should.

1 Like

Yes… but I see that’s not the problem… Thanks.

Hi George,

The reason that I created a single server action is that I’m using a single server connect form. Surely Wappler would know that I only want the output from the branch of the condition which runs? (If it doesn’t, I would have thought this was rather a serious problem).

I certainly wouldn’t want to create separate pages for the insert and update forms (and a third for duplicating a record). The page is quite complicated and links 7 tables. It’s 600 lines long. It would be very inefficient and require much more work and maintenance to create multiple pages - particularly when a single page works fine now I’ve got the variable working.

I think there are numerous situations where I would want to access variables created within different conditions. As far as I can see Wappler handles this fine - it just doesn’t make the variables available in App Connect, so they have to entered manually. Are you suggesting that using this approach could be unreliable?

I would appreciate your advice - for an alternative approach to the current issue and also about using variables and conditions generally, and limitations to be aware of. Thanks.

It’s not that the method is unreliable.

What George is saying is that the UI, at the moment of binding data on the page, does not know which of the conditions values will be returned when you run the condition in your browser later.
I.e. the problem is that the UI does not know what is the result of the server action until it runs, and in order to run you need to input the data, which only happens when you run the server action in the browser and enter the data needed.

You can always manually enter these values and they will work on the page.

I see. That makes sense. Perhaps all variables could appear under Data in App Connect - it would be up to the developer to know whether or not they would actually be defined. But I’m quite happy to add the values manually now I understand the problem.

I’m not sure why this is not the best design - but as long as I know there’s not some issue as far as Wappler is concerned, that’s fine. I appreciate that using conditionals (case, if, else etc.) can be confusing but having used them many thousands of times over the past few years, I would pretty lost developing without them.

1 Like

One day i hope to see INSERT ON DUPLICATE KEY UPDATE ......... come to Wappler but i don’t see it a priority personally

1 Like

I would like this too. I’ve created the same functionality using a conditional, but it’s slower.

I would also like an option to use LOAD DATA INFILE for importing large numbers of records. It’s hugely faster than multiple inserts, and can insert or update. I’ll still use PHP for this.