Wednesday, May 4, 2016

Next level ABAP Development: Creating efficient code

The technical innovations in the SAP world are following each other in rapid succession. It should therefore not come as a surprise that even our trusty ABAP language is not left untouched. A lot of new cool features have been introduced in the ABAP 7.40 release which has been around for quite some time now and with the upcoming 7.50 release even more ammunition can be added to your ABAP arsenal. Being an ABAP developer myself I find it important to keep track of these innovations so that I can benefit from the new possibilities they give me and allow me to create more efficient code in the process. In this blog I will first give you some practical examples of the new possibilities in the ABAP 7.40 release which I use daily and make my life as an ABAP developer a lot easier. In the second part I will dive deeper into some of the new features of the ABAP 7.50 release and give some practical examples of how they can be applied.

ABAP Features for Release 7.40

The ABAP release 7.40 is around for some time now and as an ABAP developer it is bound to cross your path sooner or later. In this first part of my blog I want to focus on the practical use of the new features available in the ABAP 7.40 release that allow you to write more compact and efficient code. In this blog I will not try to cover all the new features introduced in the ABAP 7.40 release as it will actually take several blogs to cover it completely. I will start right away with some examples, but if you want some more background information I recommend you to check the following links:
ABAP News for Release 7.40 - What is ABAP 7.40? 
SAP NetWeaver AS ABAP 7.4 - Overview and Product Highlights

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.

SAP defines them as follows:

“A constructor expression consists of a predefined constructor operator, a data type or object type that matches the operator and that can be derived implicitly from the operand position using #, and type-specified parameters specified in parentheses. Each constructor expression creates a result whose data type is determined using the specified type. The parameters specified in parentheses are used to pass input values”
Rather cryptic right But I will try to clear things up in the examples below. Currently the following constructor operators exist:

  • The instance operator NEW 
  • The value operator VALUE 
  • The conversion operator CONV 
  • The casting operator CAST 
  • The reference operator REF 
  • The component operator CORRESPONDING 
  • The lossless operator EXACT 
  • The reduction operator REDUCE 
  • The filter operator FILTER 
  • The conditional operators COND and SWITCH

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.

The instance operator NEW

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.

The value operator VALUE

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 FILTER

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 CAST

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 conditional operators COND and SWITCH 
COND and SWITCH are so called Constructor operators. Constructor operators are used in constructor expressions to create a result that can be used at operand positions. The COND statement I use a lot and it constructs a result of the specified type based on logical expression. In the example below I determine a process type (Delete, Update or Insert) based on certain conditions. The result is variable LV_PROCESS of type string.

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.

Another differences is that if a table line is not found, the exception CX_SY_ITAB_LINE_NOT_FOUND is raised and if you don’t catch this exception a short dump will occur. So you can’t check for sy-subrc.

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.

We can also add some additional formatting.

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.

ABAP Features for Release 7.50

Now that we have had an impression of the possibilities of the 7.40 release it is time to move on to the latest ABAP 7.50 release which is the underlying ABAP platform for the SAP S/4HANA on-premise edition, SAP Business Suite Enhancement Package 8 and SAP Business Warehouse 7.5. If it will find its way to other SAP products such as for example CRM on premise is not clear at the moment. But time will tell.

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.


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.

Host and Other Expressions in Open SQL

In ABAP release 7.40 (SP05) a new foundation for Open SQL was laid by introducing a new SQL parser into the ABAP runtime environment. In fact the role of Open SQL in the ABAP framework has changed as is explained in detail in this excellent blog. It will go too far to explain all new Open SQL features here but I want to show some examples of what new features are introduced in ABAP release 7.50. Starting with host expressions, Host expressions are ABAP expressions that can be specified in operand positions in Open SQL.This really opens up a lot of possibilities you never had before. For a complete list of operand positions for host expressions I like to point to the ABAP Keyword Documentation on this subject.

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.


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.

In the end, it is up to you to choose where to use inline declarations, constructor expressions and so on. If it is important to keep the code readable and debuggable, feel free to use some more code than strictly required... Or try using comments in the coding. That still works. :-).

Setting up your own trial environment

I can imagine you have become excited about the new possibilities of both the ABAP 7.40 and the 7.50 release and that you can’t wait to get some hands-on experience yourself. Luckily there is a trial environment readily available in the SAP Cloud Appliance Library which can be set up quite easily. All you need is an S-user and a credit card because the services is, however very cheap, not entirely free of charge. The link below will take you directly to the page where you can set up your own environment.

SAP NetWeaver 7.50 SP1 AS ABAP and SAP BW on SAP HANA[Developer Edition]
How to Guide


SAP NetWeaver AS ABAP 7.4 - Overview and Product Highlights
ABAP Language News for Release 7.40
ABAP 7.40 Quick Reference
SAP NetWeaver AS for ABAP 7.5 – Overview
SAP NetWeaver AS for ABAP 7.5  - One ABAP Platform for SAP Business Suite and SAP S/4HANA, on-premise edition
ABAP Language News for Release 7.50
Developer & Trial Editions: SAP NetWeaver Application Server ABAP and SAP Business Warehouse powered by SAP HANA