Professional Documents
Culture Documents
Inline Declarations
I would like to start with the inline declarations which is one of the major changes
introduced in release 7.40. Inline declarations are a new way of declaring variables and
field symbols at operand positions in your ABAP statements. By using the new
declaration operator DATA(…) or FIELD-SYMBOL(…) you can declare your variables
on the fly and it will speed up your development significantly as you don’t have to
specifically type all your variables. It can be used in a lot of different circumstances of
which some are shown in the examples below.
Simple assignment of a left hand side variable. In this example we use a string template
on the right hand side so the variable LV_PARAMETER will be of type STRING.
Declaration of work areas / field symbols in loops or other internal table operations. In
the example below de variable LS_MESSAGE will be of the line type of the internal
table LT_MESSAGE_TABLE (you could have used the ASSIGNING FIELD-
SYMBOL(…) here as well!)
Declaration of reference variables. As you can see in the BOL programming example
below the inline declaration works for reference variables as well. In this example the
variable LR_CAMPAIGN_DATES will hold an instance of class CL_CRM_BOL_ENTITY
It may at first glance seem like a small improvement but once you start using this
feature (especially in combination with the other new features) you will see the benefits
and you will use it all the time. There is however a small price to pay, in the last
example above you will lose the possibility to directly navigate to underlying classes by
double clicking on the variables.
Constructor Expressions
Another useful feature introduced in the ABAP 7.40 release are the constructor
expressions.
Rather cryptic right But I will try to clear things up in the examples below. Currently the
following constructor operators exist:
All of the constructor operators mentioned above have their benefits but I will highlight
some of them I find particularly useful or interesting in the examples below.
I often use the NEW constructor operator as it gives me, in combination with the inline
declaration functionality, a quick and easy way to instantiate classes. Nothing
spectacular but convenient nonetheless.
I’m also a big fan of the VALUE constructor operator as it allows me to quickly fill
internal table structures without excessive use of data declarations and using work
areas and such. In the example below there is a quick and clean way to add an
additional button to a WebUI component.
You can also use it to build more complex nested tables as shown in the example below
where I fill the input fields table for the famous CRM_ORDER_MAINTAIN function
module in one compact statement. It is also convenient that the new constructor
expressions combine nicely with the inline declarations (I bet that’s not a coincidence)
The filter operator does what its name suggests it filters the content of an internal table.
As a prerequisite, the filtered table must have a sorted or a hash key (primary or
secondary), that is evaluated behind the WHERE clause. I have to admit that I don’t use
this option not very often but I wanted to show it here anyway. In the example below the
internal table LT_FILTERED will contain only the entries that match with a certain
partner function.
The casting operator is a constructor operator that executes an up or a down cast for
reference variables with the type specified. Again not a statement I use very often but
you will encounter it now and then and this is one of the statements that you have to
look over twice before you see what is going on. In the example below I use an up cast
twice and again the results can be conveniently combined with inline data declarations.
The SWITCH operator is closely related to the COND operator but it works not with a
logical expression but with a case differentiation. The COND and the SWITCH operator
can also be used to assign a value to an input parameter of a method. In the example
below I read an OTR text based on the language present in the variable
MYLANGUAGE which is actually pretty cool.
Also note that if you are tired of only catching exceptions you can throw exceptions now!
Table Expressions
In ABAP release 7.40 the new table expression itab[…] is introduced and enables you
to access internal table data and it can be used to replace the good old READ TABLE
statement. In the diagram below I will explain its use by comparing the new itab[…]
statement with the old READ TABLE statement
At first glance it seems that the itab[…] is just doing the same as the READ TABLE
statement so why introduce it. But there are some differences.
With the ITAB[…] statement you have the possibility to chain the table expressions and
this allows you to pinpoint directly to a certain record or even a single field in complex
nested table structures. A nice example can be found here.
I do use table expressions regularly because it saves you a lot of coding to achieve
certain goals while working with internal tables in ABAP. This is best illustrated in the
example below.
String templates (and embedded expressions)
Technically speaking string templates were first introduced with ABAP release 7.31 and
allow you to do a whole range of useful string operations. If you want to see its full
potential I would recommend you take a look at the ABAP program
DEMO_EXPRESSIONS which gives a nice overview of all the available possibilities.
The result of a string operation is however always a character string and it can be used
directly as input in method calls with character based input fields. In the example below
we pass a variable which is defined as type P with two decimals to a character based
input variable. Without the curly brackets this will never activate let alone run, wrapping
it in a string template we have no issues at all.
And you can even extend it further and place the result of another method call directly in
the string template and combine it with some embedded expressions and pass it on as
input. No problem at all.
Of course these examples are making no sense at all but syntactically they are correct
and activating them is giving no errors whatsoever. It does however show the power of
using string templates and it is yet another way to make your coding more structured
and compact and that is why I like using them.
For more detailed information about SAP NetWeaver AS for ABAP 7.5 this landing
page is probably a good place to start.
In this second part of this blog I want to focus again on the practical application of some
of the new features of this ABAP release and provide examples that will give you an
idea what you might expect.
Is instance of
The first interesting addition is the new relational expression ‘IS INSTANCE OF’ which
allows you to check whether a reference variable of a given static type is pointing to a
certain object.
I can imagine that it is something I will use. In the example below a check is done to
determine if variable PARAM is a table, a structure or referring to a data element.
You can also use a similar approach in a CASE like construction as shown below where
I try to determine what kind of context node we are dealing with in a WebUI scenario.
If you wanted to achieve something similar before the 7.50 release you had to use a
casting operation inside a TRY …. ENDTRY construction.
Corresponding
The next feature is nice but I don’t see it as something I will user very often. In ABAP
release 7.40 a new constructor operator CORRESPONDING was introduced that allows
an explicit mapping of structure components with different names. Something that
wasn’t possible with the classic MOVE-CORRESPONDING statement. In release 7.50 a
new system class CL_ABAP_CORRESPONDING has been introduced which makes it
possible to assign components of structures or internal tables with dynamically specified
mapping rules.
As I said before this isn’t something I see myself use very much, but there might be
some circumstances that it will be useful. There are some nice examples in the
standard SAP help you can find here.
In the example below a table expression is evaluated as a host expression on the right
side of a where condition. The result is then used for the comparison with the column
specified on the left side.
The previous example I copied from the SAP Help but to show you the flexibility of host
expressions I came up with an example of my own where the host expression is
introduced in the UP TO n ROWS statement using the switch conditional operator. As
you can see the sky is the limit!
Beside host expressions it also possible the use a wide variety of SQL expressions now
in Open SQL. For example an arithmetic expression on the left hand side of a WHERE
condition.
But you can introduce a lot more complexity using SQL expressions. For a complete
overview I would like to refer to the standard SAP documentation which is full of
comprehensible examples.
Conclusions
In general, working with the new possibilities in ABAP will allow you to create more
efficient code with less effort. When you start working with the new features you will see
that getting used to them almost comes naturally and before you know it they will
become a second nature to you. As far as I can see there is only one possible drawback
of all the possibilities that are available and how they interact with each other. You can
make your code unnecessary complex. Maybe you can remember the moment when
Spiderman was told by his uncle: “With great power, comes great responsibility”. This
actually might apply to the modern ABAP developer as well, because with all
possibilities SAP is providing, in combination with chaining options and the possibility of
adding code at operand positions, there lurks the danger that your code
becomes obfuscating. If you don’t know what that means the examples below might
give you a clue.
SAP NetWeaver 7.50 SP1 AS ABAP and SAP BW on SAP HANA[Developer Edition]
How to Guide
References