SAP ABAP New Syntax
After version 7.4 in SAP, new syntax where introduced which increases the efficiency of the code written and also makes it easy for developers, which includes Inline Declaration, Table Expression, Data Operations.
Here are some of the new syntax which I have come across and found out to be very useful for working in ABAP and making coding less complex.
1.Inline Declaration:
Before 7.4 New syntaxes were not introduced the variable needs to be declared before using it, now using inline declaration variable can we declare and use at the same time, Below are some of the syntax which has changed after 7.4.
Whenever data is to be moved to a variable, can be declared at the same instance,
Old Syntax.
DATA var1 TYPE char5.
var1 = ‘ABC’.New Syntax.
DATA(var1) = ‘ABC’.
Select Single From DataBase Table into Work Area
Old Syntax.
DATA wa TYPE ....
SELECT SINGLE fld1 fld2 FROM ....
INTO wa
WHERE fld1 = var1
AND fld2 = var2.New Syntax.
SELECT SINGLE fld1, fld2, FROM ....
INTO @DATA(wa)
WHERE fld1 = @var1
AND fld2 = @var2.
Selecting Data From DataBase Table Into Internal Table,
Old Syntax.
DATA itab TYPE TABLE OF ....
SELECT fld1 fld2 FROM ....
INTO TABLE itab
WHERE fld1 = var1
AND fld2 = var2.New Syntax.
SELECT fld1, fld2 FROM ....
INTO TABLE @DATA(itab)
WHERE fld1 = @var1
AND fld2 = @var2.
Declaration of the work area while operation on Internal Table,
Old Syntax.
DATA wa TYPE structure
LOOP AT itab INTO wa.
or
READ TABLE itab into wa WITH KEY field1 = var1.New Syntax.
LOOP AT itab INTO data(wa).
or
READ TABLE itab into data(wa) WITH KEY field1 = var1.
Moving Data From One Table To Other.
Old Syntax.
DATA lt_tab LIKE itab.
lt_tab[] = itab[].New Syntax.
DATA(lt_tab) = itab[].
Assigning Field Symbols
Old Syntax.
FIELD-SYMBOLS: <line> type …
LOOP AT itab ASSIGNING <line>.
or
READ TABLE itab ASSIGNING <line> WITH KEY FLD1 = VAR1.New Syntax.
LOOP AT itab ASSIGNING FIELD-SYMBOL(<line>).
OR
READ TABLE itab ASSIGNING FIELD-SYMBOL(<line>) WITH KEY FLD1 = VAR1.
2.Table Expression:
In SAP whenever the data is to be taken from Internal Table to any variable in Code for further processing, Loop or Read Table are generally used to get that data from Internal Table to work area and then move to another variable, so this effort can be lessened by using the new syntax.
Get line from table to Work Area.
Old Syntax.
READ TABLE itab INTO DATA(wa) WITH KEY fld1 = var1.New Syntax.
DATA(wa) = itab[ fld1 = var1 ].
Read table with Index 1.
Old Syntax.
READ TABLE itab INTO DATA(wa) index 1.New Syntax.
DATA(wa) = itab[ 1 ].
Get a particular field from one table to another variable.
Old Syntax.
READ TABLE itab INTO wa WITH KEY fld1 = var1.
IF sy-subrc EQ 0.
lv_var2 = wa-var2.
ENDIF.New Syntax.
lv_var2 = itab[ fld1 = var1 ]-var2.
Check particular Value is in Internal Table.
Old Syntax.
READ TABLE itab TRANSPORTING NO FIELDS WITH KEY fld1 = var1.
IF sy-subrc = 0.ENDIF.New Syntax.
IF line_exists( itab[ fld1 = var1 ] ).ENDIF.
Get Index Number of Particular Entry From Internal Table.
Old Syntax.
READ TABLE itab TRANSPORTING NO FIELDS fld1 = var1.
IF sy-subrc eq 0
lv_tabix = sy-tabix.
ENDIF.New Syntax.
lv_tabix = line_index( itab[ fld1 = var1 ] ).
Modify Entry in Table.
Old Syntax.
READ TABLE itab ASSIGNING <lfs_tab> fld1 = var1.
IF sy-subrc eq 0
<lfs_tab>-fld2 = 'Text'.
ENDIF.New Syntax.
itab[ fld1 = var1 ]-fld2 = 'Text'.
Note: While Using these Syntaxes in case of Table Expression use with Try Catch with cx_sy_itab_line_not_found and Entry.
3.Data Operations:
Here are some of the new Syntax which can be used for manipulating, changing, and storing data according to requirement.
Adding or Removing Leading Zeros to a Variable.
Old Synatx.
For Removing Leading Zeros
CALL FUNCTION ‘CONVERSION_EXIT_ALPHA_OUTPUT’
EXPORTING
input = var1
IMPORTING
OUTPUT = var1 .
For Adding Leading Zeros
CALL FUNCTION ‘CONVERSION_EXIT_ALPHA_INPUT’
EXPORTING
input = var1
IMPORTING
OUTPUT = var1 .New Syntax.
For Removing Leading Zeros
var1 = |{ var1 ALPHA = OUT}|.
For Adding Leading Zeros
var1 = |{ var1 ALPHA = IN }|.
Getting Data into Variable Depending on Condition.
Old Syntax.
IF cond1.
var1 = fld1.
ELSE.
var1 = fld2.
ENDIF.New Syntax.
var1 = COND #( WHEN cond1 THEN fld1 ELSE fld2 ).
Case Endcase.
Old Syntax.
CASE var1.
WHEN cond1.
var1 = fld1.
WHEN cond2.
var1 = fld2.
ENDCASE.New Syntax.
var1 = SWITCH #( var1 WHEN cond1 THEN fld1
WHEN cond2 THEN fld2 ).
Creating Table and Filling Data into a Table
Old Syntax.
DATA: itab TYPE TABLE OF structure,
wa TYPE structure.wa-fld1 = 1.
wa-fld2 = 'A'.
APPEND wa TO itab.
CLEAR WA.wa-fld1 = 2.
wa-fld2 = 'B'.
APPEND wa TO itab.
CLEAR WA.New Syntax.
DATA(itab) = VALUE structure(( fld1 = 1 fld2 = 'A' )
( fld1 = 2 fld2 = 'B' )).
Adding New entries to Internal Table already having entries.
Old Syntax.
DATA: itab TYPE TABLE OF structure,
wa TYPE structure.wa-fld1 = 1.
wa-fld2 = 'A'.
APPEND wa TO itab.
CLEAR WA.wa-fld1 = 2.
wa-fld2 = 'B'.
APPEND wa TO itab.
CLEAR WA.New Syntax.
itab = VALUE #( BASE itab
( fld1 = 1 fld2 = 'A' )
( fld1 = 2 fld2 = 'B' ) ).
Concatenate for data type apart from C, N, D, T, or STRING. Before 7.4 use to get Syntax Error saying this, but with new Syntax, it’s possible for any Data Type.
Old Syntax.
CONCATENATE 'The Value in inr is' var1 into fld1.New Syntax.
fld1 = |The Value in inr is { var1 }|.
Combining two tables into a single table.
Old Synatx
loop at itab1 into wa1.
append wa1 into itab3.
endloop.
loop at itab2 into wa2.
append wa2 into itab3.
endloop.
New Synatx.
itab3 = itab2.
itab3 = corresponding #( BASE ( itab3 ) itab2 ).
Moving Data from one Container to another.
Old Syntax.
MOVE-CORRESPONDING itab1 to itab2.New Syntax.
itab2 = CORRESPONDING #( itab1 ).
Using the Above Syntax only fields that are identical in the container will be moved to move it to other fields this new syntax can be used.
itab2 = CORRESPONDING #( itab1 MAPPING t1_fld1 = t2_fld1
t1_fld2 = t2_fld2 ).
If some fields need to excluded while moving corresponding fields, this syntax can be used.
itab2 = CORRESPONDING #( itab1 EXCEPT t1_fld3,t1_fld4 ).
Moving Data From Table to other by Splitting it into two fields.
*--Defining Structure
TYPES: BEGIN OF gty_s_rseg_key,
belnr TYPE belnr_d,
gjahr TYPE gjahr,
END OF gty_s_rseg,
gty_rseg TYPE STANDARD TABLE OF gty_s_rseg WITH EMPTY KEY.Old Syntax.
DATA: lt_awkey TYPE gty_rseg,
lw_awkey TYPE gty_s_rseg.LOOP AT lt_bkpf into lw_bkpf.
lw_awkey-belnr = lw_bkpf-awkey+0(10).
lw_awkey-gjahr = lw_bkpf-awkey+10(4).
APPEND lw_awkey to lt_awkey
ENDLOOP.New Syntax.
DATA(lt_awkey) = VALUE gty_rseg( FOR <lfs_bkpf> IN lt_bkpf
( belnr = <lfs_bkpf>-awkey+0(10)
gjahr = <lfs_bkpf>-awkey+10(4) ) ).
Display Message combining two or more Fileds.
Old Syntax.
DATA lv_name TYPE sy-ucomm.
DATA lv_output TYPE char20.
CONCATENATE 'The User ID is' lv_name INTO lv_output SEPARATED BY space.
MESSAGE lv_output TYPE 'E' DISPLAY LIKE 'E'.New Syntax.
MESSAGE |The User ID is { lv_name }| TYPE 'E' DISPLAY LIKE 'E'.
Object-Oriented.
Old Syntax.
DATA: binary_content TYPE solix_tab.
DATA: xl_content TYPE xstring .CALL METHOD cl_document_bcs=>xstring_to_solix
EXPORTING
ip_xstring = xl_content
receiving
rt_solix = binary_contentNew Syntax.
binary_content = cl_bcs_convert=>xstring_to_solix( ip_xstring = xl_content ).
Creating Object For OOABAP.
Old Syntax.
DATA oref TYPE REF TO class.
CREATE OBJECT oref EXPORTING …New Syntax.
DATA oref TYPE REF TO class.
oref = NEW #( … ).
or
with an inline declaration
DATA(oref) = NEW class( … )
Thank you for reading. Stay tuned!