How to not screw up when returning a REF TO DATA

images/thumbnail.jpg - Thumbnail

I’ve been using more and more references in ABAP.

I used to use REF TO only for classes but I’ve been finding more and more advantages in using them for other data types. But, just like in C++, care must be used when dealing with data references. Things can easily go very wrong.

In this article, I’ll try to show you how to use and how not to use REF TO DATA. Let’s start by an example of how not to do it.

The curious case of the partially protected structure

images/thumbnail.jpg - Thumbnail

The other day I was trying (and failing) to modify a line of a SORTED TABLE.

Weird things happen when you pass SY-TABIX as a parameter

images/thumbnail.jpg - Thumbnail

The other day a very strange thing happened to me. I was sending SY-TABIX as a parameter into a method. Before the call it contained 1 but, once inside, the parameter’s value was 0. Weird. o_thingalizer->very_strange_thing(sy-tabix). I was hearing “Why?! Why?!” repeatedly inside my head. But suddenly I understood it! When you send some field of the SYST as a by parameter you have to pass it by value and not by reference.

Convert class exception to BAPIRET2

images/thumbnail.jpg - Thumbnail

Some time ago I wrote an article explaining a way to automatically convert classic exceptions to exception classes. Today I use this technique in almost all the exception classes that I create (and I’ve even improved it, but I’ll leave that for another post).

This tip explains the exact opposite.

Ignore indentions when comparing version

images/thumbnail.jpg - Thumbnail

Sometimes, when comparing versions, several differences show up even when we did not make changes to the code. These differences may simply be a consequence of a Pretty Printer that altered code indentations. This makes comparing the versions confusing. But ABAP Workbench lets you configure the diff tool to ignore indentations and even comments. Just press F5 while there or go to the “Display Format> Settings” menu and adapt it to your needs:

Use a Standadrd GUI status without copying it

images/thumbnail.jpg - Thumbnail

When you want to use CL_GUI_ALV_GRID in a CONTAINER you must copy a standard Status GUI from another program. For example, the GUI Status “STANDARD” of the function group SALV. And then, in the screen’s PAI you do:

SET PF-STATUS ‘STANDARD’.

And thus the little standard buttons show up on your screen.

ABAP popular saying

images/thumbnail.jpg - Thumbnail

Don’t put off until tomorrow what you can do SY-DATUM.

Create templates in SE80 by dragging stuff into the code

images/thumbnail.jpg - Thumbnail

While coding, it’s always good to have ways to speed up what we’re doing. A good one is automatic generation of templates for function and method calls.

I know 3 ways to achieve this.

Local $PACKAGES

images/thumbnail.jpg - Thumbnail

Any object created in SAP must belong to a package.

Until recently, whenever I needed to create a program for a quick test I’d put it in the $TMP package. This way I was sure that it would never be transported to another system.

But sometimes I have the need to create stuff in the development system which, even though it should never be transported, should stay there forever. For example, development tools like ZSAPLINK and abapGit. But if we put everything under package $TMP it will soon be a big mess.

DDIC structure key

images/thumbnail.jpg - Thumbnail

Today when I want to create a SALV I always create a DDIC structure with the data elements already set to show the texts I need. This avoids the need for code to manipulate the field catalog.

Find everything at once

images/thumbnail.jpg - Thumbnail

The ABAP editor is full of functionalities yet to be discovered.

When I do CTRL-F to search for text, the option I usually choose is “Find next”.

APPEND STRUCTURES do more stuff than I thought

images/thumbnail.jpg - Thumbnail

During all my ABAPian life I was convinced that APPEND STRUCTURES had one single purpose: to add new extra fields to an existing standard table.

But I recently learned that they let you do two more things after all:

INSERT wa INTO itbl REFERENCE INTO ref. Bug?

images/thumbnail.jpg - Thumbnail

Every day I use more reference variables in ABAP. First I used REF TO just for classes but, as I become more familiarized with its advantages, I start using them more and more for data structures, instead of field-symbols.

But I recently found na unfortunate behavior of the following command:

INSERT wa INTO itbl REFERENCE INTO ref.

Let me give you some context before I complain about it.

We should be accountable for the crap we make

images/thumbnail.jpg - Thumbnail

If, when building a bridge, a civil engineer makes a mistake in one of the calculations, the bridge falls. But the bridge won’t fall alone. Most probably that engineer will also fall with it. He is accountable for what he did because he must sign his projects.

ABAP programmers don’t have that kind of problems.

Search form methods, attributes, types, events, etc

You remember having written a method some years ago which you need now. You know it had the word MALMEQUER somewhere in its name. But what now? How will you find it? You could use transaction SE24 to look at every class you worked on since then. But that would probably keep you busy for ages. Don’t do that. Try using transaction SE84 instead: