Supported by
Supported by Inetum

Implementing private methods in BADIs

images/thumbnail.jpg - Thumbnail

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 in user-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:

image

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

image

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

image

  1. Define exceptions if that makes sense;

  2. Implement your logic inside the new private method:

image

  1. 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);

  2. In this method you invoke your new private method (in this case the 2 are invoked immediately):

image

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.