Skip to content

Instantly share code, notes, and snippets.

@SullivanC137
Forked from kzhangkzhang/APEX_CheatSheet.md
Created January 27, 2020 07:44
Show Gist options
  • Save SullivanC137/743f8dbe87ba2d45f9f54d70e6aeda27 to your computer and use it in GitHub Desktop.
Save SullivanC137/743f8dbe87ba2d45f9f54d70e6aeda27 to your computer and use it in GitHub Desktop.

Oracle APEX Cheat Sheet

Support\APEX\APEX_CheatSheet.md

Oracle Application Express (APEX) Cheat Sheet

PLSQL

Apex Application

Stop Apex Engine

BEGIN
  apex_application.stop_apex_engine();
END;

Session

Create Session

BEGIN
  apex_session.create_session(
                      p_app_id=>498,
                      p_page_id=>200,
                      p_username=>'KZHANG');
END;

Detach Session

BEGIN
  apex_session.detach;
END;

APEX Debug

Turn on Debug

BEGIN

c_log_level_error        CONSTANT t_log_level := 1; -- critical error
c_log_level_warn         CONSTANT t_log_level := 2; -- less critical error
c_log_level_info         CONSTANT t_log_level := 4; -- default level if debugging is enabled (e.g. used by apex_application.debug)
c_log_level_app_enter    CONSTANT t_log_level := 5; -- application: messages when procedures/functions are entered
c_log_level_app_trace    CONSTANT t_log_level := 6; -- application: other messages within procedures/functions
c_log_level_engine_enter CONSTANT t_log_level := 8; -- apex engine: messages when procedures/functions are entered
c_log_level_engine_trace CONSTANT t_log_level := 9; -- apex engine: other messages within procedures/functions

  -- https://dgielis.blogspot.com/2019/06/see-apex-debug-info-in-plsql-and-sql.html
  apex_debug.enable(
              p_level => apex_debug.c_log_level_info
  );

  apex_debug.message(p_message => 'Debug enabled.');

END;

View Debug

SELECT
  *
FROM
  apex_debug_messages
WHERE 
  session_id = 16458652970080
ORDER BY message_timestamp;

APEX UTIL

Redirect URL

apex_util.redirect_url(p_url => apex_page.get_url(p_page => 1));

-- Warning: redirect_url raises the exception ORA-20876: Stop APEX Engine, so it’s probably preferable to avoid combining this with other PL/SQL code that might need to be committed first.
BEGIN
  -- you need manually call apex_application.stop_apex_engine()
  owa_util.redirect_url('https://apex.oracle.com/pls/apex/f?p=42:100', true);
  apex_application.stop_apex_engine();
END;  

Create URL

apex_page.get_url(
        p_page => 4,
        p_items => 'P4_UNIQUE_ID,P4_REVISION',
        p_values => unique_id||','||revision
    ) car_number_link

Get build option status

SELECT 
  apex_util.get_build_option_status(122, 'KEVIN_DEV_ONLY')
FROM dual;

OWA_UTIL

Set Http status code

BEGIN
  -- https://blog.longacre.ch/2019/06/setting-http-response-status-code-301.html
  owa_util.status_line(
                    nstatus        => 301,
                    bclose_header  => false);

JavaScript

Trigger page refresh from modal dialog if and only if target page is page # X

var $ = apex.jQuery;

// Need to refresh whole page after alter EEE Engr action given authority is impacted
if (this.data.dialogPageId === X){
    apex.submit();  
}

Get the display value for page item with LOV

$('#P3_CAPEX_START_DATE option:selected').text();

Get the return value for page item with LOV

$('#P3_CAPEX_START_DATE option:selected').val();

Get the key pressed and char code

apex
  .item("PX_TYPED")
  .setValue(
    apex.item("PX_TYPED").getValue() +
      String.fromCharCode(this.browserEvent.which)
  );

modal dialog via jQuery API

apex
  .jQuery(
    '<div style="margin:4px; padding: 4px;">Please enter your name and email to receive weekly news letter</div>'
  )
  .dialog({
    modal: true,
    resizable: false,
    height: 200px,
    width: 300px,
    title: "Sign Up",
    buttons: {
        "OK": function () {
            apex.jQuery(this).dialog("close");
        },
        "Cancel": function () {
            apex.jQuery(this).dialog("close");
        }
    }
  });

Validate Email address using regular expression

// https://stackoverflow.com/questions/46370725/how-to-do-email-validation-using-regular-expression-in-typescript
var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))
@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/);

var email = apex.item("PX_EMAIL").getValue();

if (re.test(email)){
    console.log("this is a valid email address");
} else {
    apex
  .jQuery(
    '<div style="margin:4px; padding: 4px;">' + email + 'is not valid</div>'
  )
  .dialog({
    modal: true,
    resizable: false,
    height: 200px,
    width: 300px,
    title: "Invalid Email",
    buttons: {
        "OK": function () {
            apex.jQuery(this).dialog("close");
        }
    }
  });
}

catch mouse current coordination (x,y)

"x: " + this.browserEvent.clientX + "y: " + this.browserEvent.clientY;

detect which mouse button is clicked

// left button
this.browserEvent.button == 0;
// middle button
this.browserEvent.button == 1;
// right button
this.browserEvent.button == 2;

Oracle APEX Cheat Sheet

Support\APEX\APEX_CheatSheet.md

Oracle Application Express (APEX) Cheat Sheet

PLSQL

