Application Express: Anonymous PL/SQL Block and Bind Variables - plsql

I'm having an issue binding the value of a page item to a declared variable in an anonymous PL/SQL block process.
The problem is that the page item (:P4550_REQUESTOR) is not populated with a value until a conditional is met. It appears that the PL/SQL block process is binding the variable to an empty value as soon as the page is loaded, despite the fact that the process does not fire until a specific button has been clicked.
Here is my code:
DECLARE
v_email_to app_user.email%type;
v_requestor VARCHAR2(15);
BEGIN
v_requestor := :P4550_REQUESTOR;
BEGIN
SELECT email INTO v_email_to
FROM app_user
WHERE userid = v_requestor;
END;
SEND_APEX_MAIL (
v_email_to,
'Your vacancy request has been rejected.'
|| chr(10)
|| 'Emailed to: ' || v_email_to
|| chr(10)
|| 'Requestor: ' || v_requestor,
'Vacancy Request Rejected'
);
END;
Does anyone have any thoughts on this?
The block works just fine if I hard code a value to v_requestor. If I try to get the value of P4550_REQUESTOR after the page has loaded, it is empty. After clicking the edit button, P4550_REQUESTOR is populated.
** **MORE DETAIL** **
P4550_REQUESTOR is a page item that resides within the Vacancy Request region which is only displayed when a conditional is met. Specifically, the conditional is an edit button associated with a table row that is created on page load. Clicking the edit button causes the details region to display, and the associated page items to be populated.
The page item values in the Vacancy Request region are populated via an Automated Row Fetch which fires After Header.
P4550_REQUESTOR has a Source Type of DB Column.
The process that fires the code above is set to fire On Submit - After Computations and Validations
If I log the value of P4550_REQUESTOR when the page loads, it shows null. If I log the value after clicking the edit button, I get the expected string value.

