"
Supported by

Stop the functions module’s error messages

silencio

There are standard functions that trigger messages where they shouldn’t. And they shouldn’t because we want to use these functions in non-interactive programs and then, instead of returning the error, an error message is triggered and ruins the whole process.
 
However, being aware of this problem, SAP has provided a quite elegant (but not well documented) way of solving this problem.
 
Ler o resto do artigo! »

Text search in a WebDynpro

seti

SAP doesn’t know how to do things right the first time. The WebDynpros are a good example of this. It doesn’t even let you do a text search. It’s sad.

Fortunately Sérgio Fraga has found a way, though it’s rather laboured:

Ler o resto do artigo! »

It’s my birthday!

6velas

I’m 6 years old!

(Thank you Caleb Prichard for the photo)

Best practices
Thou shalt always use a predefined structure with ALV

ashtanga13

It is common to find an ALV data structure explicitly defined in the code. If this is done, the field catalog has to be manually constructed. If a predefined structure (from DDIC or declared as a TYPE) is used instead, the field catalog can be automatically built. This approach is always better and results in less code, even if the field catalog needs to be adjusted here and there.

http://abapinho.com/en/2011/12/automatizar-catalogo-alv/

Best practices
Thou shalt use TRANSPORTING NO FIELDS

ashtanga06

Many times we do READ TABLE itbl or LOOP AT itbl just to do a CHECK SY-SUBRC = 0. In these cases, the actual data read is not needed. For these cases always use TRANSPORTING NO FIELDS. This way is faster and avoids having to declare a target structure.

Debugging an infinite loop already in execution

cobra-infinita

Imagine you have a program executing an infinite cycle or, at least, a cycle with 70×7 iterations. It is neverending, and you want to know what’s going on there.

In the past you had to go to SM50, select the process and choose from the menu “Administration | Program | Debug”.

But now there is a much easier way.

Ler o resto do artigo! »

How many includes is a class made of?

sapateira

No matter how many times things go around in ABAP, everything ends up in SE38. Even the methods of the ABAP classes are saved in includes.

Sometimes, when there is a dump, it says the problem is, for example, here: CL_MESSAGE_HELPER=============CM001.
Ler o resto do artigo! »

Unreleasing a released transport order

mulher-policia

You’ve released a transport order because you thought everything was ready. However, one more minor modification was still missing. So now you will have to create a new order and transport both of them. What a drag.

Don’t worry.
Ler o resto do artigo! »

Presenting the EGSAP_TECH app

EGSAP_TECH

Do you know about the EGSAP_TECH app? It’s a SAP knowledge bank.

Here is a description in the words of its own creators:

Ler o resto do artigo! »

SELECT-OPTIONS default behavior

interruptor

Abapinho received a letter.

Mr. Abapinho,

Everybody knows how to set default values in select options using the DEFAULT keyword. What some people may not know is that one can also set the default option, sign and even if allows for intervals or just fixed values.
Ler o resto do artigo! »

Chained exceptions

corrente

Today I will teach you how to chain exceptions. It’s a very practical solution to a complicated, not so obvious problem.

Let’s start by describing the problem.

Imagine you are in the application BANANA.
The application is quite complex.
It has three modules: BANANA1, BANANA2 and BANANA3.
Each one has its exception class ZCX_BANANA1, ZCX_BANANA2 and ZCX_BANANA3.
Since the application is in fact well designed, all the exception classes inherit from the same ZCX_BANANA.
Now imagine the following scenario.
You are in the BANANA1 module doing something.
There, you must call a class from the MORANGO module.
Of course, this class launches exceptions of the type ZCX_MORANGO.
This is the context.

You have several options:

Option 1: Declare the external exceptions
The method which calls the MORANGO class declares the exception ZCX_MORANGO in RAISING.
All of the methods which call it must declare it as well.
Henceforth, to the top of the call hierarchy.
It’s a big mess.
Imagine that BANANA must also use classes from the modules ABACATE, LARANJA and UVA.
It will also have to declare the respective exception classes in every method which uses them.
The more complex it is, the more confusing it all becomes.
This is not what we want.
Generally, anyone who does this ends up having to do a CATCH CX_ROOT, which is somewhat unhealthy.
For all of these reasons, option 1 should be avoided.
Except in very simple scenarios.

