You are on page 1of 87

APEX Cheat Sheet

Karen Cannell

kcannell@integratc.com

1 www.odtug.com
About Me …(Speaker Qualifications)

Karen Cannell ~ Principal


w Analyzed, designed developed, converted, upgraded, enhanced
and otherwise worked on database applications for 20+ years,
focused on Oracle since 1994. Currently migrating business
processes from paper to web-based systems, leveraging the
Oracle 9i and 10g suite of tools. Lately APEX, Discoverer
upgrades.

VOLUNTEER for IOUG, ODTUG, Oracle Magazine.


ODTUG Journal Sr. Technical Editor
Volunteer to author ODTUG Journal Articles!!

w Using APEX since the HTMLDB beginning

Cannell
2 www.odtug.com
About You … (Audience Background)

New to APEX?
APEX Experience?
Previous Tools?

APEX Training?
Version 3.1?

Cannell
3 www.odtug.com
Session Objectives

NOT
NOTan anAPEX
Advanced
How-ToAPEXSession
Session
… …
Learn APEX syntax; Know what to use
when and where
Learn common tricks to customize APEX to
your needs
Accelerate your APEX learning curve

Cannell
4 www.odtug.com
Why an APEX Cheat Sheet?

APEX is a blend of technologies …


A “Rapid Application Development” tool
Lots of syntax .. Syntax sensitive ..
Lots of wizards …Wizards are OK, defaults are OK, but we need more …
Mix of languages: HTML, PL/SQL, JavaScript, XML ..

We don’t intuitively know everything!

Cannell
5 www.odtug.com
What we’ll Cover:
f?p syntax Validation
Learning from APEX APEX
Case JavaScript
Referencing Items Report tips
& . #...# V and NV Edit Link
Item Types, Positions, Defaults Embed Link in Query
Layout Control Popup Windows
Page Region Sending Mail
Region Templates Custom Authentication
Alignment References
Customizations

6
Cannell
www.odtug.com
APEX 30-second Intro

Part of Database – Fully Supported


Browser-based – no client install
HTTP Server
Rapid Web Development Tool
Declarative Programming
Wizards Wizards Wizards
All Applications are HTML - no applet downloads

Cannell
7 www.odtug.com
Application Builder
SQL Workshop
Utilities

8
Cannell
www.odtug.com
APEX Demonstration

Quick APEX Demo ?

9 www.odtug.com
f?p Syntax
URL Reference to an APEX page
All APEX pages are f?p calls
Two Parts:
Path to Server:
http://<hostname>:<port>/apex/pls/...

Call to PL/SQL Procedure F with Parameter P


…/f?p=<AppId:Page:Session:Request:Debug:ClearCache:
Item Name:Value(s):PrinterFriendly>

Cannell
10 www.odtug.com
http://hostname:port/pls/ape
f?p URL x/

f?p=4000:4001:<session
id>::NO:4001:FB_FLOW_ID:700

11
Cannell
www.odtug.com
f?p Part 1: Address to APEX Server

http://<hostname>:<port>/pls/apex/...

http:// hostname Host name


:port Port number, usually 7777, 7778,
7779
/pls Path to modplsql
/apex Name of the PL/SQL DAD used in
APEX configuration. Apex is the
default per configuration guide

Cannell
12 www.odtug.com
f?p Part 2: F Call and P Parameter

F is an APEX PL/SQL procedure that renders


APEX pages
Single parameter P
P is a concatenation of 9 arguments:
AppId :Page :Session :Request :Debug :
ClearCache :Items :Values :PrinterFriendly

f?p=4000:4001:<session id>::NO:4001:FB_FLOW_ID:700

Cannell
13 www.odtug.com
f?p: parameter P args
Argument Definition Usage
App Id Application identifier or alias APP_ID, APP_ALIAS

Page Page number or alias Page number or alias, or use


APP_PAGE_ID to reference
the current page

Session Session identifier APP_SESSION or SESSION

Request HTTP page request The actual request: GO SAVE,


ADD.
Debug Debug flag YES, NO or DEBUG

Clear Cache Clear cache for the listed page(s) Page numbers or page alias, or
APP to clear all

Item Name(s) Item names to pass to the page P1_ITEM, P2_ITEM

Item Value(s) Item values to correspond to item :P1_ITEM, :P2_ITEM or


names &P1_ITEM. &P2_ITEM.

Printer Friendly Printer Friendly flag YES, NO or


PRINTER_FRIENDLY
Cannell
14 www.odtug.com
f?p P Parameter String

Arguments Must be In Order


Colon : to Separate Arguments
Can Omit Arguments – will Default
Use : to Maintain Position
If All Remaining Args are Default, can Omit
f?p=550::::::

Cannell
15 www.odtug.com
P = string of arguments

f?p=
AppId : Page : Session : Request :
Debug : ClearCache : ItemNames : Values : PrinterFriendly

All Programmers Smartly Request


Double Cash In Virtual Programs

Cannell
16 www.odtug.com
f?p Examples

Part of a URL:
http://tunahuntress.tunahunter.com:7777/pls/apex/f?p=710:6:361726
3641284922::NO::P6_COMMITTEE_CODE:459

Translates to:
Render page 6 of application 710 for the current session, no debug,
using 459 as the value of P6_COMMITTEE_CODE

and looks like …

Cannell
17 www.odtug.com
Cannell
18 www.odtug.com
f?p Example – APEX URL Target

In HTML, as in an APEX URL Target:


<a href=
”f?p=(&APP_ID.:12:&APP_SESSION.::NO::P12_COMMITTEE_C
ODE, P12_COMMITTEE_NAME:&P10_COMMITTEE_CODE.,
&P10_COMMITTEE_NAME.)”>

A link to the current app, page 12, the current session, no debug,
setting p12_committee_code and p12_committee_name with the
values of p10_committee_code and p10_committee_name.

Cannell
19 www.odtug.com
f?p Example – APEX URL Target

Javascript PopupURL

