"
Supported by

Implementing private methods in BADIs

A little while ago I had an idea as eccentric as it was amazing that today I have decided to share here, namely, how to create private methods in BADI classes.

Let me explain.

So by means of an introduction, two years ago I wrote an article entitled Enhancing enhancements and I proposed an organised way for implementing enhancements which consists in never placing code directly in the enhancements (whether inuser-exits, mods, BADIs or enhancements) but in advance by encapsulating every separate element that has to be done within one function. Read the article to understand the benefits of this approach. At the time objects were not yet used and for this reason I suggested the creation of a group of functions with a combination of function modules. Meanwhile, 2 years later I don’t think it makes any sense at all to use functions unless when SAP forces us to do this (RFCs for example) Therefore the article is worth updating so as to use a class with static methods.

Well, I was in the process of implementing a BADI according to this same logic of always encapsulating the logic that is added to the enhancements and I started to create a class to which I added a method which I then invoked from the BADI method.

But then, I had a lightbulb moment and I thought… but the BADI is already a class of its own. Why then do I have to create a new class? Indeed.

Then the idea emerged in my head to implement new private methods in the same class that implements the BADI and then to invoke them from the official BADI methods. In this way everything remains orderly without any need to create an extra class.

Let me now explain the process in detail.

Now imagine that you have to implement a BADI method. This is what you do:

  1. In SE19 you create the BADI implementation with the base in the BADI you want. Give it a name, etc, etc;
  2. Instead of entering the method that you want to implement, you first enter into the class that will contain the implementation:

  3. (Now you are in SE24) Create a private method with a name that describes what you intend to do (I created 2):

  4. Define the parameters of this method copying only the parameters that you will need from the BADI method:

  5. Define exceptions if that makes sense;
  6. Implement your logic inside the new private method:

  7. Then go to the public method of the BADI that you want to implement (you can go there directly via SE24 or do this from SE19);
  8. In this method you invoke your new private method (in this case the 2 are invoked immediately):

Then you’re done. Only now do you activate the BADI (in SE19) if it is not already active.

Advantage: Any orderly organisation has to have recourse to external classes.

Disadvantage: Private methods are not visible in SE19 (which only shows the BADI interface), which could cause some confusion for anyone unfamiliar with classes.

I think the advantage outweighs the disadvantage. What about you? What do you think? Do you see more advantages or disadvantages?

I found out that SAP likes this approach and uses it. Check out standard BADI BNK_BADI_ORIG_PAYMT_CHG) which makes use of it in the fallback implementation CL_BNK_BADI_ORIG_PAYMT_CHG. So we have their blessing.

Greetings from Abapinho.

5 comentários a “Implementing private methods in BADIs”

  1. Ricardo Quintas Diz:

    Grande Nuno!
    E sempre bom ler os teus posts, ainda por cima com um português limpinho.

    Para mim, tudo o que ofereça código arrumado será sempre um “bono malum superate”.
    :o)

    E por falar em imagens…. de quem e a imagem deste post ?
    Gostei.

    Abraço

  2. admin Diz:

    Olá Ricardo (desde o aeroporto!), que bom que gostaste. Quanto à imagem, experimenta esta pesquisa: http://tinyurl.com/7foozjv ;)

    Abraço

  3. joao Diz:

    Boas Nuno,

    A ideia é boa mas penso que só faz total sentido caso a badi não permite multiplas implementações pois caso assim seja, porque não ter uma classe diferente para cada uma das nossas necessidades. Esse é o verdadeiro encapsulamento… :)

  4. admin Diz:

    Olá João, mas quando há multiplas implementações há uma classe por cada implementação, por isso não há problema, fica tudo bem encapsulado. Ou entendi mal?

  5. Bruno Diz:

    Oi João!

    Vou ainda acrescentar… para esses casos de que falas, eu até ia mais longe e criava mesmo uma BAdI custom *dentro da BAdI standard* a permitir múltiplas implementações.

    De facto, é o que estou a tentar vender por onde ando, e acho que consegui :)

    Abraço!
    Bruno

Deixe um comentário


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