Option 2: Convert external into internal exceptions
Each BANANA method which invokes MORANGO methods always does TRY CATCH for ZCX_MORANGO and immediately launches an exception of the type ZCX_BANANA.
This even works.
The problem is that, for each exception of MORANGO, there must be an equivalent exception of BANANA.
If it’s just one, everything’s okay.
But in the case of dozens, it becomes silly.
And it’s not very secure.
Each of the exceptions created will have to replicate the specific text of the respective MORANGO exception.
This is not very practical.
This is because if tomorrow someone changes something in MORANGO, then BANANA becomes outdated.
It also tends to generate a large amount of confusion.
For all of these reasons, option 2 should be avoided.
Except in very simple scenarios.

Option 3: Use PREVIOUS to create chained exception
All exception classes have an attribute called PREVIOUS.
This is a reference for each exception class.
When the BANANA method invokes methods of MORANGO it always does TRY CATCH to ZCX_MORANGO.
But it does not launch a specific exception for each exception of MORANGO.
Instead, it always launches the same ZCX_BANANA exception.
But I have assigned the MORANGO exception to the PREVIOUS of the BANANA exception.
Whoever handles all the exceptions at a higher level only has to see whether the PREVIOUS has any content.
If it does, then it presents/saves/processes the exception there as well.
Ideally this should be done in LOOP.
In the event the exception of PREVIOUS has, in turn, another exception in its own PREVIOUS.
This is the best of both worlds:
Future proof code, which does not lose the information of specific exceptions.
For me, this is the best option.

I hope the both problem and the solution I propose are clear.

I leave you now with a simplified implementation of option 3:


TRY.
  TRY.
      o_morango1->cresce().
    CATCH cx_morango INTO o_exp.
      RAISE EXCEPTION TYPE cx_banana
        EXPORTING
          previous = o_exp.
  ENDTRY.
CATCH cx_banana INTO o_exp.
  WHILE o_exp IS BOUND.
    log( o_exp ).
    o_exp = o_exp->previous.
  ENDWHILE.
ENDTRY.

Thank you Clark for the photo.

Greetings from Abapinho.
Ler o resto do artigo! »

Ignore function module exceptions

zero

When calling a function module which returns exceptions you normally give them sequential numbers like this:


CALL FUNCTION 'GO_BUT_PLEASE_COME_BACK'
  EXPORTING
    ali = 'To the moon'
  EXCEPTIONS
    NOT_FOUND = 1
    GOT_LOST  = 2
    OTHERS    = 3.

But Code Inspector may be configured to report a warning if afterwards you are not careful to add an IF or a CASE to look at SY-SUBRC,
Ler o resto do artigo! »

Best practices
Thou shalt use use exception classes

ashtanga07

In classes, consider using exceptions classes over the old ones. These have great advantages and, once understood, are simple and allow for simpler code.

http://www.slideshare.net/pruebaedublog/exceptions-handling-concept-in-abap

http://www.scribd.com/doc/86551830/26/Lesson-Exception-Handling-in-ABAP-Objects

http://zevolving.com/2011/12/class-based-exception/

Best practices
Thou shalt use readily translatable explicit literals in programs

ashtanga08

In reports, instead of WRITE TEXT-001, use WRITE ‘bla bla bla’(001). This way a default text will always be there and besides the readability of the program is improved.

http://abapinho.com/en/2011/11/programas-poliglotas/

Best practices
Thou shalt use SALV instead of the old ALV functions

ashtanga14

SALV classes are more versatile and more recent than the old function modules. So, for new ALVs always use SALV. The only exception is editable ALVs which SALV classes are still very incapable of doing.

http://scn.sap.com/docs/DOC-10365

http://scn.sap.com/docs/DOC-10366

Let’s concatenate

comboio

We start with two variables:


DATA word1 TYPE string.
DATA word2 TYPE string.
DATA: phrase TYPE string.

word1 = ‘this’.
word2 = ‘that’.

And we want to concatenate them adding the word “plus” between them and, of course, separate them by space.

Ler o resto do artigo! »

Communication by event between programmes

europa-titan

In Greek mythology, the gods’ most commonly used means of communication with mortals was rape. They would rape for no reason whatsoever.

The closest thing we have to rape in ABAP is the command “SUBMIT”, which is also the most common way of communicating between two programmes.

Ler o resto do artigo! »

Write in multiple lines at the same time

bart-quadro

The ABAP editor has many curious functionalities.
You can even write in multiple lines at the same time.

Ler o resto do artigo! »

Where is the boolean?

checkbox

It’s not.

But they – the people who make and remake the ABAP itself – are trying to mend this unfortunate situation.

Look at this new functionality:

Ler o resto do artigo! »

LOOP AT tbl ASSIGNING <line> CASTING

casting

Did you know that you can do a LOOP on an internal table of one type into a structure of a different type?

Ler o resto do artigo! »


About Abapinho
Abapinho runs on WordPress
Articles (RSS) e Comments (RSS).