javascript:popupURL('f?p=&APP_ID.:46:
&APP_SESSION.::NO::P46_MEMBERID:
&P44_MEMBERID.');

Cannell
20 www.odtug.com
Learn APEX from APEX

APEX Import Utility


Import <APEX install dir>/builder/f4000.sql
(f4050.sql,f4155.sql,f4300.sql,f4411.sql …)

On Install:
Renumber App
Change Alias
Inspect Only (Cannot run)

21 www.odtug.com
About CASE … APEX is picky

APEX passwords ARE case sensitive!


APEX item names work in either case, uppercase or
lower.
Reference to substitution strings should be in
UPPERCASE
:APP_ID &SESSION. PRINTER_FRIENDLY.
Button name references should match case

Cannell
22 www.odtug.com
Referencing Items – 6 ways!

Bind Variable :P1_ITEM


Substitution String &P1_ITEM.
PL/SQL value function V(‘P1_ITEM’) or
NV(‘P1_ITEM’)
Direct PL/SQL
APEX_APPLICATION.G_FLOW_ID
Template Substitution
#P1_ITEM#
Cannell
23 www.odtug.com
Bind Variable

:P1_ITEM
Within SQL, PL/SQL, Page Processes,
Validations, Computations

Most frequently used

Cannell
24 www.odtug.com
Substitution

&P1_ITEM.
Use in APEX element, f?p and HTML
references

Cannell
25 www.odtug.com
Substitution Syntax - Example

In the Messages Dialog of a Process:

… Demo Email sent to &P2_EMAILADDRESS.

Cannell
26 www.odtug.com
V and NV Functions

V(‘P1_ITEM’) and NV(‘P1_NUMBER’)


Use within PL/SQL modules
Current session only
Do not work in spawned/background jobs

Cannell
27 www.odtug.com
V and NV Caution

Not DETERMINISTIC
Potential Performance Impact
SELECT x,y,z FROM abc WHERE x = V(‘P1_ITEM’)
Executes once for each row of abc
Wrapper for V and NV – Drop-in “replacement”
available from Inside-Apex Blog at:
http://inside-apex.blogspot.com/2006/12/drop-in-replacement-for-
v-and-nv.html

thanks Patrick!
Cannell
28 www.odtug.com
Template Substitution

#P1_ITEM#
Page, Region and Report Templates
Use to customize your own templates
Be aware of #...# to understand APEX
templates, button, nav bar, default region
placement

Cannell
29 www.odtug.com
Template Substitution - Example

<table class="t12FormRegion" id="#REGION_ID#" summary="">


<tr><td class="t12Header">#TITLE#</td></tr>
<tr><td
class="t12ButtonHolder">#CLOSE##PREVIOUS##NEXT##DELETE##EDI
T##CHANGE##CREATE##HELP#</td></tr>
<tr><td class="t12Body">#BODY#</td></tr>
<tr><td><img src="#IMAGE_PREFIX#themes/theme_12/t.gif"
Template Substitution - width="400"
default placement for Region
height="1"></td></tr> Buttons
</table>

Cannell
30 www.odtug.com
Direct

APEX_APPLICATION.G_<name>
Used in PL/SQL, similar to V and NV
Current session only
Do not work in spawned/background jobs

Cannell
31 www.odtug.com
What to Use When

:Bind variables in SQL, Page Processes,


Validations, Computations
&__. Substitution In HTML
V and NV or APEX_APPLICATION in PL/SQL
#TEMPLATE# in page, region, report and other
templates.

Cannell
32 www.odtug.com
Syntax Examples

HTML anchor in Header, Footer or HTML Region:


<a href=”f?p=&APP_ALIAS.:4:&APP_SESSION.>”Click here for Page 4</a>

PL/SQL htf.anchor:
htf.anchor(‘f?p=’||APEX_APPLICATION.G_FLOW_ID||’:4:’||V(‘APP_SESSION’),’Click
for Page 4’);
or
htf.anchor(‘f?p=’||V(‘APP_ID’)||’:4:’||V(‘APP_SESSION’),’Click for Page 4’);

Anchor in a SQL Query:


SELECT ‘<a href=“f?p=’||:APP_ID||’:4:'||:APP_SESSION||’>Click for Page 4</a>’
FROM DUAL;

Cannell
33 www.odtug.com
Item Type, Position and Default

Item Types
Position Options
User Interface Defaults

Know the Defaults ...


Then Know How to Adjust

Cannell
34 www.odtug.com
Item Types –APEX defaults

Cannell
35 www.odtug.com
Some Different Item Types

File Browse
Multi Select
Shuttle
Stop/Start Table
Text Area … w/Counter, Spellcheck, HTML Editor

Cannell
36 www.odtug.com
Advanced Item Types

Cannell
37 www.odtug.com
Item Position Settings

Use the Displayed settings to adjust item


placement relative to other items on the page
New Line Yes
Field Yes
ColSpan 1
RowSpan 1

New Line No
Field No
ColSpan 1
RowSpan 1 Cannell
38 www.odtug.com
Drag and Drop Item Layout

Access from Items element

Cannell
39 www.odtug.com
Drag and Drop Item Interface

Cannell
40 www.odtug.com
User Interface Defaults

Use them! Tedious now or later.


Shared Components Æ User Interface Æ User
Interface Defaults
By Table or View
Defines Defaults for Reports and Forms
Grid or Individual Element Access

Cannell
41 www.odtug.com
User Interface Defaults

Cannell
42 www.odtug.com
Layout Control

Page Region
Region Templates

Know the Defaults ...


Then Know How to Adjust

Cannell
43 www.odtug.com
Page Regions

Will vary slightly


from theme to theme
Region Position
for Sidebar
Note template
Substitution Strings

If you don’t like it …


Edit the Template!

Cannell
paper #447
44 www.odtug.com
Region
Templates

Will vary
slightly from
theme to
theme

Edit as you
like

Cannell
45 www.odtug.com
Region
Templates
alt. theme

Cannell
paper #447
46 www.odtug.com
Forcing Alignment

Regions
Specifically set Height and Width
<table height=”400” width=”400”><tr><td>
and close with
</td></tr></table>

Use a Spacer image to force height or width


<img src="#IMAGE_PREFIX#/1px_trans.gif" width="355"
height="0">

Cannell
47 www.odtug.com
Forcing Region Alignment

Spacer (pixel) image in header, footer, source

Img src=#IMAGE_PREFIX#/1px_trans.gif width =“355” height=“0”>

Cannell
48 www.odtug.com
49
Cannell
www.odtug.com
Forcing Item Alignment

Use the HTML Form Element Attributes field:


Select Lists
style=“width:80;”
Form Fields
style=“width:80;”
Set other Item attributes:
style=”text-transform:uppercase;”
onFocus=”javascript:this.maxlength=30;”

Cannell
50 www.odtug.com
Force Item Alignment, Attributes

Text Area:
Set height, width, a scrollbar and force the data to
Uppercase
style="font-family:Arial;font-size:10px;
height:50px;width:300px;overflow:auto;"
onBlur="javascript:{this.value =
this.value.toUpperCase(); }"