Apex Application

Stop Apex Engine

BEGIN
  apex_application.stop_apex_engine();
END;

Session

Create Session

BEGIN
  apex_session.create_session(
                      p_app_id=>498,
                      p_page_id=>200,
                      p_username=>'KZHANG');
END;

Detach Session

BEGIN
  apex_session.detach;
END;

APEX Debug

Turn on Debug

BEGIN

c_log_level_error        CONSTANT t_log_level := 1; -- critical error
c_log_level_warn         CONSTANT t_log_level := 2; -- less critical error
c_log_level_info         CONSTANT t_log_level := 4; -- default level if debugging is enabled (e.g. used by apex_application.debug)
c_log_level_app_enter    CONSTANT t_log_level := 5; -- application: messages when procedures/functions are entered
c_log_level_app_trace    CONSTANT t_log_level := 6; -- application: other messages within procedures/functions
c_log_level_engine_enter CONSTANT t_log_level := 8; -- apex engine: messages when procedures/functions are entered
c_log_level_engine_trace CONSTANT t_log_level := 9; -- apex engine: other messages within procedures/functions

  -- https://dgielis.blogspot.com/2019/06/see-apex-debug-info-in-plsql-and-sql.html
  apex_debug.enable(
              p_level => apex_debug.c_log_level_info
  );

  apex_debug.message(p_message => 'Debug enabled.');

END;

View Debug

SELECT
  *
FROM
  apex_debug_messages
WHERE 
  session_id = 16458652970080
ORDER BY message_timestamp;

APEX UTIL

Redirect URL

apex_util.redirect_url(p_url => apex_page.get_url(p_page => 1));

-- Warning: redirect_url raises the exception ORA-20876: Stop APEX Engine, so it’s probably preferable to avoid combining this with other PL/SQL code that might need to be committed first.
BEGIN
  -- you need manually call apex_application.stop_apex_engine()
  owa_util.redirect_url('https://apex.oracle.com/pls/apex/f?p=42:100', true);
  apex_application.stop_apex_engine();
END;  

Create URL

-- f?p=4500:4:17166624244700::NO::P4_UNIQUE_ID,P4_REVISION:12345,A
SELECT
  apex_page.get_url(
          p_page => 4,
          p_items => 'P4_UNIQUE_ID,P4_REVISION',
          p_values => unique_id||','||revision
      ) car_number_link
FROM dual;


-- f?p=4500:4:17166624244700:::RP:P4_UNIQUE_ID:12345
SELECT
  apex_util.prepare_url('f?p=&APP_ID.:4:&APP_SESSION.:::RP:P4_UNIQUE_ID:'||entity_id)
FROM dual;

Get build option status

SELECT 
  apex_util.get_build_option_status(122, 'KEVIN_DEV_ONLY')
FROM dual;

OWA_UTIL

Set Http status code

BEGIN
  -- https://blog.longacre.ch/2019/06/setting-http-response-status-code-301.html
  owa_util.status_line(
                    nstatus        => 301,
                    bclose_header  => false);

JavaScript

jQuery to change property and add class to parent

-- https://jeffkemponoracle.com/2019/07/conditionally-required-floating-item/
var item = $("#P1_COST_CENTRE");
item.prop("required",true);
item.closest(".t-Form-fieldContainer").addClass("is-required");

Trigger page refresh from modal dialog if and only if target page is page # X

var $ = apex.jQuery;

// Need to refresh whole page after alter EEE Engr action given authority is impacted
if (this.data.dialogPageId === X){
    apex.submit();  
}

Get the display value for page item with LOV

$('#P3_CAPEX_START_DATE option:selected').text();

Get the return value for page item with LOV

$('#P3_CAPEX_START_DATE option:selected').val();

Get the key pressed and char code

apex
  .item("PX_TYPED")
  .setValue(
    apex.item("PX_TYPED").getValue() +
      String.fromCharCode(this.browserEvent.which)
  );

$(this.affectedElements[0])
  .closet('.apex-item-wrapper')
  .fadeOut();

modal dialog via jQuery API

apex
  .jQuery(
    '<div style="margin:4px; padding: 4px;">Please enter your name and email to receive weekly news letter</div>'
  )
  .dialog({
    modal: true,
    resizable: false,
    height: 200px,
    width: 300px,
    title: "Sign Up",
    buttons: {
        "OK": function () {
            apex.jQuery(this).dialog("close");
        },
        "Cancel": function () {
            apex.jQuery(this).dialog("close");
        }
    }
  });

Validate Email address using regular expression

// https://stackoverflow.com/questions/46370725/how-to-do-email-validation-using-regular-expression-in-typescript
var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))
@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/);

var email = apex.item("PX_EMAIL").getValue();

if (re.test(email)){
    console.log("this is a valid email address");
} else {
    apex
  .jQuery(
    '<div style="margin:4px; padding: 4px;">' + email + 'is not valid</div>'
  )
  .dialog({
    modal: true,
    resizable: false,
    height: 200px,
    width: 300px,
    title: "Invalid Email",
    buttons: {
        "OK": function () {
            apex.jQuery(this).dialog("close");
        }
    }
  });
}

catch mouse current coordination (x,y)

"x: " + this.browserEvent.clientX + "y: " + this.browserEvent.clientY;

detect which mouse button is clicked

// left button
this.browserEvent.button == 0;
// middle button
this.browserEvent.button == 1;
// right button
this.browserEvent.button == 2;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment