New Features in ABAP 7.4 – Conditional Logic

Anthony 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 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.

The continuing theme in all of these posts, is how we can make our code thinner, more readable and transparent. Well, nothing clogs up the visual readability of your code like long IF-THEN-ELSE-ENDIF or CASE-WHEN-ENDCASE constructs. Well, in ABAP 7.4 we have some new Constructor Operators we can use in Constructor Expressions that will make our code easier to read, more compact, and safer to maintain.

You remember Constructor Operators… We discussed them in the last blog post New Features in ABAP 7.4 – Calling Methods and Functions. In That post we highlighted the CONV Constructor Operator and how we could use it to convert a local variable to a STRING. In this post, I’d like to talk about the Constructor Operators COND and SWITCH. Again, for a complete list see the SAP ABAP 7.4 help for more information.

In ABAP 7.4, you can achieve the same thing, but you can do this in a more economical way by using the COND constructor operator. This also means 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 fly!

With ABAP 7.4, we can simplify and accomplish the same thing by by using the new SWITCH constructor operator instead of the CASE sttatement. Like so…

DATA(L_DAY) = SWITCH char10( l_indicator
when 1 THEN 'Monday'
when 2 THEN 'Tuesday'
when 3 THEN 'Wednesday'
when 4 THEN 'Thursday'
when 5 THEN 'Friday'
when 6 THEN 'Saturday'
when 7 THEN 'Sunday'
ELSE THROW zcx_day_problem( ) ).

Also note that if you are tired of only catching exceptions you can throw exceptions now! The usage is identical to the RAISE EXCEPTION TYPE, however, the compiler evaluates the keywords RAISE EXCEPTION TYPE and THROW as if they were one and the same.

For quite some time the lack of a real Boolean type in ABAP led to a lot of discussions amongst developers. I would see post on the SCN community constantly asking why a developer couldn’t write..

IF meth( … ).
…
ENDIF.

The answer from SAP was always “Because you have to have a Boolean type behind IF and no method can return a Boolean type.” But, now in Release 7.40, SP08 You can write predicative method calls as shown below..

… meth( ) …

as a short form of the relational expression

… meth( ) IS NOT INITIAL …

So you can place simple functional method calls everywhere, where logical expressions are allowed: behind IF, CHECK, ASSERT, COND, SWITCH, … Here is an example…

IF zcl_system=>is_production( ).
"In production we never want a short dump
TRY.
zcl_application=>main( ).
CATCH cx_sy_no_handler INTO gcl_no_handler.
ENDTRY.

The reason this works is spelled out completely in Paul Hardy’s Book ABAP to The Future. Paul explains, “What’s happening from a technical point of view is that if you don’t specify anything after a functional method the compiler evaluates it as IS_PRODUCTION( ) IS NOT INITIAL. An ABAP_TRUE value is really the letter X, so the result is not initial, and so the statement is resolved as true.”

Another common situation with respect to Boolean logic in ABAP, is when you want to send a TRUE/FALSE value to a method or get such a value back from a functional method. For instance, did you ever stumble over this one?

Guess what? The relational expression “boolc( 1 = 2 ) = ABAP_FALSE” is false! Why? Because BOOLC despite its name does not return c but a string and the comparison rules for c and string blah, blah, blah …

Technically BOOLC( 1 = 2) evaluates to a string containing a blank of length 1. So far so good. But comparing ` ` with ‘ ‘ or ABAP_FALSE is false, since the text field is converted to string resulting in an empty string.

Now in ABAP 7.4, a new built-in function was added, called XSDBOOL, which does the same thing as BOOLC but returns an ABAP_BOOL type parameter.

The relational expression xsdbool( 1 = 2 ) = abap_false is true, because xsdbool returns type XSDBOOLEAN from the ABAP Dictionary that is – yes, you guess it – c of length 1. For the experts among you, XSDBOOLEAN is normally used for special mappings in XML-transformations and was reused here for quite another purpose. And that’s were the funny name xsdbool comes from.

Again, I highly recommend reading Horst Keller’s Blog on all the ABAP 7.4 enchantments to the code base.

Summary

With the new conditional code constructs in ABAP 7.2 and ABAP 7.4 we have the ability to create ABAP code with fewer statements for the same functionality, without compromising readability of the code. New boolean functions like XSDBOOL, and new conditional operators such as SWITCH and COND allow us more flexability and extensibility when developing and maintaining our ABAP code.

If you enjoyed this blog, New Features in ABAP 7.4 – Conditional Logic, 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

Gallery image with caption: 10+ New Features in S4HANA Finance Group Reporting 1909

Gallery image with caption: How a Platform Technology like BPM Can Accelerate Your DX Journey

The tools of the New ABAP Debugger are optimized for the different debugging situations you may have to deal with during the course of solving a problem. Some of the tools you will recognize from the Classic ABAP Debugger; others are brand new. Before we look at a few of the tools and how to use them, lets first get comfortable with the tools menu and how the tools are organized.

With all their new features, the latest versions of the ABAP Editor, I thought it best to appreciate the new features; we should first go back in time to see some of the limitations that were present in prior versions of the editor.

SAP NetWeaver 7.0 (formerly 2004s) offers the new Enhancement Framework that not only is intended to unify the modification and classic enhancement techniques, but also offers you almost the same flexibility as modifications without the limitations of modifications.

In contrast to enhancements in the Enhancement Framework, modifications are physically part of the object they modify. This means that every single modification gets lost in an upgrade and needs to be re-inserted even in cases where the underlying SAP object has not changed at all!

In all the aspects, enhancements are more powerful than modifications, even given all the modification support that transaction SPAU offers. The reason is due to an important difference between an enhancement and a modification that stems from the conceptual difference between the two technologies.

In addition to the new kernel-based BAdI, there are many other types of enhancement options that are part of the new Enhancement Framework. You implement these enhancements using the Enhancement Framework tools, which are integrated into the ABAP Workbench

No matter if you create an enhancement point or a BAdI as an enhancement option provider, or if you implement an existing enhancement option (be it implicit or explicit), what you create must fit into the structure of the Enhancement Framework in a way that enables you to collect and organize the enhancement options and their counterparts on the implementation side.

The Controls Technology Framework resides on the application server (the back-end), and the Automation Controller sits on the presentation server (the front-end). The integral component that optimizes the communication between the two is the Automation Queue.

The ALV Grid Control solves this problem. Its user interface provides a set of generic functions (e.g., sorting, filtering) for handling tabular data. It also confers the many benefits of controls technology to users, enabling more operations by mouse, and interaction with other controls, such as drag-and-drop. Developers simply plug the ALV Grid Control into their applications and the tool takes care of the rest. You do not need to do any further programming to offer users these functions. How you “plug” the control into an application is the first thing I will show you in this month’s blog. So lets get started!

he key to configuring the ALV Grid Control for your particular application is the structures that are passed by the application to an ALV Grid instance before or during list display. For some simple extensions of your ALV Grid instance, you only need to set the right parameter and pass the table or structure by using method set_table_for_first_display.

Adding new GUI elements to an ALV Grid instance is event-controlled and requires experience in ABAP Objects event handling. There is an event for each element type (toolbar push button, toolbar menu, and context menu). In the relevant event handler method, you define the properties of an element (such as its menu options) and assign a function code to each executable function.

What is web Dynpro? It’s SAP’s newest user interface (UI) development option for the SAP NetWeaver platform — has been designed to become the de facto option of choice for SAP development. Web Dynpro was created because, like every other software vendor in the Web space, SAP needed a longterm, strategic solution for the many problems faced by Web developers during the implementation of browser-based business applications.

Pin It on Pinterest

Share This

THIS WEBSITE USES COOKIES: By clicking "Accept", you agree to the use of cookies and similar technologies to enhance your browsing experience, and analyze your engagement with our content. Read our Privacy Policy.