Abap 740 Quick Reference
Abap 740 Quick Reference
40 Quick Reference
So you're an experienced ABAP programmer wanting to leverage off the fantastic new functionality
available to you in ABAP 7.40!
However, searching for information on this topic leads you to fragmented pages or blogs that refer to only a
couple of the new features available to you.
What you need is a quick reference guide which gives you the essentials you need and shows you how the
code you are familiar with can be improved with ABAP 7.40.
The below document contains exactly this!
It gives examples of "classic" ABAP and its 740 equivalent. It goes into more details on the more difficult topics
normally via examples. This allows the reader to dive in to the level they desire. While this document does not
contain everything pertaining to ABAP 740 it certainly covers the most useful parts in the experience of the
author.
The document has been compiled by drawing on existing material available online as well as trial and error by
the author. In particular the blogs by Horst Keller have been useful and are the best reference I have found
(prior to this document
). He has a landing page of sorts for his various blogs on the topic here:
ABAP Language News for Release 7.40
Credit also goes to Naimesh Patel for his useful explanations and examples on ABAP 7.40. Here is his
example of the "FOR iteration expression" which I leaned on (links to his other 740 articles can be found at the
bottom of the link):
http://zevolving.com/2015/05/abap-740-for-iteration-expression/
I compiled the below document to make the transition to using ABAP 740 easier for myself and my project
team. It has worked well for us and I hope it will do the same for you.
Regards,
Jeff Towell
Author:
Date Created:
Jeffrey Towell
Friday, 22 May 2015
Contents
Inline Declarations
Table Expressions
Conversion Operator CONV
1. Definition.
2. Example.
Value Operator VALUE
1. Definition.
2. Example for structures.
3. Examples for internal tables.
FOR operator
1. Definition.
2. Explanation.
3. Example 1.
4. Example 2.
5. FOR with THEN and UNTIL|WHILE.
Reduce
1. Definition.
2. Note.
3. Example 1.
4. Example 2.
5. Example 3.
Conditional operators COND and SWITCH
1. Definition.
2. Example for COND.
3. Example for SWITCH.
Strings
1. String Templates.
2. Concatenation.
3. Width/Alignment/Padding.
4. Case.
5. ALPHA conversion.
6. Date conversion.
Classes/Methods
1. Referencing fields within returned structures.
2. Methods that return a type BOOLEAN..
3. NEW operator
Meshes
1. Problem..
2. Solution.
3. Output
Filter
1. Definition.
2. Problem..
3. Solution.
Inline Declarations
Description
Before 7.40
With 7.40
Data statement
DATA(text) = `ABC`.
text = `ABC`.
Loop at into work area
...
ENDLOOP.
Call method
oref->meth( IMPORTING p1 =
DATA(a1)
IMPORTING p2 =
DATA(a2) ).
Loop at assigning
LOOP AT itab
ASSIGNING FIELDSYMBOL(<line>).
LOOP AT itab
<line>
ASSIGNING
...
ENDLOOP.
ENDLOOP.
Read assigning
ASSIGNING FIELDSYMBOL(<line>).
Select single f1 f2
Table Expressions
If a table line is not found, the exception CX_SY_ITAB_LINE_NOT_FOUND is raised. No sy-subrc.
Description
Before 7.40
With 7.40
wa = itab[ idx ].
wa = itab[ col1 =
col2 = ].
col2 =
INTO wa.
Read Table with key
components
col2=
].
col2 =
INTO wa.
Does record exist?
DATA(idx)=
line_index( itab[ ] ).
TRANSPORTING NO FIELDS.
idx = sy-tabix.
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.
ASSIGN lt_tab[ 1 ] to FIELD-SYMBOL(<ls_tab>).
IF sy-subrc = 0.
...
ENDIF.
NB: Use itab [ table_line = ] for untyped tables.
Example
Method cl_abap_codepage=>convert_to expects a string
Before 7.40
DATA text TYPE c LENGTH 255.
DATA helper TYPE string.
DATA xstr TYPE xstring.
helper = text.
xstr = cl_abap_codepage=>convert_to( source = helper ).
With 7.40
DATA text TYPE c LENGTH 255.
DATA(xstr) = cl_abap_codepage=>convert_to( source = CONV string( text ) ).
OR
DATA(xstr) = cl_abap_codepage=>convert_to( source = CONV #( text ) ).
TYPE ty_columns2.
itab = VALUE #( ( ) ( 1 ) ( 2 ) ).
Structured line type (RANGES table):
DATA itab TYPE RANGE OF i.
FOR operator
Definition
FOR wa|<fs> IN itab [INDEX INTO idx] [cond]
Explanation
This effectively causes a loop at itab. For each loop the row read is assigned to a work area (wa) or fieldsymbol(<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
that subroutine. Index like SY-TABIX in loop.
Given:
TYPES: BEGIN OF ty_ship,
tknum TYPE tknum,
"Shipment Number
"Starting city
"Shipment route
END OF ty_ship.
TYPES: ty_ships TYPE SORTED TABLE OF ty_ship WITH UNIQUE KEY tknum.
Row
TKNUM[C(10)]
Name[C(12)]
City[C(25)]
Route[C(6)]
001
John
Melbourne
R0001
002
Gavin
Sydney
R0003
003
Lucy
Adelaide
R0001
004
Elaine
Perth
R0003
Example 1
Populate internal table GT_CITYS with the cities from GT_SHIPS.
Before 7.40
DATA: gt_citys TYPE ty_citys,
gs_ship TYPE ty_ship,
gs_city TYPE ort01.
Example 2
Populate internal table GT_CITYS with the cities from GT_SHIPS where the route is R0001.
Before 7.40
DATA: gt_citys TYPE ty_citys,
gs_ship TYPE ty_ship,
gs_city TYPE ort01.
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
DATA(gt_itab) = VALUE ty_tab( FOR j = 11 THEN j + 10 UNTIL j > 40
( col1 = j col2 = j + 1 col3 = j + 2 ) ).
Reduce
Definition
... REDUCE type(
INIT result = start_value
...
FOR for_exp1
FOR for_exp2
...
NEXT ...
result = iterated_value
... )
Note
While VALUE and NEW expressions can include FOR expressions, REDUCE must include at least one
FOR expression. You can use all kinds
of FOR expressions in REDUCE:
with IN for iterating internal tables
with UNTIL or WHILE for conditional iterations
Example 1
Count lines of table that meet a condition (field F1 contains XYZ).
Before 7.40
DATA: lv_lines TYPE i.
LOOP AT gt_itab INTO ls_itab where F1 = XYZ.
lv_lines = lv_lines + 1.
ENDLOOP.
With 7.40
DATA(lv_lines) = REDUCE i( INIT x = 0 FOR wa IN gt_itab
Example 2
Sum the values 1 to 10 stored in the column of a table defined as follows
DATA gt_itab TYPE STANDARD TABLE OF i WITH EMPTY KEY.
gt_itab = VALUE #( FOR j = 1 WHILE j <= 10 ( j ) ).
Before 7.40
DATA: lv_line TYPE i,
lv_sum TYPE i.
LOOP AT gt_itab INTO lv_line.
lv_sum = lv_sum + lv_line.
ENDLOOP.
With 7.40
DATA(lv_sum) = REDUCE i( INIT x = 0 FOR wa IN itab NEXT x = x + wa ).
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.
output->display( ).
Strings
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
functional methods and method chainings
Before 7.40
DATA itab TYPE TABLE OF scarr.
SELECT * FROM scarr INTO TABLE itab.
cl_demo_output=>display( output ).
With 7.40
Concatenation
Before 7.40
DATA lv_output TYPE string.
CONCATENATE 'Hello' 'world' INTO lv_output SEPARATED BY space.
With 7.40
DATA(lv_out) = |Hello| & | | & |world|.
Width/Alignment/Padding
WRITE / |{ 'Left'
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) }|.
ALPHA conversion
DATA(lv_vbeln) = '0000012345'.
WRITE / |{ lv_vbeln ALPHA = OUT }|. or use ALPHA = IN to go in other direction
Date conversion
WRITE / |{ pa_date DATE = ISO }|. Date Format YYYY-MM-DD
WRITE / |{ pa_date DATE = User }|. As per user settings
Classes/Methods
Referencing fields within returned structures
Before 7.40
DATA: ls_lfa1 TYPE lfa1,
lv_name1 TYPE lfa1-name1.
ls_lfa1 = My_Class=>get_lfa1( ).
lv_name1 = ls_lfa1-name1.
With 7.40
DATA(lv_name1) = My_Class=>get_lfa1( )-name1.
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.
NEW operator
This operator can be used to instantiate an object.
Before 7.40
DATA: lo_delivs TYPE REF TO zcl_sd_delivs,
Meshes
Allows an association to be set up between related data groups.
Problem
Given the following 2 internal tables:
TYPES: BEGIN OF t_manager,
name TYPE char10,
salary TYPE int4,
END OF t_manager,
tt_manager TYPE SORTED TABLE OF t_manager WITH UNIQUE KEY name.
TYPE char10,
Name[C(10)]
Salary[I(4)]
Jason
3000
Thomas
3200
Row
Name[C(10)]
Salary[I(4)
Manager[C(10)]
Bob
2100
Jason
David
2000
Thomas
Jack
1000
Thomas
Jerry
1000
Jason
John
2100
Thomas
Tom
2000
Jason
Get the details of Jerrys manager and all developers managed by Thomas.
Solution
With 7.40
TYPES: BEGIN OF MESH m_team,
managers TYPE tt_manager ASSOCIATION my_employee TO developers
ON manager = name,
developers TYPE tt_developer ASSOCIATION my_manager TO managers
ON name = manager,
END OF MESH m_team.
Output
Jerry's manager: Jason
Salary: 3000
Thomas' developers:
Employee name: David
Employee name: Jack
Employee name: John
Filter
Filter the records in a table based on records in another table
Definition
... FILTER type( itab [EXCEPT] [IN ftab] [USING KEY keyname]
WHERE c1 op f1 [AND c2 op f2 [...]] )
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.
Solution
With 7.40
TYPES: BEGIN OF ty_filter,
cityfrom TYPE spfli-cityfrom,
cityto TYPE spfli-cityto,
f3
TYPE i,
END OF ty_filter,
ty_filter_tab TYPE HASHED TABLE OF ty_filter
WITH UNIQUE KEY cityfrom cityto.
DATA: lt_splfi TYPE STANDARD TABLE OF spfli.