Cannell
51 www.odtug.com
Customizations

Header Logo
Corporate Interface / Look-and-Feel

Cannell
52 www.odtug.com
Header Logo or Text

Application Æ Edit Attributes Æ Definition Æ Logo.

Cannell
53 www.odtug.com
Corporate User Interface

Yes, Is Doable
Best Reference:
Cloning your Corporate UI with HTML DB blog series
at
http://www.orablogs.com/scott/archives/001189.html
Scott Spendolini
My Experience: 1 Hour to “Useable”, 4+ for all
page templates complete with substitution tags
Advice: Go for It … weighing costs, of course.
Cannell
54 www.odtug.com
To Change Default Templates

Application Æ Shared Components Æ Themes


Create/Edit Theme
Here can edit all default templates

Cannell
55 www.odtug.com
Cannell
paper #447
56 www.odtug.com
Validations

APEX
JavaScript

Cannell
57 www.odtug.com
Validations - APEX

Item or Page Level


NOT NULL, String Comp, Reg Exp, SQL,
PL/SQL

Cannell
58 www.odtug.com
Validations - JavaScript

In HTML Form Element Attributes field

Not a JavaScript wiz? Search online …

Cannell
59 www.odtug.com
JavaScript Example - SSN

In HTML Form Element Attributes:


onBlur="javascript:SSNValidation(this.value);“

Function goes in the Page Header, or, in an


include file that contains all the JavaScript for
the page.

