During the course of the following document; the reader can understand how the BAdI concept fits in to the new Enhancement Framework. We will create a BAdI definition in the first part and in the later part we will implement the BAdI. Basic knowledge about the new enhancement framework will help in understanding this document better.
Who creates the BAdI definition
Please understand that most of us (Technical SAP – ABAP) will seldom have the requirement to create a BAdI definition. Unless we are working on a ‘product development’ in collaboration with SAP itself; we will be only dealing with the implementation part. Though I mention above that we will seldom be defining a BAdI; it is a very important to understand the BAdI declaration; to appreciate the concept of Enhancement Framework as a whole.
— BAdI definition is part of product development; majorly.
Ok let us assume we are working on a SAP product which has a requirement to perform currency conversion (wrt USD) based on the country provided by the user.
Let us write a program for it.
The program simply has a parameter country. To output the converted value; I would require the conversion rate. And I leave it to the customer to enhance the application and provide the conversion rate.
This is where I use a BAdI.
First step -> creating the Enhancement Spot!!
People familiar with source code enhancements can appreciate the fact that the first step for an enhancement implementation is an Enhancement Spot. Same is the case here; we need to create an enhancement spot which will act as a container for the BAdI definition
So for the time being let us forget the ABAP report and move to SE80; to create the container (Enhancement Spot).
Select Local Objects -> Enhancements -> Enhancement Spots -> Create (right click).
— Enhancement spot acts as a container for a BAdI definition!!
Note – If you questioned the usage of local objects in the previous step; please be aware that; strangely; the section ‘Enhancement Info Systems’ in SE80 does not let you create a new enhancement spot. The ‘Local’ issue is however taken care of; when upon saving; we are prompted to enter the package.
Create a new BAdI definition as shown below.
Currently; in our requirement we are performing only one operation on the amount based on the conversion rate. However; say in China; we want to perform another operation ‘Enhance Currency’; we would be required to use the option ‘Multiple Use’. Please be aware that the order of occurrence of the various implementations of a multiple use BAdI cannot be ascertained!!
— Multiple Use -> Series of operations (BAdI implementations); not following specific order of occurrence though!!
For the current requirement however we will be content without the multiple use option.
The next step is to define an interface. Just pull the arrow down in the left side section and mention the interface name and press the ‘change interface’ button.
— ‘Interface’ differentiates a BAdI from explicit enhancement option!!
The screen above is what differentiates an explicit enhancement option from a BAdI. BAdI is like a controlled explicit enhancement point. Controlled; why? Because; the developer decides the parameters that the implementer will have access to.
Whereas; in explicit enhancement option the implementer can play around with all the variables of the source code; often inviting unplanned behavior of the system.
In other words BAdI is definitely less dangerous than explicit enhancement option!!
The above might lead you in to thinking that the developer should always follow the best path and create an option for a BAdI; why give the option of explicit enhancement points/sections. BUT; there are numerous cases wherein the developer does not know which variable the user might want to use and in what way (yes, it happens!!) This is where explicit enhancement options are inevitable.
Also, of course there are cases wherein there are no parameters required and no operations to be performed on them; but simply a piece of code needs to be added to the source code -> Explicit Enhancement Option is the answer; no use of BAdI here !!
Create a method ‘Conversion’ as shown below.
Filter -> Remember the ‘country-specific’ functionality!!
The conversion factor would vary with the country the user enters; therefore we require different implementations to be called based on the ‘filter’ country. The option of providing the scope of a filter lies with the developer defining the BAdI. (Then only it will be available to the implementer!!) The following screenshot shows how to do the same; go to BAdI Definition -> Right Click -> Create filter
Enter the Filter Name and the Filter Type. In this case; the filter type would be Character.
— Filter facilitates usage of multiple implementations in a single usage BAdI (Only one is executed though!!)
This is all that is required to provide the option of a filter!!
Summary till now!!
We have created an Enhancement Spot which holds the BAdI definition. Our BAdI definition defines the parameter that the implementers can pass/retrieve/change to the method in the BAdI. Also, we have left an option of a Character-Type filter.
Back to the report…
We still do not have the link to the BAdI in the source code of the report. The following coding does just that!!
If you thought the job was over for the developer; please look at the following dump. The system raises an exception ‘CX_BADI_NOT_IMPLEMENTED’ if it does not find any implementations for the BAdI. In our case; we are still developing the application; hence the obvious dump.
— Fallback Class is useful in case no active implementation exists!!
The solution for the Dump -> Fallback Class
Fallback Class is the savior when there is no active implementation for the BAdI definition. It the current scenario; it can be imagined as the case wherein the user wants to convert USD to USD itself!! To create the fallback class; just got to the BAdI definition screen and click check-box ‘Call fallback if no implementation is executed’. Enter the class name and press enter.
In the method of the fallback class we will pass the amount as the answer itself.
That is it!!
This completes the BAdI Definition part of the document!!
And now the implementation…
Well the product development is over; now I am the customer of SAP; and want to use the hook provided above to fulfill my specific requirement. So; let us implement the BAdI now!!
— Implementation is post product development; of course!!
Enter the Enhancement Spot via SE80; of course in Display mode (SAP created it!!). Right click on the BAdI definition and click Create BAdI definition.
You will be prompted to enter the implementing class for the BAdI implementation created in the previous step. Here; you can use the example class to make life easier (The fallback class we created is an example of ‘Example Class’).
Enter the desired code in the method of the implementing class; for our requirement; I am using conversion factor as 49 (Rupee depreciation / appreciation a separate issue!!)
Filter value: We are still to add the filter value to this implementation. Go to the implementation and add the filter.
Similarly, create n implementations for n countries; giving the filter value as desired. Here; I created one for UK.
Let us test the report…
Case 1 Data: – Country IN; Amount 100
Case 2 Data:- Country UK, Amount 100
A freebie in The End…
Though not related; I will demonstrate creating an explicit enhancement point in the source code as a freebie for this document; yipee!!
Simply go to the report Right Click -> Enhancements -> Create
Enter the Enhancement Point and Enhancement Spot.
That is all. Yes; easier than BAdI definition but definitely more dangerous!!
Hopefully the document through the simple example clears the BAI concept; both from definition and implementation point of view.