Sap 7.4 Abap Inline Coding
Sap 7.4 Abap Inline Coding
40
Contents
1. Inline Declarations
2. Table Expressions
3. Conversion Operator CONV
I. Definition
II. Example
4. Value Operator VALUE
I. Definition
II. Example for structures
III. Examples for internal tables
5. FOR operator
I. Definition
II. Explanation
III. Example 1
IV. Example 2
V. FOR with THEN and UNTIL|WHILE
6. Reduction operator REDUCE
I. Definition
II. Note
III. Example 1
IV. Example 2
V. Example 3
7. Conditional operators COND and SWITCH
I. Definition
II. Example for COND
III. Example for SWITCH
8. CORRESPONDING operator
I. Definition
II. Example Code
III. Output
IV. Explanation
V. Additions MAPPING and EXCEPT
9.Strings
I. String Templates
II. Concatenation
III. Width/Alignment/Padding.
IV. Case
V. ALPHA conversion
VI. Date conversion
10. Loop at Group By
I. Definition
II. Explanation
III. Example
IV. Output
11. Classes/Methods
I. Referencing fields within returned structures
II. Methods that return a type BOOLEAN
III. NEW operator
12. Meshes
I. Problem
II. Solution
III. Output
13. Filter
I. Definition
II. Problem
III. Solution
1. Inline Declarations
Description Before 7.40 With 7.40
Datastatement DATA text TYPE string. DATA(text) = `ABC`.
text = `ABC`.
NB: There will be a short dump if you use an inline expression that references a non-existent record.
SAP says you should therefore assign a field symbol and check sy-subrc.
II. Example
Method cl_abap_codepage=>convert_to expects a string
Before 7.40
coln1 TYPE i,
coln2 TYPE ty_columns1,
END OF ty_columns2.
DATA: struc_simple TYPE ty_columns1,
struc_nest TYPE ty_columns2.
struct_nest = VALUE t_struct(coln1 = 1
coln2-cols1 = 1
coln2-cols2 = 2 ).
OR
itab = VALUE #( ( ) ( 1 ) ( 2 ) ).
5. FOR operator
I. Definition
FOR wa|<fs> IN itab [INDEX INTO idx] [cond]
II. Explanation
This effectively causes a loop at itab. For each loop the row read is assigned to a work area (wa) or
field-symbol(<fs>).
This wa or <fs> is local to the expression i.e. if declared in a subrourine the variable wa or <fs> is a
local variable of
III. Example 1
Populate internal table GT_CITYS with the cities from GT_SHIPS.
Before 7.40
IV. Example 2
Populate internal table GT_CITYS with the cities from GT_SHIPS where the
route is R0001.
Before 7.40
Note: ls_ship does not appear to have been declared but it is declared implicitly.
TYPES:
BEGIN OF ty_line,
col1 TYPE i,
col2 TYPE i,
col3 TYPE i,
END OF ty_line,
Before 7.40
j TYPE i.
j = 1.
DO.
j = j + 10.
IF j > 40. EXIT. ENDIF.
APPEND INITIAL LINE TO gt_itab ASSIGNING <ls_tab>.
<ls_tab>–col1 = j.
<ls_tab>–col2 = j + 1.
<ls_tab>–col3 = j + 2.
ENDDO.
With 7.40
Before 7.40
III. Example 1
Count lines of table that meet a condition (field F1 contains “XYZ”).
Before 7.40
V. Example 3
Using a class reference – works because “write” method returns reference to instance object
With 7.40
TYPES outref TYPE REF TO if_demo_output.
[ ELSE resultn ] ) …
[ ELSE resultn ] ) …
II. Example for COND
DATA(time) =
COND string(
|High Noon|
ELSE
THROW cx_cant_be( ) ).
III. Example for SWITCH
DATA(text) =
SWITCH #( sy-langu
8. Corresponding Operator
I. Definition
… CORRESPONDING type( [BASE ( base )] struct|itab [mapping|except] )
II. Example Code
With 7.40
IV. Explanation
Given structures ls_line1 & ls_line2 defined and populated as above.
1. The contents of ls_line1 are moved to ls_line2 where there is a matching column name. Where there
is no
… MAPPING t1 = s1 t2 = s2
EXCEPT allows you to list fields that must be excluded from the data transfer
… EXCEPT {t1 t2 …}
9. Strings
I. String Templates
A string template is enclosed by two characters “|” and creates a character string.
Literal text consists of all characters that are not in braces {}. The braces can contain:
data objects,
calculation expressions,
constructor expressions,
table expressions,
predefined functions, or
Before 7.40
With 7.40
III. Width/Alignment/Padding
WRITE / |{ ‘Left’ WIDTH = 20 ALIGN = LEFT PAD = ‘0’ }|.
WRITE / |{ ‘Centre’ WIDTH = 20 ALIGN = CENTER PAD = ‘0’ }|.
WRITE / |{ ‘Right’ WIDTH = 20 ALIGN = RIGHT PAD = ‘0’ }|.
IV. Case
WRITE / |{ ‘Text’ CASE = (cl_abap_format=>c_raw) }|.
WRITE / |{ ‘Text’ CASE = (cl_abap_format=>c_upper) }|.
WRITE / |{ ‘Text’ CASE = (cl_abap_format=>c_lower) }|.
V. ALPHA conversion
DATA(lv_vbeln) = ‘0000012345’.
WRITE / |{ lv_vbeln ALPHA = OUT }|. “or use ALPHA = IN to go in other
direction
VI. Date conversion
WRITE / |{ pa_date DATE = ISO }|. “Date Format YYYY-MM-DD
WRITE / |{ pa_date DATE = User }|. “As per user settings
WRITE / |{ pa_date DATE = Environment }|. “Formatting setting of language
environment
10. Loop at Group By
I. Definition
LOOP AT itab result [cond] GROUP BY key ( key1 = dobj1 key2 = dobj2 …
…
[LOOP AT GROUP group|<group>
…
ENDLOOP.]
…
ENDLOOP.
II. Explanation
The outer loop will do one iteration per key. So if 3 records match the key there will only be one iteration
for these 3 records. The structure “group” (or
“<group>” ) is unusual in that it can be looped over using the “LOOP AT GROUP” statement. This will
loop over the 3 records (members) of the group. The
structure “group” also contains the current key as well as the size of the group and index of the group ( if
GROUP SIZE and GROUP INDEX have been
assigned a field name). This is best understood by an example.
III. Example
With 7.40
TYPES: BEGIN OF ty_employee,
name TYPE char30,
role TYPE char30,
age TYPE i,
END OF ty_employee,
ty_employee_t TYPE STANDARD TABLE OF ty_employee WITH KEY name.
DATA(gt_employee) = VALUE ty_employee_t(
( name = ‘John‘ role = ‘ABAP guru‘ age = 34 )
( name = ‘Alice‘ role = ‘FI Consultant‘ age = 42 )
( name = ‘Barry‘ role = ‘ABAP guru‘ age = 54 )
( name = ‘Mary‘ role = ‘FI Consultant‘ age = 37 )
( name = ‘Arthur‘ role = ‘ABAP guru‘ age = 34 )
( name = ‘Mandy‘ role = ‘SD Consultant‘ age = 64 ) ).
DATA: gv_tot_age TYPE i,
gv_avg_age TYPE decfloat34.
“Loop with grouping on Role
LOOP AT gt_employee INTO DATA(ls_employee)
GROUP BY ( role = ls_employee-role
size = GROUP SIZE
index = GROUP INDEX )
ASCENDING
ASSIGNING FIELD-SYMBOL(<group>).
CLEAR: gv_tot_age.
“Output info at group level
WRITE: / |Group: { <group>-index } Role: { <group>-role WIDTH = 15 }|
& | Number in this role: { <group>-size }|.
“Loop at members of the group
LOOP AT GROUP <group> ASSIGNING FIELD-SYMBOL(<ls_member>).
gv_tot_age = gv_tot_age + <ls_member>-age.
WRITE: /13 <ls_member>-name.
ENDLOOP.
“Average age
gv_avg_age = gv_tot_age / <group>-size.
WRITE: / |Average age: { gv_avg_age }|.
With 7.40
SKIP.
ENDLOOP.
IV. Output
Group: 1 Role: ABAP guru Number in this role: 3
John
Barry
Arthur
Average age: 40.66666666666666666666666666666667
Group: 2 Role: FI Consultant Number in this role: 2
Alice
Mary
Average age: 39.5
Group: 3 Role: SD Consultant Number in this role: 1
Mandy
Average age: 64
11. Classes/Methods
I. Referencing fields within returned structures
Before 7.40
DATA: ls_lfa1 TYPE lfa1,
ls_lfa1 = My_Class=>get_lfa1( ).
lv_name1 = ls_lfa1–name1.
With 7.40
DATA(lv_name1) = My_Class=>get_lfa1( )–name1.
IF My_Class=>return_boolean( ) = abap_true.
…
ENDIF.
With 7.40
IF My_Class=>return_boolean( ).
…
ENDIF.
NB: The type “BOOLEAN” is not a true Boolean but a char1 with allowed values X,- and <blank>.
Using type “FLAG” or “WDY_BOOLEAN” works just as well.
III. NEW operator
This operator can be used to instantiate an object.
Before 7.40
DATA: lo_delivs TYPE REF TO zcl_sd_delivs,
lo_deliv TYPE REF TO zcl_sd_deliv.
CREATE OBJECT lo_delivs.
CREATE OBJECT lo_deliv.
lo_deliv = lo_delivs->get_deliv( lv_vbeln ).
With 7.40
DATA(lo_deliv) = new zcl_sd_delivs( )->get_deliv( lv_vbeln ).
12. Meshes
Allows an association to be set up between related data groups.
I. Problem
Given the following 2 internal tables:
END OF t_manager,
END OF t_developer,
Populated as follows:
Row Name[C(10)] Salary[I(4)]
1 Jason 3000
2 Thomas 3200
Get the details of Jerry’s manager and all developers managed by Thomas.
II. Solution
With 7.40
Thomas’ developers:
I. Definition
… FILTER type( itab [EXCEPT] [IN ftab] [USING KEY keyname]
WHERE c1 op f1 [AND c2 op f2 […]] )
II. Problem
Filter an internal table of Flight Schedules (SPFLI) to only those flights based on a filter table that
contains the fields Cityfrom and CityTo.
III. Solution
With 7.40
Note: using the keyword “EXCEPT” (see definition above) would have returned the exact opposite
records i.e all records EXCEPT for those those returned above.