Cannell
60 www.odtug.com
JavaScript – function in Header
function SSNValidation(ssn) {
var matchArr = ssn.match(/^(\d{3})-?\d{2}-?\d{4}$/);
var numDashes = ssn.split('-').length - 1;
if (matchArr == null || numDashes == 1) {
alert('Invalid SSN. Must be 9 digits or in the form NNN-
NN-NNNN.');
msg = "does not appear to be valid";
}
else
if (parseInt(matchArr[1],10)==0) {
alert("Invalid SSN: SSN's can't start with 000.");
msg = "does not appear to be valid";
} }

Cannell
61 www.odtug.com
Reports

Edit Link
URL(f?p) in SQL Query
Interactive Reports (3.1)

Cannell
62 www.odtug.com
Report – Edit Link

Report Region Æ Region Attributes Æ Column Link

Cannell
63 www.odtug.com
Embed Link in SQL Query - example

Embed a link in SQL Query


SELECT ‘http://www.ioug.org/open_link.cfm?’
OPEN, MEMBER_ID, LAST_NAME, …

SELECT “f?p( …)” PLINK, MEMBER_ID,


LAST_NAME, …

The link is now part of your report. Too Simple!

Cannell
64 www.odtug.com
Interactive Reports

Includes a Select Bar and an Action Bar


Settings are Options in Report Attributes

Cannell
65 www.odtug.com
Action
Select Bar
Dropdown

Link to
Single Row
Page

Custom
Edit Button
(in Query,
not part of
IR)

Cannell
66 www.odtug.com
Interactive Reports - Limitations

Single Template
Customize by CSS only (no template edits)
One IR per page
Cannot be an Updateable Report

Cannell
67 www.odtug.com
Popup Options

APEX JavaScript built-in popupURL


javascript:popupURL('f?p=&APP_ID.:46:&SESSION.::
NO::P46_MEMBERID:&P44_MEMBERID.');

Build Custom Popup Page


OTN APEX How-To Build Custom Popup Pages
http://www.oracle.com/technology/products/database/
application_express/howtos/how_to_create_custom
_popups.html

Cannell
68 www.odtug.com
Build Custom Popup - steps

Build Calling Form Page


Build Popup Page
Add JavaScript to call the Popup Page,
passing values as needed
Add Popup Link or Button
Add JavaScript to Popup Page to pass values
back
See the How-To for
Syntax details!
Cannell
69 www.odtug.com
Sending Email

Configure Email Server Settings


HTMLDB_MAIL package
HMTLDB_MAIL.SEND
HTMLDB_MAIL.PUSH_QUEUE

Cannell
70 www.odtug.com
Environment Settings Æ SMTP Mail Server

Set by the APEX Administrator for the


installation

Cannell
71 www.odtug.com
APEX_MAIL.SEND

APEX_MAIL.SEND (
p_to IN VARCHAR2,
p_from IN VARCHAR2,
p_body IN VARCHAR2 | CLOB,
p_body_html IN VARCHAR2 | CLOB,
p_subj IN VARCHAR2 DEFAULT NULL,
p_cc IN VARCHAR2 DEFAULT NULL,
p_bcc IN VARCHAR2 DEFAULT NULL);

Cannell
72 www.odtug.com
APEX_MAIL.SEND - Function

v_mail_id := APEX_MAIL.SEND (
p_to IN VARCHAR2,
p_from IN VARCHAR2,
p_body IN VARCHAR2 | CLOB,
p_body_html IN VARCHAR2 | CLOB,
p_subj IN VARCHAR2 DEFAULT NULL,
p_cc IN VARCHAR2 DEFAULT NULL,
p_bcc IN VARCHAR2 DEFAULT NULL);

Cannell
73 www.odtug.com
APEX_MAIL - Example
DECLARE
l_body_html varchar2(4000);
lv_tolist varchar2( 2000) := ‘joedba@yourcompany.com’;
BEGIN
l_body_html := '<p> ‘||
‘To view matching KALEIDOSCOPE Contacts click the link: </p>'||
'<a href="http://tunahuntress.tunahunter.com:7777’||
'/f?p=550:33:::::’||'P33_REPORT_SEARCH:'||:P1_LAST||
'">View KALEIDOSCOPE Contacts</a></p>';
APEX_MAIL.SEND(
P_TO => lv_tolist,
P_FROM => :P1_EMAIL,
P_BODY => l_body_html,
P_BODY_HTML => l_body_html,
P_SUBJ => 'New Mail Message from APEX’;
END;
Cannell
74 www.odtug.com
APEX_MAIL.PUSH_QUEUE

Mail Queue “Sent” – Pushed – every 10 min.


PUSH_QUEUE to send immediately

APEX_MAIL.PUSH_QUEUE(
p_smtp_hostname IN VARCHAR2 DEFAULT,
p_smtp_portno IN NUMBER DEFAULT);

APEX_MAIL.PUSH_QUEUE;

Useful for Debugging –


No Wait Cannell
75 www.odtug.com
APEX Mail Attachments

Not possible via APEX_MAIL til APEX 3.1


Can do via UTL_SMTP or Java Stored
Procedure
See OTN APEX Forum thread
http://forums.oracle.com/forums/thread.jspa?message
ID=1383936

Cannell
76 www.odtug.com
APEX_MAIL.ADD_ATTACHMENT
APEX_MAIL.ADD_ATTACHMENT(
p_mail_id IN NUMBER,
p_attachment IN BLOB,
p_filename IN VARCHAR2,
p_mime_type IN VARCHAR2);
Use function version of APEX_MAIL.SEND to return a
mail id.
Query filename from APEX_APPLICATION_FILES
Add multiple attachments by multiple
ADD_ATTACHMENT calls

Cannell
77 www.odtug.com
APEX Mail Attachments - Example
DECLARE
l_id number;
BEGIN
l_id := APEX_MAIL.SEND(
p_to => 'fred@flintstone.com',
p_from => 'barney@rubble.com',
p_subj => 'APEX_MAIL with attachment',
p_body => 'Please review the attachment.',
p_body_html => '<b>Please</b> review the attachment' );
FOR c1 IN (SELECT filename, blob_content, mime_type FROM
APEX_APPLICATION_FILES WHERE ID IN (123,456)) loop
--
APEX_MAIL.ADD_ATTACHMENT( p_mail_id => l_id, p_attachment =>
c1.blob_content, p_filename => c1.filename, p_mime_type =>
c1.mime_type);
END LOOP;
COMMIT;
END;

Example from APEX User Guide Cannell


78 www.odtug.com
Custom Authentication

Custom Authentication Wizard


Most defaults are OK
Page Sentry function (here or redirect?)
Session Verify function (valid session?)
Invalid Session Target (where to go if failed login)
Pre-Authentication (cookies? audits?)
Authentication Process …

Cannell
79 www.odtug.com
Custom Authentication, cont’d

Authentication Process:
Boolean function
2 parameters: Username, Password

return authenticate_kaleidoscope_user;
NO arguments in the call!

Cannell
80 www.odtug.com
Final Words …

SAVE OFTEN
Apply Changes
Business Logic in Packages
Export Periodically (a simple backup!)

Apply Sound Development Practices

Cannell
81 www.odtug.com
APEX Resources

APEX Forum
http://forums.oracle.com/forums/forum.jspa?forumID=137
APEX Studio
http://apex.oracle.com/i/index.html
APEX OTN Technology Center
http://www.oracle.com/technology/products/database/application_e
xpress/index.html
Oracle Metalink
http://metalink.oracle.com

Cannell
82 www.odtug.com
APEX Resources, cont’d
Blogs, Blogs and More Blogs …
Scott Spendolini:
http://spendolini.blogspot.com and the older http://blogs.oracle/com/scott
Carl Backstrom: http://carlback.blogspot.com
APEX Blogs …
John Scott, http://jes.blogs.shellprompt.net/
Dimitri Gielis, http://dgielis.blogspot.com/
Mark Swetz http://marcsewtz.blogspot.com/index.html
Denes Kubicek, http://deneskubicek.blogspot.com/
Dietmar Aust, http://daust.blogspot.com/
Patrick Wolf – Inside APEX. http://inside-apex.blogspot.com and the ApexLib add-on
at http://apexlib.sourceforge.net
and more blogs off of these!
Many thanks to Mike ,Joel ,Raj, David
Cannell
83 www.odtug.com
APEX Resources, cont’d

Textbooks
Oracle HTMLDB Handbook, Develop and Deploy Fast,
Secure Web Applications by Bradley Brown, Lawrence Linnemeyer,
Oracle Press, 2006.
Easy Oracle HTML-DB, Create Dynamic Web Pages with
Oracle by Michael Cunningham and Kent Crotty, Rampant Press,
2006.
Pro Application Express by John Scott, Scott
Spendolini Coming Soon!

Cannell
84 www.odtug.com
Share Your Knowledge !
Call for Articles/Reviewers

ODTUG Journal IOUG SELECT


Journal

85
Cannell
www.odtug.com
Questions ?

APEX Cheat Sheet

Karen Cannell
kcannell@integratc.com

86 www.odtug.com
Thank You ~

APEX Cheat
Sheet

Karen Cannell
kcannell@integratc.com

87 www.odtug.com

You might also like