Oracle APEX Selection list - oracle

I have a form on a table with some values (text fields) and a select list. The select list is declared in shared components and shows me values from another table. Also I have some process (after submit) to modify and create new table entry. Everything works fine without propagating values from select list. There are no errors from the process. It looks like the process didn't get a value for :P22_WORKER_LIST from the selection list. It should work when I push the create or save buttons but nothing happened. Every instruction in BEGIN END block runs great without this one.
Process:
BEGIN
<some instructions>
UPDATE "WORKER" SET ACCOUNT_LOGIN = :P22_LOGIN
WHERE SURNAME = :P22_WORKER_LIST;
END;

Thank you for suggestion with the session state. After submiting page it turned out that the value for my :P22_WORKER_LIST is a WORKER_ID instead a SURNAME.
BEGIN
<some instructions>
UPDATE "WORKER" SET ACCOUNT_LOGIN = :P22_LOGIN
WHERE WORKER_ID = :P22_WORKER_LIST;
END;

Related

Oracle automatically insert record in multirecord block part 2

My table looks like this:
+-------------------+
|Name |
+-------------------+
|Name1 |
|Name2 |
|Name3 |
|Name4 |
|Name1Jr |
|Name2Jr |
|Name4Jr |
+-------------------+
My multirow block looks like:
What I wanted to know is how can I insert a record that has the same name with Jr after I insert a name. For example, I inserted Name2, it will also insert Name2Jr into the multirow block. Like this:
Note: I need to get the value of the automatically inserted data from database.
I tried in WHEN-NEW-RECORD-INSTANCE trigger(the answer of Sir #Littlefoot on my last question):
if :system.trigger_record = 1 and :test.name is null then
-- do nothing if it is the first record in a form
null;
else
duplicate_record;
if substr(:test.name, -2) = 'Jr' then
-- you've duplicated a record which already has 'Jr' at the end - don't do it
:test.name := null;
else
-- concatenate 'Jr' to the duplicated record
:test.name := :test.name || 'Jr';
end if;
end if;
And now, what I want is to know if there is a way to do it on WHEN-VALIDATE-RECORD trigger. The problem there is that the duplicate_record can't be used in WHEN-VALIDATE-RECORD trigger. How to do it using procedure, function or something? Thanks in advance!
DUPLICATE_RECORD is a restricted procedure and you can't use it in WHEN-VALIDATE-RECORD trigger (or any other of the same kind).
As you have to navigate to the next record (if you want to copy it), even if you put that restricted procedure into another PL/SQL program unit, everything will just propagate and - ultimately - raise the same error. So ... you're out of luck.
Even if you wrote a (stored) procedure which would insert that "Jr" row into the database somewhere behind the scene, you'd have to fetch those values to the screen. As EXECUTE_QUERY is the way to do it, and as it is (yet another) restricted procedure, that won't work either.
If you planned to clear data block and fill it manually (by using a loop), you'd have to navigate to next (and next, and next) record with NEXT_RECORD, and that's again a restricted procedure. Besides, if it was a data block (and yes, it is), you'd actually create duplicates for all records once you'd save changes so - either it would fail with unique constraint violation (which is good), or you'd create duplicates (which is worse).
BTW what's wrong with WHEN-NEW-RECORD-INSTANCE? What problems do you have when using it?
What We need is
a Data Block (BLOCK1) with
Query Data Source Name is mytable,
and Number of Records Displayed set to more than 1(let it be 5 as in your case).
a Text item named as NAME same as the column name of the table
mytable, and Database Item property set to Yes.
a Push Button with Number of Records Displayed set to 1,
Keyboard Navigable and Mouse Navigate properties are set to No
and has the following code (inside WHEN-BUTTON-PRESSED trigger):
commit;
Query_Block1;
Where Query_Block1 is beneath Program Units node with this code :
PROCEDURE Query_Block1 IS
BEGIN
execute_query;
last_record;
down;
END;
A POST-INSERT trigger beneath BLOCK1 node with code :
insert into mytable(name) values(:name||'Jr');
An ON-MESSAGE trigger at the form level with the code (to suppress the messages after commit):
if Message_Code in (40400, 40401) then
null;
end if;
And WHEN-NEW-FORM-INSTANCE trigger with the code :
Query_Block1;

Update table using multi record block in post forms commit

what I wanted is to update my table using values from a multi record block and here is what I tried in post forms commit:
BEGIN
FIRST_RECORD;
LOOP
UPDATE table1
SET ord_no = :blk.new_val;
EXIT WHEN :SYSTEM.LAST_RECORD='TRUE';
NEXT_RECORD;
END LOOP;
END;
but when I save I got an error
FRM-40737: Illegal restricted procedure
FIRST-RECORD in POST-FORMS-COMMIT trigger.
OK, a few things to talk about here
1) I'm assuming that 'table1' is NOT the table on which the block is based. If the block was based on table1, simply as the user edits entries on screen, then when you do a COMMIT_FORM command (or the users clicks Save) then the appropriate updates will be done for you automatically. (That is the main use of a database-table based block).
2) So I'm assuming that 'table1' is something OTHER than the block based table. The next thing is that you probably need a WHERE clause on your update statement. I assume that you are updating a particular in table1 based on a particular value from the block? So it would be something like:
update table1
set ord_no = :blk.new_val
where keycol = :blk.some_key_val
3) You cannot perform certain navigation style operations when in the middle of a commit, hence the error. A workaround for this is to defer the operation until the completion of the navigation via a timer. So your code is something like:
Declare
l_timer timer;
Begin
l_timer := find_timer('DEFERRED_CHANGES');
if not id_null(l_timer) then
Delete_Timer(l_timer);
end if;
l_timer := Create_Timer('DEFERRED_CHANGES', 100, no_Repeat);
End;
That creates a timer that will fire once 100ms after your trigger completes (choose name, and time accordingly) and then you have your original code on a when-time-expired trigger.
But please - check out my point (1) first.

PL/SQL: ORA 01422 fetch returns more than requested number of rows

I am developing an order transaction where a user can order a product. Once they clicked the 'add to cart' button, it will be able to save on the database in how many times they want with the same order id. Order id is like a transaction id.
My problem is that whenever I want to display the items that customer ordered, it displays an error or ORA 01422. How can I resolve this error?
Here is my code
DECLARE
order_item_id NUMBER;
BEGIN
order_item_id := :MOTOR_PRODUCTS_ORDER.M_ORDERID;
SELECT MOTOR_ID,
MOTOR_QTY_PURCHASED,
UNITPRICE
INTO :MOTOR_ORDER_ITEMS.MOTOR_ID,
:MOTOR_ORDER_ITEMS.MOTOR_QTY_PURCHASED,
:MOTOR_ORDER_ITEMS.UNITPRICE
FROM MOTOR_ORDERS
WHERE motor_order_id = order_item_id;
END;
As krokodilo says, this error is caused because your query returns multiple rows. Depending on what you want to do, you have a couple of options.
If you want multiple values then either use a loop and process them one row at a time (if you are going to be performing a dml operation use bulk collect). If you only want a single row then narrow your result set down with an extra where clause, or use MAX to ensure you only get one value back.
If there is more than one row which will be returned from a query you'll need to use a cursor. One way to do this is with a cursor FOR loop:
DECLARE
order_item_id NUMBER;
BEGIN
order_item_id := :MOTOR_PRODUCTS_ORDER.M_ORDERID;
FOR aRow IN (SELECT MOTOR_ID, MOTOR_QTY_PURCHASED, UNITPRICE
FROM MOTOR_ORDERS
WHERE motor_order_id = order_item_id)
LOOP
-- Do something here with the values in 'aRow'. For example, you
-- might print them out:
DBMS_OUTPUT.PUT_LINE('MOTOR_ID=' || aRow.MOTOR_ID ||
' MOTOR_QTY_PURCHASED=' || aRow.MOTOR_QTY_PURCHASED ||
' UNITPRICE=' || aRow.UNITPRICE);
END LOOP;
END;
Best of luck.
This looks like a Forms question; is it? If so, my suggestion is to let Forms do that job for you.
According to what you posted, there are two blocks:
MOTOR_PRODUCTS_ORDER (a master block, form type)
MOTOR_ORDER_ITEMS (a detail block, tabular type)
I guess that there is a master-detail relationship between them. If there's none, I'd suggest you to create it. Although you can make it work without such a relationship, it'll be much more difficult. If you are unsure of how to do it, start from scratch:
delete MOTOR_ORDER_ITEMS block (detail)
create it once again, this time by following the Data Block Wizard
Set MOTOR_PRODUCTS_ORDER to be its master block
Relationship is on ORDER_ID column/item
Let's presume that by this point everything is set up. Retrieving items that belong to that ORDER_ID is now very simple:
navigate to master block
enter query mode
enter value into an item that represents ORDER_ID
execute query
End of story. Forms triggers & procedures (which were created by the Wizard) will do its job and retrieve both master and detail records.
No need for additional coding; if you're skilled developer, you can create such a form in a matter of minutes. Won't be beautiful, but will be effective & reliable.
There's really no use in doing it manually, although it is possible. Your code works if there's a single item for that ORDER_ID. For two or more items, as you already know, it'll fail with TOO-MANY-ROWS error.
Basically, if you insist, you should use a loop:
you'd enter ORDER_ID into the master block
as you need to move through the detail block, i.e. use NEXT_RECORD, which is a restricted procedure, you can't use number of triggers (open Forms Online Help System and read about them) so a "Show items" button (with its WHEN-BUTTON-PRESSED trigger) might be just fine
a cursor FOR loop would be your choice
for every row it fetches, you'd populate block items and
navigate to next record (otherwise, you'd keep overwriting existing values in the 1st tabular block row)
As I said: possible, but not recommended.

Refresh a Classic Report based on a collection

I am currently learning apex but got stuck.
I am using Oracle apex 4.2. My app consists of 2 reports. In first I am choosing some checkboxes and clicking button which fires DA with some js code and uses apex.server.process with AJAX Callback like in this link.
Next the "On Demand" process launches PL/SQL procedure (I know there is no reason for that, but I did it for learning purpose) which processes checked values and concatenates them into one clob which is then being added to collection.
So each time I am clicking the button, exactly 1 new clob is being added.
My second report just reads ALL clobs from collection, and I want to refresh it each time after I click the button without submitting all of the page content. I have added DA with a True Action: Refresh but it works with delay. When I first click it - nothing happens, on the second click the clob which was generated previously shows up in the report region and so on. I guess I need to submit collections value to session state but I have nothing to add to the reports "Page Items to Submit" option.
Not seeing "most recent" data
The only reason you would see "outdated" data is because you use an ajax call to update the collection. It's asynchronous!
Similar, the refresh is async too. The actions don't wait for eachother to complete.
It can go kind of like this:
collect the checkboxes and send them off to some process, which somewhere down the line will return me something
sometimes, it depends, the ajax call is fast enough to have finished before the region refresh occurs
refresh the region
other times, the ajax call has taken a bit longer, and the refresh has already occured
To solve this, you'll have to add something to your apex.server.process call: the refresh of the collection-based report should only occur when the callback has completed.
Firstly though: add a static report id to your collection-based report. Do this by editing it. Providing a static id allows for easier selection of the region through a selector.
apex.server.process('Process Checked Rows', {f01: arr_f01}).done(function(){
//do something when the callback has finished
//like triggering the refresh event on the report required
apex.jQuery("#your_report_static_id_goes_here").trigger("apexrefresh");
});
Now the report will only refresh when you're sure the checkboxes have been processed into the collection.
Using a clob for storing IDs
As I've pointed out in the comments, it makes no sense at all to use a clob to keep the selected IDs together. There's just no argument to make for it beside perhaps pure experimentation. Honestly, time better spent elsewhere if you want to experiment with CLOBs.
Collections are meant to be a Global Temporary Table replacement within the context of Apex sessions. Use them like you would use a table. Simply insert the IDs of the selected rows into the collection.
FOR i IN 1..apex_application.g_f01.count
LOOP
apex_collection.add_member
( p_collection_name => 'CHECKBOX_COLLECTION'
, p_n001 => apex_application.g_f01(i) --NOTE: using N001 here to store a numerical ID
);
END LOOP;
If you only want to have uniques then you can do that here by adding a select on the collection, or add a distinct in the query used to loop or select over the collection.
FOR i IN 1..apex_application.g_f01.count
LOOP
DECLARE
l_exists BINARY_INTEGER;
BEGIN
SELECT 1
INTO l_exists
FROM apex_collections
WHERE collection_name = 'CHECKBOX_COLLECTION'
AND n001 = apex_application.g_f01(i);
EXCEPTION WHEN no_data_found THEN
apex_collection.add_member
( p_collection_name => 'CHECKBOX_COLLECTION'
, p_n001 => apex_application.g_f01(i) --NOTE: using N001 here to store a numerical ID
);
END;
END LOOP;
You can then eeasily loop over the records in this collection or use it to limit rows in other queries (eg another report).
SELECT n001 selected_id
FROM apex_collections
WHERE collection_name = 'CHECKBOX_COLLECTION';

FRM-40401 no changes to save after WHEN-CREATE-RECORD trigger

I googled this but, unfortunately, could not find any solution.
I have a simple form (Oracle Forms Builder 10g) with a single block. The form is written in Oracle EBS style, that is, the block is based on a view that fetches the base table fields together with the rowid and DML events (on-insert, on-update etc. triggers) are handled by a table handler package.
The functionality I wanted to add was the following: when a user creates a new record, the form automatically suggests values for all fields in the form. So, I created a WHEN-CREATE-RECORD trigger that calculates the field values and assigns them. All, except the primary key wich is based on a Sequence and is handled by the package.
Everything runs OK when I create the new record but when I attempt to save it, all I get is a FRM-40401 "no changes to save" error and nothing happens.
I tried to trace the error and it seems like the form considers the record as NEW with no changes on it. This happens even if I try to explicitly alter the record status to INSERT.
I already tried to change the default behaviour to STANDARD.COMMIT (created an ON-COMMIT trigger for that) but this did not dfix anything.
For the record, I tried to make the form table-based, getting rid of table handlers and leaving all DML to Forms. I still get FRM-40401.
I can't understand what is going wrong, any ideas please?
The record status is reset to NEW after the when-create-record trigger completes. This normally makes sense, since you are effectively setting the default values for items, but the user hasn't actually entered any data yet.
You need something to mark the record for insert after the trigger has finished - normally the user would do this when they enter some data into the record. If you want the user to be able to save the record without changing anything in it, you could perhaps add something to the save button to do a no-change assignment, e.g.
:MYBLOCK.ANYITEM := :MYBLOCK.ANYITEM;
This would cause the record to be marked for insert.
OK, for the time being I used the classic TIMER workaround and everything works as it should:
PACKAGE body form_timers IS
PROCEDURE CREATE_NEW_RECORD;
procedure do_create(name varchar2) is
timer_id TIMER;
Begin
timer_id := CREATE_TIMER(name,1,NO_REPEAT);
End;
procedure expired is
expired_timer CHAR(20);
BEGIN
expired_timer:=GET_APPLICATION_PROPERTY(TIMER_NAME);
IF expired_timer='CREATE_NEW_RECORD' THEN
CREATE_NEW_RECORD;
-- ELSIF expired_timer='T2' THEN
-- /* handle timer T2 */ NULL;
ELSE
NULL;
END IF;
END;
PROCEDURE CREATE_NEW_RECORD IS
/* create record logic goes here */
END;
END;
... but still, I'd like to know why this behaviour occurs.

Resources