Process Flow Control in Oracle APEX
(This is actually useful to think about in other programming disciplines and environments.)
Problem Defined
The problem is that the page item (:P4550_REQUESTOR) is not populated with a value until a conditional is met. It appears that the PL/SQL block process is binding the variable to an empty value as soon as the page is loaded, despite the fact that the process does not fire until a specific button has been clicked.
The problem statement reworded in Apex terminology and presented in the form of an actual question:
There is a REPORT REGION on the page which contains the result of a direct reference to a data table/view. This report is managed by an Apex process called "Automated Fetch" and is initiated automatically by the loading of the page headers.
There is a FORM ITEM on a page which which is populated conditionally by a BUTTON ITEM selection made by the user. The BUTTON ITEM is part of the report results.
There are multiple button items. Each is associated with a value for each report record.
If the user does not select the BUTTON ITEM from the REPORT REGION, the FORM ITEM remains unassigned and contains a "null" value.
There is a defined PL/SQL block of code which is set to execute when a SUBMIT BUTTON item is pressed (also on the same page). Why does my code block (defined page process) run with a null value when it is triggered without first pressing a BUTTON ITEM from the REPORT REGION first?
Event Driven Program Design for Procedural Programmers
The answer is not obvious if you think under the paradigm of a procedural language. Without diving into a lecture on the topic, here's a visual layout of the problem space of the OP that I cooked up to illustrate how the problem can be made more obvious:
This is my Apex page design in implementation. It's generic enough to use as a template for other Apex designs. There are no flow arrows on this diagram because it's a stateful system. One thing causes another thing to happen and so on... but not always and not all at the same time.
Use Cases for Apex UI Page Designs
Try walking through a few use cases to understand how the elements broken down in the diagram operate together. Each user may take any number of click combinations and interactions, but there is a commonality:
They all enter the same initialized conditions on page load.
They all leave the page by: navigating elsewhere or through the SUBMIT button event.
Use Case #1
User chooses {MyPage:SQLReport:ThisButton} from one of the records in {MyPage:SQLReport}
According to {MyPage:SQLReport:ThisButton} #3, the value associated between the report record and the button item is passed to: {MyPage:HTML-Region:ThisItem}
The form item state has been updated and changed from the initial null value.
User selects {MyPage:HTML-Region:ThisSubmit} button to inform the system to continue on.
The submit button executes the defined PL/SQL procedure block: {MyPage:RunCodeBlock}
Use Case #2
User enters page and reviews results displayed in the {MyPage:SQLReport} region.
User decides no additional input is necessary and then selects the {MyPage:HTML-Region:ThisSubmit} button to inform the system to continue on.
(a note: the state of form item {MyPage:HTML-Region:ThisItem} has not been changed from the initial null value at this point... after the submit button has been selected)
The submit button executes the defined PL/SQL procedure block: {MyPage:RunCodeBlock}
Use Case #3
User chooses {MyPage:SQLReport:ThisButton} from one of the records in {MyPage:SQLReport}
According to {MyPage:SQLReport:ThisButton} #3, the value associated between the report record and the button item is passed to: {MyPage:HTML-Region:ThisItem}
The form item state has been updated and changed from the initial null value.
User chooses {MyPage:SQLReport:ThisButton} from a different selection from one of the records in {MyPage:SQLReport}.
According to {MyPage:SQLReport:ThisButton} #3, the value associated between the report record and the button item is passed to: {MyPage:HTML-Region:ThisItem}
The form item state has been updated and changed from the initial value stored in step (2).
User selects {MyPage:HTML-Region:ThisSubmit} button to inform the system to continue on.
The submit button executes the defined PL/SQL procedure block: {MyPage:RunCodeBlock}
The difference between each case should illustrate why the dependent value (ThisItem, or more specifically, page item P4550_REQUESTOR) is null in one use case vs. the other.
Building a Physical Implementation (An Apex Page)
The table I used is called STAR_EMPS. It is similar to the EMP table but has only three columns: ename, deptno and salary. Although it is not super important, this is the data set I used to populate STAR_EMPS:
I used a simple two-column table named STAR_EMPS_LOG for capturing the output of a successfully executed procedure call. You could accomplish the same with just one column, but I wanted a sequential id for tracking the order each event was recorded- for running multiple test cases. The procedure is one of several defined processes kept on this page:
contained in: {MyPage:RunCodeBlock} is below:
DECLARE
-- output from this procedure will be recorded in the star_emps_log
-- table. {MyPage:RunCodeBlock}
mycelebrity star_emps.ename%TYPE:= :P17_CELEBRITY_NAME;
mylogmessage star_emps_log.log_message%TYPE;
BEGIN
-- Conditional; changes message based on the value set for the
-- page item.
if mycelebrity is null then
mylogmessage:= 'No button was pressed on the previous page.';
else
mylogmessage:= 'The user selected: ' || mycelebrity ||
' from the report list.';
end if;
-- populate value from the page item.
INSERT INTO star_emps_log (log_message)
VALUES (mylogmessage);
commit;
END;
This is how the page layout was set up:
As in your example, I made a {MyPage:SQLReport} region with its supporting elements. The SQL Report represents a query directed at the source data table.
{MyPage:Form} has been renamed to {MyPage:HTML-Region}.
{MyPage:SQLReport} is defined by a SQL query, there is also a mock column to use as a place holder for placement of the "edit" buttons.
{MyPage:SQLReport:ThisButton} The button specifications are detailed through this:
The TWO Page processes: PROCESS and BRANCH need to be linked with the same settings referencing a BUTTON triggering Item.
User Interface Test Cases
Run through the three suggested scenarios to get started. Verify that the system is interpreting the requests correctly. This is what the page layout looks like:
The two processes on the system have a definition that wasn't mentioned in previous discussions may solve our original problem at hand:
Some Parting Thoughts
It is a good thing this turns out to be a trivial case once broken down. The diagramming method described here should scale to other Apex applications of varying complexity. There is considerable utility in stepping away from the code, locking down on terminology and trying to describe systems and processes without actual code. Please be sure to share any stories if this approach helps with your own Oracle Apex design challenges.
Onward!

The original, verbose answer seems to way overcomplicate the issue. The session state concepts manual covers this behaviour more succinctly.
Should P4550_REQUESTOR be a normal item created from a wizard, using :P4550_REQUESTOR will return a value in processes running post submit because the submit processes moves values in browser to session state.
If P4550_REQUESTOR is rendered conditionally, then it will always be null and I'm not sure what would happen if you tried to set it - probably depends how.
On a similar note, if you used &P4550_REQUESTOR. to parameterise the process, you would face the behaviour originally described (and made the code less secure)

Related

ASP.NET controls disappearing from dynamically built user control

