In the previous post I showed some options how you can represent a many-to-many table relationship in a report using the LISTAGG Oracle function.
In this post we will edit a record and see how we can represent the data in a Form and save the data back to the different tables.
First I create a Form on the main table (customers) by just following the wizards.
Next I'll add a Shuttle item to the page: P2_PRODUCT_IDS
Finally for the item source value we can't use a Database Column as the data is in a different table, so we enter the select statement to get all the product ids for that customer. Note that the Source Type needs to be set to SQL Query (return colon separated value), so it returns 1 or more product ids.
The selected products will be shown on the right in the shuttle.
Here's how the Form looks like when we select John Dulles who's interested in two products (Jacket, Business Shirt):
When we move Products from left to right and the other way and hit Apply Changes we need to store those values.
Add a new Process after the build-in Process and call it Save Products with this code:
We first store the selected products in an array (l_vc_arr2) which apex_util.string_to_table is doing.
Next we delete all (possible) records that are not selected. You could remove the last line in the where clause so all products for that customer are deleted (if you add all the selected ones later again), but if you're auditing that table your info in not correct as that person might not have actually deleted it.
I added some debug info in the process too.
Finally we loop through the array and check if the record already exists in our table, if it doesn't we add it. Again here you could not do the lookup if you are dropping all records in the delete statement and just add all selected again.
I typically have a condition on this request to not run when the request is Delete.
In the online example (click on the edit icon in the report) I dropped the Create and Delete buttons in the Form, but if you keep them and want everything to work, there're two more things you have to do:
-) For the Create - in the "Automatic Row Processing (DML)" Process (of the Customer table) you need to specify P2_ID in "Return Key Into Item" field so the next process (the one you see above) has a value for P2_ID (= customer id).
-) For the Delete - you need to add another process before the "Automatic Row Processing (DML)", so the child records get deleted first, before the automatic row process deletes the customer.
In the next post I'll give an example of working with this data in a Master-Detail form.