# Showing Success and Error Messages in Oracle APEX Made Easy

### **Using PL/SQL**

**Soft validation error** - Does not crash page, adds to stack, for business rule validation failure message. (for more check [apex\_error](https://docs.oracle.com/en/database/oracle/apex/24.2/aeapi/APEX_ERROR.html)).

```sql
apex_error.add_error (
    p_message          => 'Username should not contain numbers',
    p_display_location => apex_error.c_inline_in_notification,
    p_page_item_name   => 'P3_USERNAME'
);
```

**Hard Stop Exception** - Crashes execution immediately, only for critical technical errors

```sql
-- Range must be between -20999 and -20000
RAISE_APPLICATION_ERROR(-20001, 'Critical Error: Authorization Failed');
```

**Success Message after page submit -** To be used in page processes, Display on next page load.

```sql
APEX_APPLICATION.G_PRINT_SUCCESS_MESSAGE := 'Record Saved Successfully.';
```

Better way to show **static success message** after page submit is to put it in the `success message` attribute of the page process. If you want to make success message dynamic (e.g. “Row ID - XXXX processed!”), return the row\_id value in a hidden page item (let PX\_ROW\_ID) and define the `success message` attribute value like - `Row ID - &PX_ROW_ID. processed!`.

Check example use [here](https://oracleapex.com/ords/r/anujs_workspace/technuj/success-and-error-messages?session=103542066171277).

### Using apex JavaScript API `apex.message`.

Show and clear errors using JS

```javascript
apex.message.showErrors([{
    type:       "error",
    location:   [ "page", "inline" ],
    pageItem:   "P10_ITEM_NAME",
    message:    "Value must be greater than 0",
    unsafe:     false
}]);
apex.message.clearErrors();
```

Show and clear Success messages using JS

```javascript
apex.message.showPageSuccess("Process Completed Successfully!");

// auto dissmiss duration can be set by putting below code into "Execute When Page Loads" section of page header.
apex.message.setDismissPreferences({
    dismissPageSuccess: true,
    dismissPageSuccessDuration: 3000 // Duration in milliseconds (3 seconds)
});
```

**Referances** - [apex.message](https://docs.oracle.com/en/database/oracle/apex/24.2/aexjs/apex.message.html)