I have a curious problem with an ASP.NET user-control which is dynamically created in VB.NET. Sorry that this is a long post, but I think it’s better for me to explain carefully what I’m doing and what the problem is.
Essentially, the user-control is an HTML table used to display data from a back-end SQL Server DB. It uses a stored procedure to return a DataTable, from which it builds html rows and columns with the values stored in the InnerHtml property of the relevant table cell. This is just a passive display of the data and works perfectly.
However, the user-control also has the facility to insert a new record, triggered by an ASP button. This causes the following sequence of events;
Post-back
The user-control_Init event rebuilds all the existing rows in the table and inserts their values as described above.
The page_Init event.
The page_Load event.
The user-control_Load event.
The button_Click event then builds a new row with the same structure as the main table. But this time it inserts into each table cell a relevant ASP.NET control, such as a textbox or checkbox, holding “null” values. Each control has its ID set explicitly to a unique value. Finally, an “insert flag” is set to true.
The user then enters the data into these controls and clicks an Insert-Save button. This causes another post-back, which follows the procedure described above, except that the “insert flag” tells the user-control_Init event (2) to rebuild the insert row. Then, before the page_Load event (4) the values previously entered by the user are reinstated in the ASP.NET controls from ViewState. Finally, the Insert-Save_Click event calls a stored procedure to save these data to the DB.
All this work perfectly when there are existing rows in the html table. But – and this is the strange behaviour – it fails when there are no existing rows. The first post-back completes successfully although, obviously, event 2 skips over the rebuilding of the existing rows because there are none. However, the insert row is rebuilt correctly and the user can then enter data into the ASP.NET controls. During the second post-back (Insert-Save), the first three events described above work perfectly. But, the moment the code hits the page_Load event (4), all the ASP.NET controls disappear. The html row and cells are still present, including the literal control (index 0) in each cell. No error is thrown at this stage, but when the Insert-Save_Click event occurs and the data should be read from the ASP.NET controls – they are not available and an "index out-of-bounds" error is thrown, because the ASP.NET controls should each have an index of 1 within their cells.
I’ve spent hours (days!) debugging this and I cannot see what is causing these controls to just evaporate! Any clever programmers out there got any ideas please?

MS Access Form Not Showing Results

This is truly baffling.
I have a subform that is set up as a continuous form and receives data from a query. Here is the SQL
SELECT Top 12 Tbl_Parent_ITN.ID, Tbl_Parent_ITN.ITN_Number, Tbl_Child_ITN.ITN,
Tbl_Child_ITN.Parent_ITN_fk, Tbl_Scope_Rqmts.Completed, Tbl_Scope_Rqmts.Child_ITN_fk,
Tbl_Lkup_Requirements.Requirement, Tbl_Lkup_Basis.Basis
FROM Tbl_Parent_ITN INNER JOIN (Tbl_Lkup_Requirements
INNER JOIN (Tbl_Lkup_Basis INNER JOIN (Tbl_Child_ITN INNER JOIN Tbl_Scope_Rqmts
ON Tbl_Child_ITN.Id = Tbl_Scope_Rqmts.Child_ITN_fk) ON Tbl_Lkup_Basis.ID = Tbl_Scope_Rqmts.Basis_fk)
ON Tbl_Lkup_Requirements.ID = Tbl_Scope_Rqmts.Requirements_fk)
ON Tbl_Parent_ITN.ID = Tbl_Child_ITN.Parent_ITN_fk
WHERE (((Tbl_Parent_ITN.ID)=[Forms]![Frm_Parent_ITN_Main]![parent_id]));
The criteria in the where clause of the SQL is passed based on the control in the form that precedes this form. The value is the primary key of the parent table that joins with several child tables to get all of the data for the subform.
When I open the with one record that has children it works perfectly fine and the form populates with 12 records. However, when I go run the same operation on the next parent record I don't get any results in view even though that parent has 4 child records.
What is really perplexing is that when I run the query independently from opening the form it gives me the exact results that I want based on that 2nd parent record. Furthermore, if I change the subform to a datasheet I still don't get any records; however, I can see the results in the filters of the datasheet form if I select those column headers (but no records show in the form itself). The properties of the subform are the exact same in both cases, so I can't understand what would cause the records to show in the 1st case but not the 2nd??
A few suggestions:
1. Try to do a Me.Refresh to see what happens.
2. Put a break point in the form's OnError event to see is there is something wrong.
3. Check if there is any other data source that may be locking any data that should be refreshed.
4. Verify that the no properties that define the parent-child relationship of the data is being changed during runtime.
5. Check if the parent record id gets refreshed on the OnCurrent event of the form.
Hope this helps,
FunkSoulBrother

