New Features in ABAP 7.4 – Declaring and Creating Variables

Recommend This Post!Tweet about this on TwitterShare on FacebookShare on LinkedInShare on Google+Pin on Pinterest

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!

Recommend This Post!Tweet about this on TwitterShare on FacebookShare on LinkedInShare on Google+Pin on Pinterest

Related Posts

Comments (5)

[…] We have all been there… you are developing an SAP ABAP program and you declare some local variables to be passed into a method, cross your fingers, and hope you have declared the variable the same as the input parameter. If not, you get an error. Worse, if it was a function module, you get a short dump. Well ABAP 7.4 can address at least the IMPORT side of the problem, using the new DATA TYPE declaration operator DATA(… ) we learned about in New Features in ABAP 7.4 – Declaring and Creating Variables. […]

[…] that you do not have to keep specifying the target variable again and again. We will also add an ABAP INLINE Declaration… by using the DATA statement to create the variable lv_text inline on the […]

[…] In release ABAP 7.4, the syntax for reading into a work area and looping through a table can now leverage INLINE DECLARATIONS, we discussed these in a prior ABAP 7.4 blog. […]

This is pretty cool though I would never use work-areas as a rule. Ive always gone the way of field symbols to perform itab copies. This alone was met with no small protest on some sites with people demanding to know how the code worked. There are still a lot of people using itabs with headers…..which is to say they are not using Objects. In fact Ive only come across one site that was making any effort toward adopting OO as standard.

I rent server space to practice all this stuff. Its not expensive but it would be nice to get to use things like OO in anger to get truly comfortable with it. I did my first Objects course in 2003 and only by my own efforts managed to keep the knowledge alive. Had a brief encounter with CRM back in 2012 but apart from that….nothing. Just goes to show that even excellence is no guarantee of take-up.

As for the new stuff. Expression like :

LOOP AT TABLE ty_mytab ASSIGNING FIELD-SYMBOL().

works just great. Its great and the definition is local to the loop.

Expressions like

DATA: (myvar) = ‘hello’. Gives a character field of 5.
DATA: (yourvar) = ‘hello you’. Gives a character field of 9.

Since none of us use hard-coded entries then the ‘myvar’ and ‘yourvar’ have to be fed from another source. If that is a table then its going to be in a loop…..where the code-check will give an ‘already declared’ error message. Besides, you are stuck with the length of the first data item posted.

NB : Only tested this once.

ABAP has changed a great deal since I took a Workbench course in 2000. The take up on a lot of changes has been abysmally slow overall. Ive heard lots of reasons for this : Older programmers (ha!), reluctance to learn, too much legacy code, no resource to re-engineer etc.

My view is that the changes are seen as zero or even negative benefit and business exists for profit, their own and not any desire to be seen with the latest greatest version of an already world-leading software.

Phillip, a lot has to do with the leads and O&M team on your projects. It took some convincing, but I was able to get a team I worked with to embrace OO after I showed them some of the advantages of encapsulation, reducing redundancy…etc. For instance, we had around 150 interfaces we needed to create Tech specs for, but a good deal of the data requirements and validation were still being discussed. The I/F lead had a deadline to meet with the specs. I convinced them to create a central class to validate and enrich the data. Then inside each I/F tech spec, point to that object in the sections on validation and enrichment. This allowed him to write the tech specs and deliver them on time, reach a milestone, get paid…etc It allowed so much more as we centralized all the validations and selects using a factory pattern and an interface with a set of defined methods everyone used come build. Once he was able to actually reap the rewards, he was much more open from that point forward. For instance, each time we created a new z-table, we created a wrapper class that everyone would use to validate, or update the ztable. Again this reduced redundant coding. It also allowed me to create change doc objects and record changes without having to teach new developers (unless they wanted to learn – I strongly believe in mentoring). Try and find a win for management, see if that greases the wheel! AND THANK YOU FOR THE EXCELLENT POST!!

Comments are closed.

Pin It on Pinterest

Share This

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