Вы находитесь на странице: 1из 25

TYPES

Simple type definition


- TYPES type.
- TYPES type(len).
Definition of a structured type
- TYPES: BEGIN OF structype
...
END OF structype.
Definition of an internal table type
- TYPES itabtype {TYPE tabin! OF linetype"
#I$E tabin! OF line%b&'
()IT* (+NI,+E"NON-+NI,+E. ey!e/. (INITI0# SI1E n..
- TYPES itabtype {TYPE 20NGE OF type"TYPES itabtype #I$E 20NGE OF
/'.
- TYPES itabtype {TYPE linetype"#I$E line%b&' O33+2S n.
Type Description DL Initial value
3 Character 1 Space
N Numeric text 1 '00...0'
D Date YYYYMMDD '00000000'
T Time !!MMSS " '000000'
4 #yte $he%a&ecimal' 1 %'00'
I Inte(er ) 0
P *ac+e& num,er 0
F -loatin( point num,er '0.0'
ST2ING Strin( varia,le empty strin(
4ST2ING #yte se.uence $% strin(' varia,le empty % strin(
The system field SY-DB3NT contains the number of lines read so far ecah time the
SE#E3T statement is executed. After ENDSE#E3T, SY-DB3NT contains the total
number of records read.
The Return Code is set as follows:
SY/S0#1C 2 0:
The result table contains at least one record.
SY/S0#1C 2 ):
The result table is empty.
SY/S0#1C 2 :
Applies only to SE#E3T SING#E FO2 +PD0TE: You did not specify all of the primary key
fields in the )*E2E condition. The result table is empty.
Dictionary Objects
DDIC Object Naming Standard Repository
Entry
Meta Data
Table
Domain !
Data "lement !
#ock $b%ect "!
&earch 'elp &'!
&tructure &!
Table ! or T!
Table Type ! or TY!
Table (ndeces )) * character uni+ue name
Type ,roup T,!
-iew -!
Sample Program Types
R - Report
D - Data Load
I - Include
X - Interface
etc.
SAP Object Naming Standard Repository Entry Meta Data
Table
Application Log Z
Area !enu Z
"atc# Input Session Z
$#ange Document Z
Pac%age Z
Dialog !odule Z
$!&D 'n#ancement
Pro(ect
Z
)orm Pool
SAP)Z
*enerated Include modules
s#ould follo+ SAP suggested
recommendations
)or ',ample
)ZT&P - Data declarations
)Z)-- - Subroutines

)unction *roups Z
)unction !odules Z.
*ui Status Z
Include ZI
!emory ID Z!
!essage $lass Z
!essages /umber numeric 0 digits 1-222
!odule Pool
SAP!Z
*enerated Include modules
s#ould follo+ SAP suggested
recommendations
)or ',ample
!ZT&P - Data declarations
!Z&-- - P"& modules
!ZI-- - PAI modules
!Z)-- - Subroutines

/umber Range Z/R
Program Ztype +#ere type identifies t#e program
function

Screen 3 c#aracters
*4I Title Z
Transaction $ode Z
Naming Conventions
/aming $on5entions lend consistency and readabilty to your code. T#is in turn impro5es t#e o5erall maintainability of
code. 6#en naming your ob(ects and 5ariables7 try to a5oid arcane acronyms. Spell it out +#ere possible.
Refer to t#e table belo+ for accepted industry standard naming con5entions for ob(ects and 5ariables +it#in a program.
Program nternal Objects Convention E!ample
Class " local lcl. lcl.salesorder
Class " nter#ace lif. lif.salesorder
Constant c. or co. c.open.indicator
Data Re#erence dr. dr.openorder
Data $ariables " %lobal 5. or g5. 5.order
Data $ariables " &ocal l5. l5.order
Data $ariables " Static ls5. ls5.order
'ield Symbols 8fs.9 8fs.order9
nternal Table " Standard t. t.orders
nternal Table " Sorted ts. or to. ts.orders
nternal Table " (as)ed t#. t#.orders
Parameters pa. pa.order
Ranges ra. ra.orders
Re#erence $ariable rf. rf.orders
Select"Options so. so.date
Str*ct*re st. or +. st.orders
Table Control tc. tc.orderlines
Types ty. ty.ordertype
+or, Area +. +.order
nter#ace Parameters
S*bro*tine 'ormal Parameters Convention E!ample
-sing u. or p.
C)anging c. or p.
Raising r. or p.

Met)od Signat*re Convention E!ample
mporting im.
E!porting e,.
C)anging c#.
Ret*rning re.
Raising c,. for e,ception classes
E!ceptions not applicable

