How to display a success message - oracle

How to generate Success Message in Apex without using custom scripts like this video
I made a form and a button, this button inserts data into a table with identity PK using PLSQL, I want to display success message with the generated ID
The button has PLSQL like this
DECLARE
V_ID INT;
BEGIN
INSERT INTO TABLE(COL) VALUES (:VALUE) returning ID into :V_ID ;
apex_application.g_print_success_message :=
'<span style="green">'||:V_ID || 'added </span>';
END;
the row is inserted into the database but Nothing is displayed
The Video is useless to me because he/she altered an existing message, this is why apex_application.g_print_success_message worked for the author of the video but I want to display a message like that in the first place, so how to display the message I just created.

create a hidden page item, let's call it P1_RETURNED_ID
button runs a process, right? That's your PL/SQL block. Change it to
insert into your_table (col) values (:P1_SOME_VALUE)
returning id into :P1_RETURNED_ID;
Put this into the "Success message" property:
Added was &P1_RETURNED_ID.
(literally, ampersand (&) followed by item name (P1_RETURNED_ID) followed by dot (.)

Related

ORACLE 10g form. check if value entering exists in database

Question 1 :
while inserting how will you check whether the value you entered in that textbox does matched with that in the database.
My example and approach not working. error displayed is:
table, view does not allow in this context.
when button pressed[Add button]
//blockname compare //database table DRINK, column drink_id
IF (:RESERVATION_BLOCK.DRINK_ID<>DRINK.DRINK_ID) THEN
MESSAGE('IF PART');
ELSE
MESSAGE('ELSE PART');
END IF;
QUESTION 2:
Using if statement to add in database
not working error displayed is when button pressed trigger raised
unhandled exception ORA-00001.
My example not working: when button pressed [SAVE button] code works perfectly without if statement but that's not a good practice when having null
IF (:RESERVATION_BLOCK.DRINK_ID is null) THEN
MESSAGE('No Drink Ordered');
ELSIF (:RESERVATION_MENU_DRINK_BLOCK.DRINK_ID is not null) THEN
INSERT INTO RESERVATION_DRINK
VALUES(
:RESERVATION_BLOCK.RESERVATION_ID, //comes from previous tab pane block
:RESERVATION_MENU_DRINK_BLOCK.DRINK_ID,
:RESERVATION_MENU_DRINK_BLOCK.QUANTITY);
COMMIT;
MESSAGE('DRINK ORDER SAVED SUCCESSFULLY!');
END IF;
The first part of the question: you can't reference table in IF, do it before it:
DECLARE
l_cnt NUMBER;
BEGIN
SELECT COUNT (*)
INTO l_cnt
FROM drink d
WHERE d.drink_id = :reservation_block.drink_id;
IF l_cnt > 0
THEN
MESSAGE ('That ID exists');
ELSE
MESSAGE ('That ID does not exist');
END IF;
END;
As of your second question: ORA-00001 means that you tried to insert a duplicate value which is restricted by unique index (might be a primary or unique key constraint).
What to do?
fix the ID value; I don't have how you populated it into the block, but you did it wrong. Consider using a sequence so that Oracle would make sure that values are always unique
modify the constraint; maybe it has to be composite (having two or more columns, not just the ID)
probably the most stupid solution, but - it is a solution, after all: drop the unique constraint

Access "Page Items to Submit" values inside a trigger in oracle apex

I want to access extra page item value from a trigger which isn't in the related data table. For an example I have a table like below,
Employee
----------------------
empId | fName | lName
----------------------
1 | John | Doe
----------------------
2 | Jane | Doe
Apex form items will be like,
P500_EMPID, P500_FNAME, P500_LNAME
I have an extra page item called P500_SOMEIDSwhich is a multi select list. I want to access those selected values inside After Insert trigger of the Employee table. I tried to add this item into "Page Items to Submit". But I do not know how to access it inside that trigger. Is it possible..? and How..?
In the page process that handles your table update (that will be a process of type "Form - Automatic Row Processing (DML)", under "Settings" there is a attribute "Return Primary Key(s) after Insert". If that is set to "On", then the insert statement will return the value of the inserted row into the page item that is defined as your primary key.
Example:
Create a form on emp
Make P1_EMPNO the primary key page item
Suppose you create a new row, and that empno value is 1000. Then after the automatic row processing page process is run, the value of P1_EMPNO is set to 1000.
If you want to insert rows into another table referencing the newly created empno then you can create a page process (that executes after the row processing) of type pl/sql code like this:
BEGIN
INSERT INTO some_other_table (empno) VALUES (:P1_EMPNO);
END;
Using triggers for business functionality should be avoided whenever possible.
Instead of a database trigger, use a stored procedure which will do the same job. Pass any number of parameters you want (including the P500_SOMEIDS item).

Oracle APEX values do not load on the form

From my APEX page I am opening a pop up page and trying to load it with data from the database. To do so, I used
Pre-Rendering After Header Process. Process type is set to PL/SQL code:
BEGIN
IF :P3_RECORD_ID IS NOT NULL THEN
select TYPE_ID, RECORD_TEXT
INTO :P3_TYPE_ID, :P3_RECORD_TEXT
from TABLE1
where RECORD_ID = :P3_RECORD_ID;
END IF;
END;
On the pop up page I have a dropdown (P3_TYPE_ID) which is filled from the LOV and a text field (P3_RECORD_TEXT).
The values show up in the session state but not in the dropdown or a text field. I can't figure out what am I doing wrong...
I also tried Automated Row Fetch but that did not load any values into fields either, just into session state
Did you consider using default value for those items? It would be a "PL/SQL Function Body" and look like this (for P3_TYPE_ID):
declare
l_type_id table1.type_id%type;
begin
select max(type_id)
into l_type_id
from table1
where record_id = :P3_RECORD_ID;
return l_type_id;
end;
I used MAX function to avoid possible NO_DATA_FOUND and TOO_MANY_ROWS errors. Handle them in EXCEPTION section, if necessary.
Similarly, populate P3_RECORD_TEXT.

Oracle forms sort records

What I wanted is to sort records in a multi-record block before saving like this:
I already tried to change the order by of this item in here:
But it has no effect(I think).
I also want to know if I can automatically sort records in
WHEN-NEW-RECORD-INSTANCE in block level so that after entering the last record, it will be sorted.
We need to have a multi-record block ( block1 ) with Query Data Source Name property set to a table name which has a varchar2 type column namely str1,
and a Text Item (str1) with Database Item Property is "Yes" ( i.e. default ),
and lastly have a button with the following code inside of WHEN-BUTTON-PRESSED trigger of it :
declare
v_blk varchar2(25) := 'block1';
begin
commit_form;
go_block(v_blk);
execute_query;
end;
Provided we set ORDER BY Clause property set to ascii(str1) as in the picture below, we'll be able to get the desired output when the button pressed after letters are entered in the order of 'h','e','l','l','o'.

Getting values from a TABLE using SELECT inside a TRIGGER (Oracle SQL)

I am working on a Music Collection Database Project and I am stuck on this TRIGGER code. There is an Album table and the task of the trigger is to check if the album being entered already exists in the table or not. And if it does, it should copy the rating which is already present in the table for the album irrespective of the new one entered by the user. (I am using Oracle 11g)
The trigger I managed to get is :
CREATE OR REPLACE TRIGGER album_constraints BEFORE INSERT ON Album
DECLARE
a_id int;
a_title varchar(50);
a_duration varchar(6);
a_number_of_tracks int;
a_recorded date;
a_rating int;
BEGIN
SELECT album_id,
album_title,
album_duration,
album_number_of_tracks,
album_recorded,
album_rating
INTO a_id,
a_title,
a_duration,
a_number_of_tracks,
a_recorded,
a_rating
FROM Album
WHERE album_title = new.album_title and
album_duration = new.album_duration and
album_number_of_tracks = new.album_number_of_tracks and
album_recorded = new.album_recorded and
rownum = 1;
IF new.album_title = a_title and
new.album_duration = a_duration and
new.album_number_of_tracks = a_number_of_tracks and
new.album_recorded = a_recorded
THEN
new.album_rating := a_rating;
END IF;
END;
Can someone please help me achieve this? I am getting multiple errors such as "PL-SQL : Statement ignored"
Actually, there is a way to do what you are trying to do using a single table. It just doesn't involve triggers. Here's a code fragment showing the insert statement:
begin
insert into Album( id, title, duration, number_of_tracks, recorded, rating )
values( v_id, v_title, v_duration, v_number_of_tracks, v_recorded, v_rating );
exception
when DUP_VAL_ON_INDEX then
update Album
set rating = v_rating
where id = v_id;
end;
This assumes ID is the PK and it is something like an ISPN number which uniquely identifies each published album, not a sequential value. If the album does not exist in the table, the Insert statement executes normally. If the album already exists, the Insert statement fails, the exception is caught and the Update is executed instead.
If you really, really want to use a trigger, then front the table with a view and create an "instead of" trigger on the view. To avoid having to make any changes in the app code, name the view Album and the table something else. (My convention is to call the table "Album_". The trailing underscore tells me that no direct access to the table is allowed and there is a view without an underscore where all access takes place. You can use whatever convention you want, of course.)
CREATE OR REPLACE TRIGGER album_constraints
instead of INSERT ON Album
for each row
begin
insert into Album_( id, title, duration, number_of_tracks, recorded, rating )
values( :new.id, :new.title, :new.duration, :new.number_of_tracks, :new.recorded, :new.rating );
exception
when DUP_VAL_ON_INDEX then
update Album_
set rating = :new.rating
where id = :new.id;
end;
Here is where I have to give a hat tip to Sql Server. It allows "instead of" triggers on tables making a "pass thru" view unnecessary.

Resources