Friday, July 8, 2016

Apex APEX_ERROR API

    When we create an apex process to execute some pl/sql code, we have the Success Message and Error message fields to input a message and give to the user an alert at the end of the process execution informing the success or failure.
     Usually there are many possibilities of error on a extensive pl/sql code, and one simple generic error alert is not so productive. It's more easier to show specific messages telling the user in which process point something went wrong.

The Apex API called APEX_ERROR helps a lot. When this procedure is executed, the an apex error message alert shows. We can use this on validations and process.

A simple way to use it:

begin

If :P1_ENAME is null then


APEX_ERROR.ADD_ERROR (
    p_message  => 'The employee name can´t be null',
    p_display_location => apex_error.c_inline_in_notification );

Elsif :P1_HIREDATE > sysdate then

APEX_ERROR.ADD_ERROR (
    p_message  => 'The Hiredate must be less than or equal to today',
    p_display_location => apex_error.c_inline_in_notification );

End IF;

insert into Emp(ENAME,HIREDATE) values (:P1_ENAME,:P1_HIREDATE);

end;

Submit the page and the result is:






You can show the notification on the page item too. Adding the p_page_item_name parameter the page item is highlighted on the page. The error message is displayed inline in a notification.

begin

If :P1_ENAME is null then

APEX_ERROR.ADD_ERROR (
    p_message  => 'The employee name can´t be null',
    p_display_location => apex_error.c_inline_with_field_and_notif ,
    p_page_item_name => 'P1_ENAME');

Elsif :P1_HIREDATE > sysdate then

APEX_ERROR.ADD_ERROR (
    p_message  => 'The Hiredate must be less than or equal to today',
    p_display_location => apex_error.c_inline_with_field_and_notif ,
    p_page_item_name => 'P1_HIREDATE');
Else

insert into Emp(ENAME,HIREDATE) values (:P1_ENAME,:P1_HIREDATE);

End If;
end;


apex error


The complete APEX_ERROR API docs, can be found on this link

5 comments:

  1. Boa tarde,

    Muito legal o seu post. Não sabia como utilizar essa API.

    At,

    ReplyDelete
    Replies
    1. Obrigado, se precisar de ajuda, é só falar

      Delete
  2. Version 5 documentation link:

    https://docs.oracle.com/cd/E59726_01/doc.50/e39149/apex_error.htm#AEAPI2209

    ReplyDelete
  3. Now my question is this: I have a pl/sql package that has procedures called by an APEX page. I am using the standard APEX error handling functionality, and wish to have the exceptions that occu in the package to be displayed in a nice fashion to the APEX application user AND logged in the standard error handling table.

    Do you know if this is possible?

    ReplyDelete
  4. are you calling this pl/sql package by an apex process? If yes, i suggest you to create an out parameter on your procedure with the error message and than use the apex_erro api to fire the alert at the apex process

    ReplyDelete