'*nction nter#ace Convention E!ample
mporting im.
E!porting e,.
C)anging c#.
Tables t.
E!ceptions not applicable
Raising not applicable
'*nctions.Met)ods
)4/$TI&/S
4se t#e built in documentation features found in S'0: to document t#e purpose of t#e function modules and t#e usage
of t#e Interface parameters.
T#e use of naming standards for interface parameters is not a common practice7 alt#oug# it s#ould be so SAPdomain
recommends t#e follo+ing;
Importing Parameters; I!.
',porting Parameters; 'X.
$#anging Parameters; $<.
Tables Parameters; T".
!'T<&DS
)or !et#ods 4se t#e )ollo+ing naming standard for t#e signature of t#e met#od.
Importing; I!.
',porting; 'X.
$#anging ; $<.
Returning; R'.
Raising; e,ception =no std>
',ceptions; e,ception.class =no std>
Events
List Processing '5ents are e,ecuted in a predefined order according to t#e rules of t#e A"AP interpreter and t#erefore
t#e order in +#ic# t#ey appear in an A"AP program is not important. <o+e5er t#ere is a standard practice among
programmers =for readability> to order e5ents according to t#e order of e,ecution.
It is good practice to identify e5ents +it# a comment banner. i.e.
List Processing '5ents
%*idelines by Topic
Error Handling
.unctions
/ethods
/essa0e Types 1 A " 2 (
3 &
Return Code Checkin0
&4#
General
(ncomplete Code
(ncorrect 5sa0e
/ulti1lin0ual
$bsolete A6A7
&tatements ?9&bsolete
$ther
Maintainability
Cle8er codin0
Code "ncapsulation
'ard Codin0
/odulari9ation
&loppy
&tandards
Perormance
(nternal Table
&4#
$ther
Processing
(nternal Tables
$utput
&electin0 Data
5pdates
Stability
Address and /emory
7ointers
Dynamic 7ro0rammin0
'andlin0 7ro0ram
Dumps
Error (andling
E!ception (andling / C)ec,ing Ret*rn Codes
%*ideline0 Al+ays $#ec% Return $ode after critical operations @ SAL7 ITA" &perations7 )unction $alls7 etc.
E!planation0 ?t#is code +ill ne5er fail?7 ?it +ill al+ays return data?
<a5e you #eard t#is before B
T#is guideline is t#e single most o5erloo%ed coding practice by de5elopers but yet one of t#e most
critical tec#niCues a5ailable t#at ensures fail safe programming t#at a5oids potential failure points.
/e5er ma%e an assumption t#at a piece of code +ill al+ays +or%. "y putting in proper subrc c#ec%ing7
your program is assured t#at it can at least #andle erroneous situations. )ail safe programming is
critical to Cuality applications.
T#e programmer s#ould al+ays7 at minimum7 put in a generic Dcatc#-allD c#ec% t#at +ill catc# any failed
condition. !ore specific c#ec%s can al+ays be added.
Proper message issuing and e,ception processing ma%es for a program t#at is easy to support and
maintain. It is a %ey component of Cuality programming. It is also a tell tale sign of t#e s%ill7 e,perience7
and attitude of a de5eloper to+ards Cuality programming.
Snippet0
a3ter any S4L5 IT6# 7perations5 -unction Calls5 etc.
I- sy/su,rc 2 0.
co&e.
8lse.
9 no co&e
8NDI-.
71
I- sy/su,rc 2 0.
co&e 3or success3ul operation only
8NDI-.
9 *ro(rammer shoul& al:ays co&e 3or the 3aile&
con&ition. In this case5 they &i& not co&e 3or a non ;ero
su,rc.
a3ter any S4L5 IT6# 7perations5 -unction Calls5 etc.
I- sy/su,rc 2 0.
co&e.
8lse.
co&e.
8NDI-.
7r
I- sy/su,rc <= 0.
co&e.
8NDI-.
71
>hen no action is re.uire& a3ter a non ;ero co&e5 then
simply a&& a comment. This at least in&icates that the pro(rammer (ave
some thou(ht to the outcome o3 a 3aile& con&ition :hich in some cases is
7? to continue processin(.
Sample comment@
9 non ;ero return co&e is 7?. Continue processin(
E!ception (andling " a#ter #*nction call
%*ideline0 *eneric !essaging after a function call is not recommended.
E!planation0 Anytime you use t#e PATT'R/ function in t#e +or%benc# to insert a function7 it automatically inserts a
generic message in t#e format
E!'SSA*' ID sy-msgid TFP' sy-msgty /4!"'R sy-msgno 6IT< sy-msg51 sy-msg5G7 sy-msg507 sy-
msg53H.
T#is statement assumes t#at SF 5ariables #a5e been filled +it# meaningful 5alues by t#e function itself.
<o+e5er7 often times7 t#is is not t#e case.
T#e generic message s#ould only be used in t#e case +#ere t#e function and all nested functions t#at
are called use t#e !essage...Raising format consistently t#roug#out t#e function c#ain. T#e
!essage...Raising format sets t#e sy-message and ot#er sy 5ariables. &nly t#en7 can you be assured
t#at a generic message statement can be used to present a proper error message. <o+e5er7 since t#is
situation is most li%ely not t#e case7 a good de5eloper +ill #andle t#e message t#emsel5es and not
count on t#e generic form to pro5ide a meaningful message rele5ant to t#e situation.
Snippet0
I- sy/su,rc <= 0.
M8SS6A8 ID sy/ms(i& TY*8 sy/ms(ty
N0M#81 sy/ms(no
>IT! sy/ms(v1 sy/ms(vB sy/ms(vC sy/ms(v).
8NDI-.
C6LL -0NCTI7N 'DEM6T81I6LEFI8>'
8%*71TINA
imE3ullE&etails 2 '%'
imEtEmaterial 2 tEmaterials
8%C8*TI7NS
invali&Einput 2 1
invali&Ematerial 2 B
noEmaterials 2 C
7T!81S 2 ).

C6S8 sy/su,rc.
>!8N 1.
M8SS6A8 eGGG >IT! 'Invali& Input'$101'.
>!8N B.
M8SS6A8 eGGG >IT! 'Invali& Material'$10B'.
>!8N C.
M8SS6A8 eGGG >IT! 'No Materials'$10C'.
>!8N ).
M8SS6A8 eGGG >IT! '7ther 1easons'$10)'.
8NDC6S8.
Message Processing
%*ideline0 4se t#e correct message type A7'7X767I7S to determine t#e e,pected outcome
E!planation0 6#en issuing error messages7 t#e flo+ of t#e program may ta%e different pat#s based on t#e message
type and t#e conte,t in +#ic# t#e message is issued.
SAP online <elp pro5ides a good e,planantion of t#e message types and t#e program reaction
depending +#ere in a processing bloc% t#e message is issued.
)or ',ample
- An DSD message reacts differently in an AT e5ent t#an it does +#en issued in a )&R!.
- An D'D !essage in t#e At Selection Screen '5ent +ill reCuire t#e user to re-enter t#e input +#ereas a
6 message is simply a +arning and t#e user only needs to press '/T'R to bypass t#e +arning
condition so t#at program processing can resume.
Suggestion; !essages in all e5ents need to be e5aluated to determine if t#ere is a reason to stop t#e
program +it# an ' type message or #a5e it continue processing.
',ecutable programs s#ould al+ays allo+ t#e user t#e option to e,it or correct an error situation7 for
e,ample in a selection Screen. T#atIs +#y ' and 6 messages need to be used +it# %no+ledge of t#e
outcome. T'ST t#e situation t#oroug#ly if you are unsure and t#en T'ST again e5en if you are sure.
In "ac%ground Jobs7 +#en a program is e,ecuted in t#e bac%ground7 a message issued of type K'I7 KAI
or KXI +ill cancel t#e e,ecution of t#e (ob. T#e (ob log is a #andy feature to determine status of a (ob.
!essages issued in bac%ground s#o+ up in t#e (ob log so be sure to pro5ide meaningful messages t#at
+ill easily identify t#e point of failure and course of action reCuired.
If an online program is e,ecuted in bac%ground7 t#e messaging can be bypassed by first c#ec%ing to
see if it is running in bac%ground by e5aluating t#e sy-batc# 5ariable. If t#e program is running in
bac%ground7 t#en do not issue error messages t#at +ould typically be used to interact +it# t#e user.
)or &nline Selection Screen type programs - use t#e 6rite statement to issue t#e message to output
and t#en issue a type S message and t#en a Lea5e List-Processing statement s#ould follo+ t#is. T#is
returns t#e user bac% to t#e selection screen.
Snippet0
un&er construction
'*nctions
E!ception (andling " a#ter #*nction call
%*ideline0 *eneric !essaging after a function call is not recommended.
E!planation0 Anytime you use t#e PATT'R/ function in t#e +or%benc# to insert a function7 it automatically inserts a
generic message in t#e format
E!'SSA*' ID sy-msgid TFP' sy-msgty /4!"'R sy-msgno 6IT< sy-msg51 sy-msg5G7 sy-msg507 sy-
msg53H.
T#is statement assumes t#at SF 5ariables #a5e been filled +it# meaningful 5alues by t#e function itself.
<o+e5er7 often times7 t#is is not t#e case.
T#e generic message s#ould only be used in t#e case +#ere t#e function and all nested functions t#at
are called use t#e !essage...Raising format consistently t#roug#out t#e function c#ain. T#e
!essage...Raising format sets t#e sy-message and ot#er sy 5ariables. &nly t#en7 can you be assured
t#at a generic message statement can be used to present a proper error message. <o+e5er7 since t#is
situation is most li%ely not t#e case7 a good de5eloper +ill #andle t#e message t#emsel5es and not
count on t#e generic form to pro5ide a meaningful message rele5ant to t#e situation.
Snippet0
I- sy/su,rc <= 0.
M8SS6A8 ID sy/ms(i& TY*8 sy/ms(ty
N0M#81 sy/ms(no
>IT! sy/ms(v1 sy/ms(vB sy/ms(vC sy/ms(v).
8NDI-.
C6LL -0NCTI7N 'DEM6T81I6LEFI8>'
8%*71TINA
imE3ullE&etails 2 '%'
imEtEmaterial 2 tEmaterials
8%C8*TI7NS
invali&Einput 2 1
invali&Ematerial 2 B
noEmaterials 2 C
7T!81S 2 ).

C6S8 sy/su,rc.
>!8N 1.
M8SS6A8 eGGG >IT! 'Invali& Input'$101'.
>!8N B.
M8SS6A8 eGGG >IT! 'Invali& Material'$10B'.
>!8N C.
M8SS6A8 eGGG >IT! 'No Materials'$10C'.
>!8N ).
M8SS6A8 eGGG >IT! '7ther 1easons'$10)'.
8NDC6S8.
Message Types " A E 1 + S
Message Processing
%*ideline0 4se t#e correct message type A7'7X767I7S to determine t#e e,pected outcome
E!planation0 6#en issuing error messages7 t#e flo+ of t#e program may ta%e different pat#s based on t#e message
type and t#e conte,t in +#ic# t#e message is issued.
SAP online <elp pro5ides a good e,planantion of t#e message types and t#e program reaction
depending +#ere in a processing bloc% t#e message is issued.
)or ',ample
- An DSD message reacts differently in an AT e5ent t#an it does +#en issued in a )&R!.
- An D'D !essage in t#e At Selection Screen '5ent +ill reCuire t#e user to re-enter t#e input +#ereas a
6 message is simply a +arning and t#e user only needs to press '/T'R to bypass t#e +arning
condition so t#at program processing can resume.
Suggestion; !essages in all e5ents need to be e5aluated to determine if t#ere is a reason to stop t#e
program +it# an ' type message or #a5e it continue processing.
',ecutable programs s#ould al+ays allo+ t#e user t#e option to e,it or correct an error situation7 for
e,ample in a selection Screen. T#atIs +#y ' and 6 messages need to be used +it# %no+ledge of t#e
outcome. T'ST t#e situation t#oroug#ly if you are unsure and t#en T'ST again e5en if you are sure.
In "ac%ground Jobs7 +#en a program is e,ecuted in t#e bac%ground7 a message issued of type K'I7 KAI
or KXI +ill cancel t#e e,ecution of t#e (ob. T#e (ob log is a #andy feature to determine status of a (ob.
!essages issued in bac%ground s#o+ up in t#e (ob log so be sure to pro5ide meaningful messages t#at
+ill easily identify t#e point of failure and course of action reCuired.
If an online program is e,ecuted in bac%ground7 t#e messaging can be bypassed by first c#ec%ing to
see if it is running in bac%ground by e5aluating t#e sy-batc# 5ariable. If t#e program is running in
bac%ground7 t#en do not issue error messages t#at +ould typically be used to interact +it# t#e user.
)or &nline Selection Screen type programs - use t#e 6rite statement to issue t#e message to output
and t#en issue a type S message and t#en a Lea5e List-Processing statement s#ould follo+ t#is. T#is
returns t#e user bac% to t#e selection screen.
Ret*rn Code C)ec,ing
E!ception (andling / C)ec,ing Ret*rn Codes
%*ideline0 Al+ays $#ec% Return $ode after critical operations @ SAL7 ITA" &perations7 )unction $alls7 etc.
E!planation0 ?t#is code +ill ne5er fail?7 ?it +ill al+ays return data?
<a5e you #eard t#is before B
T#is guideline is t#e single most o5erloo%ed coding practice by de5elopers but yet one of t#e most
critical tec#niCues a5ailable t#at ensures fail safe programming t#at a5oids potential failure points.
/e5er ma%e an assumption t#at a piece of code +ill al+ays +or%. "y putting in proper subrc c#ec%ing7
your program is assured t#at it can at least #andle erroneous situations. )ail safe programming is
critical to Cuality applications.
T#e programmer s#ould al+ays7 at minimum7 put in a generic Dcatc#-allD c#ec% t#at +ill catc# any failed
condition. !ore specific c#ec%s can al+ays be added.
Proper message issuing and e,ception processing ma%es for a program t#at is easy to support and
maintain. It is a %ey component of Cuality programming. It is also a tell tale sign of t#e s%ill7 e,perience7
and attitude of a de5eloper to+ards Cuality programming.
Snippet0
a3ter any S4L5 IT6# 7perations5 -unction Calls5 etc.
I- sy/su,rc 2 0.
co&e.
8lse.
9 no co&e
8NDI-.
71
I- sy/su,rc 2 0.
co&e 3or success3ul operation only
8NDI-.
9 *ro(rammer shoul& al:ays co&e 3or the 3aile&
con&ition. In this case5 they &i& not co&e 3or a non ;ero
su,rc.
a3ter any S4L5 IT6# 7perations5 -unction Calls5 etc.
I- sy/su,rc 2 0.
co&e.
8lse.
co&e.
8NDI-.
7r
I- sy/su,rc <= 0.
co&e.
8NDI-.
71
>hen no action is re.uire& a3ter a non ;ero co&e5 then
simply a&& a comment. This at least in&icates that the pro(rammer (ave
some thou(ht to the outcome o3 a 3aile& con&ition :hich in some cases is
7? to continue processin(.
Sample comment@
9 non ;ero return co&e is 7?. Continue processin(
Per#ormance
Performance desc
SE&ECT vs2 $E+
%*ideline0 4se 5ie+s +#ere possible o5er multiple table selects
E!planation0 LI'6s can ma%e programs more readable.
LI'6 impro5e performance.
Lie+s can be buffered.
4se SAP standard LI'6s instead of creating ne+ LI'6s if possible.
nternal Tables " Reading a Standard Table
%*ideline0 6#en using R'AD on a standard table7 al+ays perform a Sort by and Delete ad(acent Duplicates
before performing t#e R'AD and al+ays use t#e "inary Searc# option i.e. R'AD itab +it# M'F..."inary
Searc#
E!planation0 Table s#ould be sorted by sorting %eys to ta%e ad5antage of "inary Searc#. If you sort by one %ey and
Read +it# a different set7 you could miss data.
Internal Tables s#ould al+ays be sorted before using D'L'T' ADJA$'/T D4PLI$AT'S. Delete
Ad(acent Duplicates s#ould al+ays be e,plicit by using t#e $&!PARI/* $lause7 e5en if t#ere is only
one field in t#e itab.
6#en sorting internal tables7 al+ays use e,plicit sort %eys i.e. use ?S&RT "F Mey1...n?7 ne5er (ust
?S&RT? on itDs o+n. /ot follo+ing t#is practice could lead to une,pected situations if t#e structure of t#e
table c#anges.
Meeping t#e ?S&RT itab "F? statement as close as possible to t#e R'AD itab +it# M'F..."inary Searc#
is a good coding practice.
Snippet0
S71T TED7C0M8NT.
D8L8T8 6DH6C8NT D0*LIC6T8S -17M TED7C0M8NT.
S71T TED7C0M8NT #Y F#8LN F#8L*.
D8L8T8 6DH6C8NT D0*LIC6T8S -17M TED7C0M8NT C7M*61INA F#8LN F#8L*.
%eneral
Constants
%*ideline0 4se Mey+ord $&/STA/T for 5ariables t#at do not c#ange rat#er t#an using t#e DATA Mey+ord
E!planation0 Defining a 5ariable as a constant pre5ents anyone from c#anging t#e 5alue of t#e constant especially
+#en t#e program gets maintained in t#e future. T#is guarantees t#at t#e 5alue can ne5er be c#anged
and potentially alter t#e outcome of t#e program.
Snippet0
D6T6@ vEs:itch$1' type c value I%J
C7NST6NTS@ vEs:itch$1' TY*8 C value K%L.
Data Objects " -se o# Type
%*ideline0 A5oid using t#e %ey+ord Dli%eD +#en typing a data ob(ect
E!planation0 Data Definitions s#ould use t#e KTFP'I %ey+ord to define itIs data type. LIM' is an old con5ention t#at is
no longer encouraged and only e,ists for bac%+ards compatibility. 6#en programming in A"AP &&7
LIM' is not allo+ed +#en referencing dictionary ob(ects.
Snippet0
D6T6@ FE71D81 LI?8 F#6?/F#8LN.
D6T6@ FE71D81 TY*8 F#6?/F#8LN.
On C)ange o#
%*ideline0 A5oid using &n $#ange &f statement for "rea% processing
E!planation0 )or Internal table Loop processing7 brea% le5el processing s#ould be programmed +it# t#e AT....'/DAT
statements. Do not use &/ $<A/*' &) because it is specific to a single field. 4npredictable results
can occur +it# t#e &/ $<A/*' &)7 +#ereas t#e AT... '/DAT triggers a brea% +#en any c#ange
occurs in t#e specified field or fields left of t#e specified field.
Snippet0
7N C!6NA8 7-.
6TM8ND6T.
Case vers*s '
%*ideline0 )or !ultiple conditions7 use $AS'
E!planation0 4se t#e $AS' statement rat#er t#an I)N'LS'I)N'/DI) statement +#en t#ere are multiple conditions. If
t#ere is only one condition7 t#en t#e I)N'LS'N'/DI) statement is a good c#oice.
Snippet0
I- SY/S0#1C 2 1.
co&e 1.
8LS8I- SY/S0#1C 2 B.
co&e B.
8LS8.
co&e others.
8NDI-.
C6S8 SY/S0#1C.
>!8N 1.
co&e 1.
>!8N B.
co&e B.
>!8N 7T!81S.
co&e others.
8NDC6S8.
nterr*pt Commands / C(EC34 E1T4 STOP4 &EA$E4 RET-RN
%*ideline0 Al+ays Test positi5e and negati5e conditions for interrupt commands to ensure t#e code branc#es do+n
t#e intended pat#
E!planation0 Interrupt commands react according to t#e rules of t#e command in con(unction +it# t#e type of code
t#at encapsulates t#em. It is #ea5ily suggested to consult your #elp guides on t#e statement used and
t#e placement of t#e statement as to t#e e,pected outcome +#enNif t#e interrupt command is e,ecuted.
6#en using interrupt type commands suc# as $<'$M7 'XIT7 L'AL'7 t#e programmer s#ould t#oroug#ly
test t#e condition to ensure t#e code branc#es do+n t#e intended pat# to ensure t#at any reCuired
follo+-on processing is actually e,ecuted.
i.e. A failed $<'$M statement in a L&&P7 S'L'$T7 'L'/T7 )&R!7 *'T7 etc. all react differently to t#e
c#ec% $&!!A/D. T#is is also true for t#e 'XIT statement in a L&&P7 S'L'$T7 'L'/T7 )&R!7 *'T7
etc.
$aution; D& /&T place a $<'$M or 'XIT statement in a user e,it. If t#e $<'$M or 'XIT statement is
e,ecuted7 all follo+ing user e,its +ill not be e,ecuted.
In Summary; 4se t#e correct Interrupt command to ensure t#e logic flo+ ta%es t#e pat# e,pected.
Snippet0
un&er construction
'ile Operations " Open
%*ideline0 4se t#e !'SSA*' %ey+ord on t#e &P'/ statement to trap operating system errors
E!planation0 Al+ays include t#e !'SSA*' %ey+ord on t#e &P'/ statement to trap operating system errors. Subrc
only tells if t#e operation +as successful or not. T#e !'SSA*' %ey+ord pro5ides for more specific
application ser5er errors suc# as permissions7 file e,istence7 etc.
Snippet0
D6T6@ FEMSA$100' Type C.
7*8N D6T6S8T FEIN*0TE-IL8 -71 IN*0T M8SS6A8 FEMSA.
I- SY/S0#1C <= 0.
:rite 8100.
8NDI-.
D6T6@ FEMSA$100' Type C.
7*8N D6T6S8T FEIN*0TE-IL8 -71 IN*0T M8SS6A8 FEMSA.
I- SY/S0#1C <= 0.
:rite 8100 :ith
vEms( sy/su,rc sy/uname sy/cpro(.
8NDI-.
The a,ove messa(e :ill pro&uce the 3ollo:in( messa(e@
I*ro(ram DEpro(xxx run ,y Hoe Smith 3aile& :ith K3ile permissionsL pro,lem5
1C2)J.
'ile Operations " logical #ile names
%*ideline0 4se Logical )ilenames for processing e,ternal files
E!planation0 All input and output file names must be passed to t#e program as parameters using logical file names.
Logical filenames are mapped to p#ysical filenames using transaction )IL'. 6#en possible7 logical file
names s#ould be included in t#e selection screen of t#e program. T#e function )IL'.*'T./A!' must
be used to translate t#e logical file name to t#e p#ysical pat# and file name. If you are t#e person
creating logical file names7 it is ad5ised t#at you use t#e logical pat#s and dynamic 5ariables i.e. client7
instance7 etc. to ensure file names meet your file naming con5entions and t#e programs t#at use t#e files
+or% across all system boundaries.
Snippet0
un&er construction
E!ception (andling " a#ter #*nction call
%*ideline0 *eneric !essaging after a function call is not recommended.
E!planation0 Anytime you use t#e PATT'R/ function in t#e +or%benc# to insert a function7 it automatically inserts a
generic message in t#e format
E!'SSA*' ID sy-msgid TFP' sy-msgty /4!"'R sy-msgno 6IT< sy-msg51 sy-msg5G7 sy-msg507 sy-
msg53H.
T#is statement assumes t#at SF 5ariables #a5e been filled +it# meaningful 5alues by t#e function itself.
<o+e5er7 often times7 t#is is not t#e case.
T#e generic message s#ould only be used in t#e case +#ere t#e function and all nested functions t#at
are called use t#e !essage...Raising format consistently t#roug#out t#e function c#ain. T#e
!essage...Raising format sets t#e sy-message and ot#er sy 5ariables. &nly t#en7 can you be assured
t#at a generic message statement can be used to present a proper error message. <o+e5er7 since t#is
situation is most li%ely not t#e case7 a good de5eloper +ill #andle t#e message t#emsel5es and not count
on t#e generic form to pro5ide a meaningful message rele5ant to t#e situation.
Snippet0
I- sy/su,rc <= 0.
M8SS6A8 ID sy/ms(i& TY*8 sy/ms(ty
N0M#81 sy/ms(no
>IT! sy/ms(v1 sy/ms(vB sy/ms(vC sy/ms(v).
8NDI-.
C6LL -0NCTI7N 'DEM6T81I6LEFI8>'
8%*71TINA
imE3ullE&etails 2 '%'
imEtEmaterial 2 tEmaterials
8%C8*TI7NS
invali&Einput 2 1
invali&Ematerial 2 B
noEmaterials 2 C
7T!81S 2 ).

C6S8 sy/su,rc.
>!8N 1.
M8SS6A8 eGGG >IT! 'Invali& Input'$101'.
>!8N B.
M8SS6A8 eGGG >IT! 'Invali& Material'$10B'.
>!8N C.
M8SS6A8 eGGG >IT! 'No Materials'$10C'.
>!8N ).
M8SS6A8 eGGG >IT! '7ther 1easons'$10)'.
8NDC6S8.
Open " Message Cla*se
%*ideline0 4se !essage clause on &P'/ statement to trap operating system errors.
E!planation0 Aut#oriOation and permission access at t#e &S le5el is one of t#e most freCuent causes of an &P'/
statement failing. $#ec%ing t#e sy-subrc +ill only indicate t#at t#e operation failed but not t#e reason
+#y. To trap t#e error condition at t#e operating system le5el7 4se t#e !essage option on t#e &pen
command.
Snippet0
7*8N D6T6S8T FEIN*0TE-IL8 -71 IN*0T.
I- SY/S0#1C <= 0.
:rite sy/su,rc.
8NDI-.
D6T6@ FEMSA$100' Type C.
7*8N D6T6S8T FEIN*0TE-IL8 -71 IN*0T M8SS6A8 FEMSA.
I- SY/S0#1C <= 0.
:rite 8100 :ith vEms( sy/su,rc.
8NDI-.
S*bscripting and O##sets
%*ideline0 A5oid code t#at uses specific offsets for t#e purpose of subscripting into fields and structures
E!planation0 A5oid code t#at uses specific offsets to subscript into t#e code. Try to determine lengt#s and use
structures to ac#ie5e positioning of fields. T#is is especially 5ulnerable +#en coding in a 4/I$&D'
en5ironment as c#aracters are not represented by a single byte.
Snippet0
Date is o3ten times su,scripte&
The 3ollo:in( co&e is sensitive to the user master recor& an& vulnera,le to
su,scriptin( errors.
FEY861 2 FEMYD6T8N)$)'.
FEM7NT! 2 FEMYD6T8NB$B'.
FED6Y 2 FEMYD6T8N0$B'.
This co&e is lon(er5 yes5 ,ut not sensitive to &ate 3ormat chan(es :hich
can cause some very serious errors an& outcome.
#8AIN 7- SEMYD6T85
M7NT!$B'5
D6Y$B'5
Y861$)'5
8ND 7- SEMYD6T8 .
#8AIN 7- SEY701D6T85
Y861$)'5
M7NT!$B'5
D6Y$B'5
8ND 7- SEY701D6T8.
SEMYD6T8/Y861 2 SEY701D6T8/Y861.
SEMYD6T8/M7NT! 2 SEY701D6T8/M7NT!.
SEMYD6T8/D6Y 2 SEY701D6T8/D6Y.
nternal Tables " De#ining
%*ideline0 Do not use &ccurs P or 6it# <eader Line unless it is a SAP function t#at reCuires it.
E!planation0 STR4$T4R' and ?TA"L'S? clause is considered obsolete. If your internal table is a custom structure7
al+ays declare t#e table structure +it#in a type statement before defining t#e internal table itself.
Snippet0
D6T6@ #8AIN 7- TEM6T 7CC01S 05
M6TN1 LI?8 M616/M6TN15
>81?S LI?8 M61C/>81?S5
DIS*7 LI?8 M61C/DIS*75
8ND 7- TEM6T.
TY*8S@ #8AIN 7- TY*8EM6T5
M6TN1 LI?8 M616/M6TN15
>81?S LI?8 M61C/>81?S5
DIS*7 LI?8 M61C/DIS*75
8ND 7- TY*8EM6T.
D6T6@ TEM6T TY*8 ST6ND61D T6#L8 7- TY*8EM6T.
Str*ct*res
Simple Structure 8xample
Data@ #8AIN 7- SE71D81.
INCL0D8 ST10CT018 F#6?.
Data@ 8ND 7- SES71D81.
Simple Structure 8xample
D6T6@ SE71D81 TY*8 F#6?.
Deep Structure
Data@ #8AIN 7- SE71D815
component15
Inclu&e Structure F#6?
componentB5
Data@ 8ND 7- SE71D81
Deep Structure
Data@ #8AIN 7- SE71D815
component1$1' type c5
componentB TY*8 F#6?5
componentC type I5
Data@ 8ND 7- SE71D81.
Special Consi&erations@
To access the in&ivi&ual 3iel&s in a &eep structure5 please use syntax in the 3ollo:in( example@
SE71D81/SES7/F#8LN 2 vEor&ernum,er.
Typin( Structures
TY*8S@ #8AIN 7- TY*8EMYST10.
INCL0D8 ST10CT018 F#6?.
TY*8S@ vEpoE&oc LI?8 8?*7/F#8LN5
8ND 7- TY*8EMYST10.
Typin( Structures
TY*8S@ #8AIN 7- TY*8EMYST105
sEso TY*8 F#6?5
vEpoE&oc TY*8 8?*7/F#8LN5
8ND 7- TY*8EMYST10.
TA5&ES
%*ideline0 TA"L'S statement is not reCuired
E!planation0 T#e use of TA"L'S statements for t#e declaration of +or% areas or +#en used in an SAL statement7 is
not reCuired. It allocates unnecessary storage. T#is is a pre 3., con5ention
Snippet0
T6#L8S@ 8?*7.
T6#L8S statement not re.uire&
nternal Tables " De#ining
%*ideline0 Do not use &ccurs P or 6it# <eader Line unless it is a SAP function t#at reCuires it.
E!planation0 STR4$T4R' and ?TA"L'S? clause is considered obsolete. If your internal table is a custom structure7
al+ays declare t#e table structure +it#in a type statement before defining t#e internal table itself.
Snippet0
D6T6@ #8AIN 7- TEM6T 7CC01S 05
M6TN1 LI?8 M616/M6TN15
>81?S LI?8 M61C/>81?S5
DIS*7 LI?8 M61C/DIS*75
8ND 7- TEM6T.
TY*8S@ #8AIN 7- TY*8EM6T5
M6TN1 LI?8 M616/M6TN15
>81?S LI?8 M61C/>81?S5
DIS*7 LI?8 M61C/DIS*75
8ND 7- TY*8EM6T.
D6T6@ TEM6T TY*8 ST6ND61D T6#L8 7- TY*8EM6T.
nternal Tables " &oop into 6or, area
%*ideline0 Loop at itab s#ould al+ays use a +or% area or assign to a field symbol
E!planation0 4se of internal tables D+it# #eaderD is considered an old con5ention. Internal tables +it# <eader are not
allo+ed in A"AP && and +ill t#erefore not pass synta, c#ec%.
Al+ays use a +or% area or assign directly to a field symbol +#en processing t#e internal table.
Snippet0
Loop at ita,.
9 re3erence ita, components &irectly via the hea&er area
result 2 ita,/3iel&1.
8n&loop.
Loop at ita, into sEita,.
9 re3erence ita, components usin( :or+ area sEita,
result 2 sEita,/3iel&1.
8n&loop.
Loop at ita,.
9 re3erence ita, components &irectly via the hea&er area
result 2 ita,/3iel&1.
8n&loop.
Loop at ita, assi(nin( <3sEita,=.
9 re3erence ita, components usin( 3iel& sym,ol <3sEita,=
result 2 <3sEita,=/3iel&1.
8n&loop.
E1TRACT #iles
%*ideline0 $ombine multiple arrays of data +it# different structures into a single table for processing7 an 'XTRA$T
file is an option
E!planation0 T#ere are ad5antages to using an ',tract file but common practice is t#at most de5elopers +ill tend to
use internal tables for array processing. It is a good option to use for reporting purposes +#en dealing
+it# internal tables of different structures t#at same same or similar %eys.
$onsideration; )rom a maintenance perspecti5e7 fe+ de5elopers #a5e e,perience +it# 'XTRA$T files.
/&T'; &nly one 'XTRA$T file can be used per program7 +#ereas many internal tables could be used in
one program. T#ere is no rig#t or +rong on t#is one and is entirely up to t#e IT s#op or t#e programming
reCuirement.
5oolean Operators
%*ideline0 4se operators =Q7 -7 7 N7 R> rat#er t#an t#e +ords ADD7 S4"TRA$T7 !4LTIPLF7 DILID'7 !&L'
E!planation0 4se t#e "oolean operators rat#er t#an t#e alp#abetic acronyms for boolean operations.
/ote; T#is is not a strict and #ard guideline. It is only meant to be a suggestion to ad#ere to boolean
operations o5er 'nglig# li%e commands.
Snippet0
multiply items ,y price (ivin( totalprice
totalprice 2 items 9 price
Move value1 to valueB.
valueB 2 value1.
I- items AT 0.
I- items = 0.
'*nctions " S)ared Memory
%*ideline0 $ustomer )unctions t#at are truly standalone s#ould not s#are t#eir function group
E!planation0 )unctions +it#in t#e same )unction *roup s#are a global memory area. 6#en a program calls a
)unction !odule7 all t#e )unction !odules +it#in t#at group are loaded into memory +it#in t#e same
logical unit of +or%. T#is reduces system o5er#ead by allo+ing t#e s#aring of global data. In t#e case7
+#ere a )unction !odule is truly a standalone ob(ect t#at #as no need to s#are t#e global resources of a
)unction *roup7 it is recommended t#at a function group is created to contain only t#e standalone
function.
Dynamically called )unction !odules must be defined in t#eir o+n )unction *roup.
If )unction !odules are being called KI/ 4PDAT' TASMI and are using internal tables t#at are defined as
*lobal Data7 t#e global internal table +ill need to be cleared eac# time t#e function module is being
e,ecuted. &t#er+ise t#e internal table records are still a5ailable from t#e last function module e,ecution
and could cause failures suc# as a ?Duplicate Mey 4pdate Tas%?.
Snippet0
un&er construction
Data " %lobal vers*s local
%*ideline0 Lariables t#at are only used locally s#ould be defined locally
E!planation0 Lariables t#at are used only +it#in a processing unit suc# as a subroutine s#ould be defined locally
+it#in t#at processing unit.
Snippet0
&ata@ (vEtotal type i.
per3orm calculateE&i33erence.
-71M calculateE&i33erence.
&ata@ lvEamt type i5
lvEraise type i.
(vEtotal 2 lvEamt N lvEraise.
:rite (vEtotal.
8ND-71M.
per3orm calculateE&i33erence.
-71M calculateE&i33erence.
&ata@ lvEamt type i5
lvEraise type i5
lvEtotal type i.
lvEtotal 2 lvEamt N lvEraise.
:rite lvEtotal.
8ND-71M
N*mber Ranges
%*ideline0 4se t#e number range functions for generating uniCue seCuence numbers
E!planation0 A uniCue seCuence number can be auto generated by SAP functionality. /o need to build custom
seCuence number generators. 4se t#e SAP functions from function groups S/R for t#is purpose.
/ote; /umber range ob(ects are transportable ob(ects but /e5er transport number range inter5als.
Snippet0
DS84N7 2 DS84N7 N 1.
C6LL -0NCTI7N 'N0M#81EA8TEN%T'
8%*71TINA
nrEran(eEnr 2 '01'
o,Oect 2 'DS84N7'
406NTITY 2 '1'
IM*71TINA
num,er 2 ;imEse.no
8%C8*TI7NS
7T!81S 2 1.
$ariant
%*ideline0 Transporting Lariants
E!planation0 Lariants s#ould be entered in a *old client and t#en tranported t#ru t#e migration pat# to t#e target
en5ironments.
Data Objects " -se o# De#a*lts
%*ideline0 Do not use implied defaults on data declarations
E!planation0 If a field is TFP' $7 t#en be e,plicit and say Type $. !any programmers are con5ersant in se5eral
programming languages. "y being e,plicit +it# 5ariable definitions7 it ma%es it clear for t#e de5eloper and
lea5es no option for interpretation. Defaults are prone to c#ange 7 especially as t#e protocols of 4nicode
and A"AP && mature. If defaults c#ange7 t#e outcome of a program can also be affected.
Snippet0
D6T6@ vEmy3iel&.
D6T6@ vEmy3iel&$1' TY*8 C.
Case " +)en Ot)ers
%*ideline0 Al+ays program a 6#en &t#ers statement
E!planation0 Al+ays program a 6#en &t#ers statement so t#at any condition t#at is not met +ill #a5e a fallout pat# for
t#e code to ta%e.
Al+ays pro5ide an action or a generic message so t#at t#e program can announce t#at an une,pected
situation #as been encountered.
Snippet0
C6S8 SY/S0#1C.
>!8N 1.
co&e 1.
>!8N B.
co&e B.
8NDC6S8.
C6S8 SY/S0#1C.
>!8N 1.
co&e 1.
>!8N B.
co&e B.
>!8N 7T!81S.
M8SS6A8 >100.
8NDC6S8.
Te!t Elements
%*ideline0 )or multi-lingual prgramming7 all te,t in Cuotes s#ould #a5e an associated te,t element defined
E!planation0 T#in% !ultilingual
If te,t literals are enclosed in Cuotes in statements li%e 6RIT'7 !'SSA*'7 $&/$AT'/AT' 7 t#en t#ere
s#ould be an associated te,t element.
i.e. 6rite; ?'rror +it# t#is statement?=P22>
Some *eneral Rules
- Do not use multiple te,t elements to generate one large te,t +it#in your program.
- 4se t#e addition DAS T'XTD +it#in S&RT statements to ensure t#at t#e sort seCuence considers
language specific c#aracters7 e.g. *erman S7 T7 U7 V .
- 4se Te,t elements or report title to define list #eadings
- Te,t descriptions in t#e Data Dictionary s#ould be defined in language dependent tables.
- Description fields defined in t#e Data Dictionary s#ould be long enoug# to store translations.
Snippet0
>rite Text/0GG
or
>rite '8rror :ith this statement'.
>rite@ '8rror :ith this statement'$0GG'.
5AP
De SAP A5AP en castellano
&altar a na8e0aci:n, b;s+ueda
=5usiness Application Programming nterfaces>. Son funciones Cue nos
permiten simular funcionalidad. $rear pedido7 #acer mo5imientos de
mercancias7 etc.. /ormalmente 5eo lo Cue #ace la transacciWn estandar7 5eo
los 5alores Cue se 5an rellenando e intento buscar su correspondencia en la
bapi en los importing o tables.. 's un forma muy simple de 5erlas7 pero es una
introducciWn simple. Son funcicones Cue estXn integradas en los "&R = ob(etos
de negocios >..
"API 5iene de un 5ie(o concepto de programaciWn7 API7 Cue son programas ya
#ec#os Cue estXn a disposiciWn del programador. 'n el caso de SAP7 las "API
son funciones para un determinado uso. 'stXn bastante bien documentadas7
algunas7 en la transacciWn "API. Su uso es 5ariado7 como e(emplo la creaciWn
de Wrdenes de compra7 por caso. & sea7 reemplaOando la e(ecuciWn de un
$ALL TRA/SA$TI&/ por la "API correspondiente. Tiene una me(or
performance7 muc#a simplicidad en el cWdigo y mayor control sobre los datos.
Tienen7 tambiYn7 la particularidad de ser R)$7 o sea Cue se pueden e(ecutar en
forma remota desde otro aplicati5o. 'n general7 casi todas7 estXn precedidas
por la palabra "API. Desde la S'0:7 tipeando "API y presionando )3 trae el
listado.
$btenido de <http:==sap>.com=wiki=index.php?title@6A7(<
R'C
De SAP A5AP en castellano
&altar a na8e0aci:n, b;s+ueda
4na R)$ solo es una funciWn Cue se puede llamar desde un sistema e,terno a
SAP7 el cual puede ser otro SAP u otro tipo de sistema. Lamos Cue se definen
en la S'0: igualmente con la diferencia de Cue en la pestaZa de atributos se
pinc#a el radiobutton ?!odulo de acceso remoto?. Las aplicaciones de una R)$
si Cue son mas e,tensas7 pero 5amos7 Cue por e(emplo7 permiten intercambiar
datos entre SAP y el sistema llamante...infinidad de cosas.
<as de muc#o cuidado es con los destinos R)$ =transacciWn S![2> y los
usuarios Cue estXn ligados a ellos. <ay 5arios tipos de usuarios7 y normalmente
el Cue se utiliOa para pasar informaciWn es el $PI$ =lo puedes 5er por la
transacciWn S4P1 y la pestaZa logon data>. <as de tener en cuenta Cue el
usuario $PI$ no pide logon7 porCue sWlo se pasa informaciWn7 pero si el usuario
es DIAL&* pide pantalla de logon y normalmente se utiliOa para pasar
pantallas7 o sea7 conectarse en remoto a una transacciWn. 'n teoria este tema
lo gestionan los de sistemas7 Cue se encargan de crear los destinos y los
usuarios asociados a ellos7 por lo Cue para t\ deber\a ser transparente. Por lo
demXs7 es una funciWn como cualCuier otra7 y si la llaman desdel e,terior de
SAP7 mientras le pasen los parXmetros correctamente7 no #ay problema.
'spero Cue te sir5a de ayuda #asta Cue alguien te pase la documentaciWn.
Ler e(emplo de R)$ desde Lisual "asic
$btenido de <http:==sap>.com=wiki=index.php?title@R.C<