How to create a form with variable no. of field set

I have to design a page for user information, for some background verification purpose, at my work. I need a set of fields for address, total count of which will be selected by user to update the form with that many fields. So, if user selects 3, form will have 3 set of address fields. Similar concept for work and education details.
Right now, I am passing the count to a handler page, which checks total count, and return it along with querystring, back to the main page. I am able to update the no. of fields, this way, but, all values are lost, once I return to the form. There are a lot of fields to even use session object for every value. Also, it resets the count of other such field set to 0. So, if I select 4 in address field, it renders four set of address fields, but fields for other details are gone.
I need to know, if it is possible to update the fields, using just one page, instead of creating a handler file to handle the redirect, so that I don't lose other data.
Sorry, for sounding a bit confusing. Will update the question, if needed.
Edit:
Similar blocks are there for education and work details. I want the update button to update the block, with that many fields, while retaining the values already entered by the user.
I have finally shifted the update code to one page. And the total count of blocks, is calculated by this way.
if request.form("addresscount") <> "" then
varaddresscount = request.form("addresscount")
else
varaddresscount = 1
end if
varaddresscount is used to loop through the html code which renders address fields. Even with this method, if I click on update button to change the total field count, every value entered by user is reset to default. Is there a way to retain the no. of fields without using session object, as there are way too many fields for which I have to store the value in session.
Why not have just a "add address" button that, whenever clicked, adds a extra set of input boxes using Javascript? That solves a lot of your problems regarding retaining the data on already filled in fields AND it makes it easier for the user.

How to change a status of a button in gridview

I am working on a job portal site where user can enter multiple profiles.
I am showing all profiles in a gridview. My requirement is now that
there should be a status link with his all profiles (it is in bit).
Example:
This is my gridview:
jobseekerid | Profileid | status
1 2 Active
1 3 DeActive
1 4 DeActive
....
....
From all of this only one profile can be active.
So when the user clicks on any inactive profile, it will become active and then all
remaining profile will become inactive.
The condition is that at a time only one profile can be active. (When he make his one profile active, the remaining ones will become inactive automatically.)
Now the Problem is, I am not able to make all the remaining profiles inactive when the user makes a profile active in the gridview.
How can I do this?
This is a common problem, and is usually solved by retaining the original dataset that you populated the gridview with. So, in your case, you should stored the dataset (or a list of profiles - whatever dataset you used to populate gridview) in a viewstate (given it is not too big, otherwise the page will load slow). Then, when you populate your gridview, you need to assign a profile ID to each click event, so you know which one got clicked. You then need to loop over the dataset that you saved in your viewstate, and update all of them (except the one that just got clicked) to inactive. The loop is not as cludgy as it sounds... with sql server 2005 and 2008 you have a new datatype called 'table', so you can pass all the other profileIDs in a one swoop to the DB to update them all to inactive.
Upon an update, loop through the table rows of the Gridview and change the values accordingly, if that's sufficient enough to update the record in the backend. If there is a value associated with it, you could hold that value in a hidden field and change the value in the hidden field as well.
If these changes are going to happen on the fly (i.e. you are updating the database upon a change), you could set a hidden field as a postback trigger and eval() it when you've clicked on whatever button is saving your updated record with the updated status. This would cause the RowCommand to fire, where you could loop through each record and update accordingly.

Cannot enable viewstate?

I have a web apllication with wizard control with 4 step index. Each having a dynamically editable grid which need to put only valid data & goto next.
In one step which have 2 columns which are not require to be editable because it has set its value from previous field by calculation, done with javascript.
But problem is that when we decalre these field as a enable false or read only mode then it works but can not able maintain state means its value goes off when we change the step index changed. (If that field are editable then it works ok.)
Also I tried it by use of label field but the same thing happen when the step changes label can not maintan its state (its value clears when step changes).
Please give me idea how to resolve this problem. Also each step has a dynamically created editable grid.
Thanks & Regards,
Girish
Well, if your javascript is doing the calculation from another field that is set by the user, then why couldn't you just redo that calculation on the server side when switching to the next step. I am assuming that the "value from previous field" is getting passed back to the server.
Another solution would be to have your javascript populate a hidden field too (in addition to the one viewable by the user). Then you just read that hidden field.

Resources