New Features in ABAP 7.4 – Declaring and Creating Variables

97be63abb66b42f281dff9197b6b35da_WglrP9Anthony Cecchini is the President of Information Technology Partners (ITP), an SAP consulting company headquartered in Pennsylvania. ITP offers comprehensive planning, resource allocation, implementation, upgrade, and training assistance to companies. Anthony has over 20 years of experience in SAP R/3 business process analysis and SAP systems integration. His areas of expertise include SAP NetWeaver integration; ALE development; RFC, BAPI, IDoc, Dialog, and Web Dynpro development; and customized Workflow development. You can reach him at ajcecchini@itpsap.com.

Declaring and Creating Variables in ABAP 7.4

In this blog installment, I’d like to introduce you to a new feature called Inline Declarations. It’s a very simple feature, but very useful, and can make our code base smaller and easier to understand. Inline declaration means, that you declare your local variables as embedded in the given context, instead of declaring them separately at the beginning of program (i.e. the TOP INCLUDE).

ABAP 7.4 Data Type Declarations

As stated in the book ABAP to the Future, the compiler knows what data type it wants, in fact it has to know in order to be able to perform a syntax check, so why not let it decide the data type of your variable and create it? So instead of declaring it yourself, let the compiler declare it for you.. lets look at some examples.

Before ABAP 7.4

DATA: lv_vehicle TYPE string.
lv_vehicle = 'Mercedes'.

What if instead, you could code this…

With ABAP 7.4

DATA(lv_vehicle) = 'Mercedes'.

OR this…

Before ABAP 7.4

DATA: lv_rows TYPE i.
lv_rows  = LINES( itab)

Becomes…

With ABAP 7.4

DATA(lv_rows) = LINES( itab ).

What about field symbols? Well For field symbols there is the new declaration operator FIELD-SYMBOL(…) that you can use now. Lets look at the 3 examples of how to use this new operator below…

With ABAP 7.4

ASSIGN ... TO FIELD-SYMBOL().

LOOP AT itab ASSIGNING FIELD-SYMBOL().
...
ENDLOOP.

READ TABLE itab ASSIGNING FIELD-SYMBOL() ...

Using The “NEW” Constructor Operator in ABAP 7.4

With Release 7.40 ABAP supports so called constructor operators. Constructor operators are used in constructor expressions to create a result that can be used at operand positions. The syntax for constructor expressions is

... operator type( ... ) ...

“NEW” is a constructor operator. “TYPE” is the explicit name of a data type. Inside the parentheses specific parameters can be specified.

So looking at some examples, for ABAP OO creating an instance …

Before ABAP 7.4

DATA lo_human TYPE REF TO class_human.
CREATE OBJECT lo_human EXPORTING NAME = 'TONY'.

With ABAP 7.4

lo_human = NEW class_human( name = ‘TONY’ ).

And for Data objects…

Before ABAP 7.4

DATA: lv_rows  TYPE i.
lv_rows  = 0.

With ABAP 7.4

lv_rows = NEW i(  0  ).

Using The “VALUE” Constructor Operator in ABAP 7.4

The VALUE constructor operator works similarly to the NEW Operator to create the ITAB entries. Using the VALUE operator, the itab would be initialized and records would be inserted. let’s look at an example, first we will create an table type…

TYPES t_itab TYPE STANDARD TABLE OF i WITH DEFAULT KEY.

Before ABAP 7.4

DATA itab_o TYPE t_itab.
APPEND: 10 TO itab_o,
        20 TO itab_o,
        30 TO itab_o.

With ABAP 7.4

DATA(itab) = VALUE t_itab( ( 10 ) ( 20 ) ( 30 ) ).

OK, so let’s take a look at these inside the debugger. As you can see we accomplished the same goal with way fewer lines of code.

abap-740-value-debug

Using the “FOR” Iteration Expression in ABAP 7.4

What is an Iteration Expression? Well you and I do not normally hard code our internal table entries as shown above. This is not feasible for large tables, and hard coding is generally frowned upon as a practice. Usually we fill our internal tables by reading the SAP database. We also filled one internal table from another, and could only do this if the columns were the same in the source and target internal tables. So prior to ABAP 7.4 you had to add all the lines of one table to another or do an assign as depicted below…

APPEND LINES OF lt_itab1 TO lt_itab2.
lt_itab2[] = lt_itab1[].

Now that the FOR command has been introduced in ABAP 7.4, you can achieve this in a much easier way, and the tables can have different columns, and you can filter or limit what gets transferred using conditional logic with the VALUE and FOR keywords. So what does FOR do? let’s examine the syntax:

 FOR wa| IN itab [INDEX INTO idx] [cond]

This effectively causes a loop at itab. For each loop the row read is assigned to a work area (wa) or field-symbol(). This wa or is local to the expression i.e. if declared in a subrourine the variable wa or is a local variable of that subroutine. Index like SY-TABIX in loop.

lets look an an example below…

Lets say we are given the following to start:

TYPES: BEGIN OF ty_ship,
           tknum TYPE tknum,     "Shipment Number
           name  TYPE ernam,     "Name of Person who Created the Object
           city  TYPE ort01,     "Starting city
           route TYPE route,     "Shipment route
       END OF ty_ship.
 TYPES: ty_ships TYPE SORTED TABLE OF ty_ship WITH UNIQUE KEY tknum.
 TYPES: ty_citys TYPE STANDARD TABLE OF ort01 WITH EMPTY KEY.

GT_SHIPS type ty_ships. -> has been populated as follows:

ABAP 740 FOR Command

We want to populate internal table GT_CITYS with the cities from GT_SHIPS.

Before ABAP 7.4

DATA: gt_citys TYPE ty_citys,
      gs_ship  TYPE ty_ship,
      gs_city  TYPE ort01.

LOOP AT gt_ships INTO gs_ship.
  gs_city =  gs_ship-city.
  APPEND gs_city TO gt_citys.
ENDLOOP.

With ABAP 7.4

DATA(gt_citys) = VALUE ty_citys( FOR ls_ship IN gt_ships ( ls_ship-city ) ).

OK, now lets throw some conditional logic into the mix. The goal now is to populate internal table GT_CITYS with the cities from GT_SHIPS where the route is R0001.

Before ABAP 7.4

DATA:  gt_citys TYPE ty_citys,
       gs_ship  TYPE ty_ship,
       gs_city  TYPE ort01.

LOOP AT gt_ships INTO gs_ship WHERE route = 'R0001'.
  gs_city =  gs_ship-city.
  APPEND gs_city TO gt_citys.
ENDLOOP.

With ABAP 7.4

DATA(gt_citys) = VALUE ty_citys( FOR ls_ship IN gt_ships
        WHERE ( route = 'R0001' ) ( ls_ship-city ) ).
ITP logo

If you enjoyed this blog, New Features in ABAP 7.4 – Declaring and Creating Variables, please fill out the form below to sign up for our newsletter. We deliver SAP Technical tips & tricks, SAP news, and the current month’s BLOG right to your inbox!

Related Posts

Pin It on Pinterest

Share This

If you enjoyed this post, why not share it with your friends!