Sunday, April 22, 2012

Saving Master Before Viewing Detail

The Issue

Master Detail forms are an excellent way to manage a one to many relationship between tables. When using the declarative master detail form, clicking the edit link for a detail record does not save any changes that have been made to the master record. That is because clicking the edit link does not submit the page. A slight modification to the edit link can easily provide that feature. This is also useful for anytime you want to save data and then navigate to another page based on an ID.

This idea is not unique to me, in fact it is completely inspired by the changes made to the APEX builder. When on the Report Attributes tab any changes made to the report definition will be saved when clicking the edit link for a column.

Clicking a Link <> Submit

Before I go any further I want to point out one thing that I think a lot of newer APEX developers have trouble with. Session state for items on a form will not be updated unless the form is submitted. Entering data in a field and then clicking a link to another part of the application is the exact same as entering data and then clicking a link that takes you to another website. There is no instruction for APEX to save the values of the form items.

The Solution

To get started I created a master detail form based on the all too familiar dept and emp tables. Dept being the master and emp being the detail. For this example I had to choose to edit details on a separate page rather than in a tabular form.

Create Detail ID Item
Instead of linking to the detail page we are going to set the value of an item with the ID of the detail record and then submit the page. For this example my item is called P5_EMPNO. Set the source to always replacing, the source type to static, and leave the source value blank. This will ensure that this item is always nulled out to prevent unwanted branching.

Create a Page Branch to the Detail Page
The page branch will redirect to the detail page conditionally if the value P5_EMPNO is not null. Additionally it will set the detail page key(P6_EMPNO) to P5_EMPNO.

Edit the Detail Link to Set the Detail ID and Submit the Page
Rather than link directly to the detail page the detail link is going to save the primary key for the employee into P5_EMPNO and then submit the page with a SAVE request. Change the link target to URL and add the call to apex.submit. This will cause the master record information to be updated and then branch to the detail page. In my case the edit link now looks like this.


Now any changes to the master record will be updated in addition to linking to the detail page. Take a look at the live demo to see how it all pieces together and check back later because I will be rolling out another post which will cover the other features of apex.submit.

No comments:

Post a Comment