ABAP New Syntax: A Comprehensive Guide with Examples - Sap 4 All

Latest

Please enter your email and get the updates in your inbox.

Thursday, 25 May 2023

ABAP New Syntax: A Comprehensive Guide with Examples


Here's a comprehensive list of various features and enhancements introduced in the new syntax of SAP ABAP, along with examples:

  1. Inline Declarations:

    • Old Syntax: DATA lv_variable TYPE string.
    • New Syntax: DATA(lv_variable) = 'Hello'.

  2. String Templates:

    • Old Syntax: CONCATENATE 'Hello' lv_variable '!' INTO lv_result.
    • New Syntax: lv_result = |Hello { lv_variable }!|.

  3. Null-Safe Navigation Operator (?.):

    • Old Syntax: IF lv_object IS NOT INITIAL.
    • New Syntax: IF lv_object?.

  4. Optional Parameters in Methods:

    • Old Syntax: CALL METHOD lo_object->method.
    • New Syntax: lo_object->method( ).

  5. Method Chaining:

    • Old Syntax: CALL METHOD lo_object1->method1. CALL METHOD lo_object2->method2.
    • New Syntax: lo_object1->method1( )->method2( ).

  6. Method Reference Operator (#):

    • Old Syntax: CALL METHOD lo_object->'METHOD_NAME'.
    • New Syntax: lo_object->#(method_name).

  7. Data Access with VALUE Operator:

    • Old Syntax: DATA lt_table TYPE TABLE OF string. lt_table = VALUE #( ( 'Hello' ) ( 'World' ) ).
    • New Syntax: DATA(lt_table) = VALUE #( ( 'Hello' ) ( 'World' ) ).

  8. Conditional Expressions:

    • Old Syntax: IF lv_condition = 'X'.
    • New Syntax: IF lv_condition = 'X' ? 'Yes' : 'No'.

  9. Field Symbols with ASSIGNMENT Operator:

    • Old Syntax: ASSIGN lv_value TO <fs>.
    • New Syntax: FIELD-SYMBOLS <fs> TYPE any. <fs> = lv_value.

  10. Table Expressions:

    • Old Syntax: READ TABLE lt_table INDEX 1 INTO lv_value.
    • New Syntax: DATA(lv_value) = lt_table[ 1 ].

  11. NEW Operator for Instantiation:

    • Old Syntax: CREATE OBJECT lo_object.
    • New Syntax: DATA(lo_object) = NEW class_name( ).

  12. FILTER Operator for Internal Tables:

    • Old Syntax: LOOP AT lt_table INTO ls_data. IF ls_data-field = 'Value'. APPEND ls_data TO lt_result. ENDIF. ENDLOOP.
    • New Syntax: lt_result = lt_table[ field = 'Value' ].

  13. REDUCE Operator for Aggregation:

    • Old Syntax: LOOP AT lt_table INTO ls_data. lv_total = lv_total + ls_data-amount. ENDLOOP.
    • New Syntax: lv_total = REDUCE #( INIT sum = 0 FOR ls_data IN lt_table NEXT sum = sum + ls_data-amount ).

  14. GROUP Operator for Grouping:

    • Old Syntax: SORT lt_table BY field. LOOP AT lt_table INTO ls_data. IF lv_group <> ls_data-field. lv_group = ls_data-field. APPEND INITIAL LINE TO lt_grouped_table ASSIGNING FIELD-SYMBOL(<fs>). <fs>-group_field = lv_group. ENDIF. APPEND ls_data TO lt_grouped_table. ENDLOOP.
    • New Syntax: lt_grouped_table = lt_table GROUP BY ( field ) ASCENDING INTO TABLE @DATA(result).

  15. Foreach Operator for Internal Tables:

    • Old Syntax: LOOP AT lt_table INTO ls_data. WRITE: / ls_data-field. ENDLOOP.
    • New Syntax: lt_table = VALUE #( FOR ls_data IN lt_table ( ls_data-field ) ).

  16. CLEANUP Operator for Resource Deallocation:

    • Old Syntax: FREE lv_object.
    • New Syntax: CLEANUP lv_object.

  17. Error Handling with EXCEPTIONS:

    • Old Syntax: IF sy-subrc <> 0. RAISE EXCEPTION TYPE cx_exception. ENDIF.
    • New Syntax: TRY. ... CATCH cx_exception INTO DATA(lx_exception). ... ENDTRY.

  18. Annotations for Metadata:

    • Old Syntax: No direct support for annotations.
    • New Syntax: @EndUserText.label: 'My Field'. field1 TYPE string.

  19. ABAP Channels for Asynchronous Communication:

    • Old Syntax: No direct support for ABAP Channels.
    • New Syntax: DATA(lo_channel) = NEW zcl_abap_channel( ). lo_channel->publish( 'MESSAGE' ).

  20. ABAP RESTful Application Programming Model (RAP) for Building RESTful Services:

    • Old Syntax: No direct support for RAP.
    • New Syntax: CLASS zcl_my_service DEFINITION PUBLIC FINAL CREATE PUBLIC. PUBLIC SECTION. INTERFACES: i_rap_application_provider. ENDCLASS.

These examples demonstrate various features and enhancements in the new syntax of SAP ABAP, introducing more concise, expressive, and efficient ways of writing ABAP code.

No comments:

Post a Comment