TIP of the Day #ABAP
Arun Kumar Solaiyan
SAP ABAP | S4 HANA | RAP | BTP | Integration suite CPI and PI
Read internal table in Modern ABAP
New ABAP features from Netweaver 7.40 allows the developers to read an internal table in much easier way. There are few points to be considered while following this new syntax.
Before Netweaver 7.40, 'Read table' statement is used to read data from an internal table. This statement also sets sy-subrc and sy-tabix depending on Read statement execution, offers more features like Binary search.
$OLD ABAP
READ TABLE lt_ekko INTO wa_ekko WITH KEY EBELN = '100000001'
BINARY SEARCH.
IF SY_SUBRC = 0.
WRITE : wa_ekko-ebeln.
ENDIF.
$NEW ABAP
wa_ekko = lt_ekko [ EBELN = '100000001']
New syntax offers better readability and ease of use, but statement will result in unexpected system error with exception 'CX_SY_ITAB_LINE_NOT_FOUND' if no data matching the key parameters. Always use new syntax in Try..Catch block to ensure exceptions are handled.
New features like LINE_EXISTS, LINE_INDEX can also be used along with new read syntax.
$NEW ABAP - OPTION 1
TRY
DATA(wa_ekko) = lt_ekko [ EBELN = '100000001' ]
WRITE : wa_ekko-ebeln.
CATCH CX_SY_ITAB_LINE_NOT_FOUND.
WRITE : 'NO RECORD FOUND'.
ENDTRY
OPTION 2
IF LINE_EXISTS( lt_ekko [ EBELN = '100000001' ] )
DATA(wa_ekko) = lt_ekko [ EBELN = '100000001' ]
WRITE : wa_ekko-ebeln.
ELSE.
WRITE : 'NO RECORD FOUND'.
ENDIF.
Either option 1 or option 2 can be followed with new syntax to avoid unexpected system error.
ABAP TIP of the Day: I will be sharing my learnings in ABAP and Modern ABAP through the series ABAP TIP of the Day. Hope this helps our developer community. Let's share and learn together!!
Integration Managing Consultant - BTP Integration Suite|| PI/PO/EDI || SAP Certified Cloud Integration Associate || Workato Automation Pro 2 Certified || Jitterbit Certified Consultant || Celigo Certified Consultant
1 年Good job bro
SAP ABAP | S4 HANA | RAP | BTP | Integration suite CPI and PI
1 年ABAP Tip of the day #002