Badi Interview Questions
Badi Interview Questions
Advantages of BADI
The main advantage of using BADI’s is , we can create multiple implementations for a single
BADI definition.
Where as with the exits, we can create a single implementation .i.e a single project for a
enhancement.
We cannot create another project (implementation) for enhancement which is already used.
That is why we go for BADI’s.
Types of BADI
Single implementation BADI:- A BADI which has only one implementation (single class) is called
single implementation BADI.
Multiple implementation BADI:- A BADI which has multiple implementations is called multiple
implementation BADI. By default all the implementations will be executed.
Unrestricted
Approach 1 via Sort Technique
Unrestricted
Implementation 1
Implementation 2
Unrestricted
Implementation 3
There are 3 Implementations for the BADI. Lets try giving a implementation sequence
via Sort.
Unrestricted
If asked provide an Transport Request. That’s all !
Ok now the sorting criteria is available for multiple Implementations. Check as shown
below.
Unrestricted
Implement the sorting order. Enter a sorting value in the implementation, save and
activate.
Unrestricted
Ok lets do a quick test to verify the implementation sorting order.
Unrestricted
After report execution, we see the implementation order sequence
Unrestricted
2 Options :
2) For standard BADI, check if the filter values are enhanceable. If yes, you can add a
new value to the value table of the Filter Type Data Element/Domain by calling the
respective implementation. For more info, press F1 on the field enhanceable.
Unrestricted
BADI ZDEMO_CLASSIC_BADI with filter type ZORDER_SEQUENCE
Unrestricted
Save the BADI.
Unrestricted
Unrestricted
Unrestricted
Time for a quick test.
Unrestricted
Result
So we can use the two approach’s with a careful considerations for the BADI
implementations order sequence. Hope this was helpful.
Unrestricted
Filter BADI It is type of BADI which has a filter value so that only those implementations which
satisfy the filter value are executed. The remaining implementations are not executed this type of
BADI is called a filter BADI.
Properties of BADI
If WITH IN SAP checkbox is selected then this BADI is only used by sap.
If multiple use checkbox is selected then it is a multiple implementation BADI.
If multiple use checkbox is not selected then it is a single implementation BADI.
If filter dependent checkbox is selected then it is filter dependent BADI. We need to specify
the filter type such as land1,bukrs, werks for the filter BADI.
Unrestricted
2. Enter a name and a short text description for the enhancement spot.
Creating a BAdI
1. To create a BAdI within the new enhancement spot, choose the Create BAdI pushbutton on the left,
as shown in the figure below:
2. In the dialog window that appears, enter the BAdI name z_badi_calc_vat and a short description.
You now have a BAdI in your enhancement spot.
3. Deselect the Multiple Use option because for the current calculation you need a single-use BAdI:
Unrestricted
2. Double-click on the Interface icon.
Enter a name for the interface. You can choose an existing name or create a new one.
3. Choose the Change pushbutton.
This leads you to the Class Builder where you can create the methods you need for your BAdI. You
simply need to type in the name of the method get_vat and enter the parameters you need.
Unrestricted
However, just building a BAdI is not enough as it does not do anything. You need a BAdI instance and
this instance must be called somewhere in the code. Since the BAdI only defines an interface, you need
a class that implements this interface.
A BAdI definition is the place to insert an object plug-in that does something at runtime.
You still need an object that is plugged in to get something done.
A Fallback Class
If you run the program at this stage, it dumps. This is because it is mandatory to have exactly one active
implementation for a single-use BAdI. You can handle this error by catching the respective exception
cx_badi_not_implemented.
The better solution is to use a fallback class. This class is used if there is no active BAdI implementation.
The GET BADI command returns a handle to an instance of the fallback class and the respective CALL
BADI calls the methods of the fallback class instance. As soon as there is an active BAdI implementation,
the fallback class is no longer used at runtime.
Using a fallback class serves two functions at once:
● The program runs with a single-use BAdI without throwing an exception.
● It is guaranteed that the fallback class is not used any more as soon as a BAdI implementation is
supplied. The fallback class is selected conditionally. This is important because in most cases the
BAdI provider does not know the details of some process. This is why a BAdI is used.
To add a fallback class, proceed as follows:
1. Select the “Call fallback class if no implementation is executed” option and enter a name for the
class:
Unrestricted
2. Choose Change to go back to the Class Builder.
The respective method of the BAdI interface is already automatically defined. You only have to
implement it:
DATA: percent TYPE p VALUE 20.
ex_amount_vat = im_amount * percent / 100.
ex_percent_vat = percent.
3. Save and activate the class.
4. Navigate back to the enhancement spot and activate it.
5. Run the program.
The result should be:
percentage: 20 VAT: 10.
Classic BADI’s is called by Proxy class cl_exithandler, Where Kernel BADI’s are called directly with the
reference to the BADI definition via GET BADI and CALL BADI statements. That is one of the reasons why
Kernel BADI is 40-600 times faster than classic BADI.
In classic BAdIs, a BAdI object is created by calling a factory method, and referenced via a reference
variable of the type of the BAdI interface.ss
In Kernel BAdIs, a BAdI object is created via the ABAP statement GET BADI as a handle for the calls of
BAdI methods, and referenced via a reference variable of the type of the BAdI.
A classic BAdI can be called only once and the call positions are registered centrally.
In Kernel BAdIs, multiple calls are possible and the call positions are not registered centrally.
Unrestricted
2. in Kernel BADI object is created with the GET BADI statement while In Classic BADI object is created by
calling factory method.
3. In New(Kernel) BADis multiple calls are possible but in classic BADIs multiple calls are not possible.
Approach 2:
Go to Tcode SE84-> Enhancements ->BADIs -> Definitions.
Find the package for the Tcode for which you are finding the BADI.
Enter it as shown and hit execute:
Unrestricted
Business Add-ins - Filter BADI - Application for displaying
customer data based on filter value - Country code
Skip to end of metadata
Attachments:16
Added by Harsh Dave, last edited by Harsh Dave on Jun 08, 2009 (view change)
Unrestricted
show comment
Go to start of metadata
→ What is a Filter-BADI?
Filter dependence for BADI definitions means that each interface method automatically has an importing parameter
FLT_VAL assigned to it. This parameter must be filled with a filter value as current parameter when it is called
We encounter a business requirement where different country clients (customers) may have different country specific
applications. These applications can be integrated centrally in one standard application by means of a 'Filter'.
Here we consider a scenario for three country specific application (say) for India (IN), USA (US) and Germany (DE)
respectively.
→ Note: Here the scenario will be dealt with a BADI created under customer name space for easy understanding.
→ To enable a BADI to allow 'Filter' implementations, make the BADI filter dependent. In case of a standard BADI it should
be inbuilt 'Filter dependent'.
Unrestricted
→ Create interface parameters. Here 'FLT_VAL' indicates the parameter which will act as a filter,
Unrestricted
→ Navigate inside the method to write the code,
Unrestricted
→ Observe the code for filter value 'INDIA',
methodzif_ex_dave_badi_filter~zdave_method_country.
data:
begin of fs_kna1,
end of fs_kna1.
data:
t_kna1 like
standard table
of fs_kna1.
select kunnr
Unrestricted
land1
name1
ort01
pstlz
regio
stras
from kna1
into table
t_kna1
If sy-subrc eq 0.
110'Street'(007) color 7.
skip.
Unrestricted
write:/2 fs_kna1-kunnr color 1inverse,
endloop.
endif.
endmethod.
→ Now, create a filter implementation for country code 'US' that is USA,
Unrestricted
→ Navigate inside the filter method to code for the filter condition,
Unrestricted
→ Observe the code for filter implementation 'USA',
methodzif_ex_dave_badi_filter~zdave_method_country.
data:
begin of fs_kna1,
end of fs_kna1.
data:
t_kna1 like
standard table
of fs_kna1.
Unrestricted
select kunnr
land1
name1
sortl
telf1
telfx
adrnr
from kna1
into table
t_kna1
skip.
Unrestricted
write:/2 fs_kna1-kunnr color 1inverse,
endloop.
endmethod.
Unrestricted
→ Navigate inside the method to write the code,
Unrestricted
→ Observe the code for filter implementation 'DE',
methodzif_ex_dave_badi_filter~zdave_method_country.
data:
begin of fs_kna1,
end of fs_kna1.
data:
select kunnr
Unrestricted
land1
name1
anred
erdat
ktokd
lifnr
from kna1
into table
t_kna1
110'Vendor'(007) color 7.
skip.
Unrestricted
35 fs_kna1-name1 color 3inverse,
endloop.
endmethod.
→ Now, execute the application (here report) and test the customer enhancement,
Unrestricted
→ Execute the test data and observe the functionality,\/]
Unrestricted
→ Execute the test data and observe the functionality,
If <itab> is not having header line, this will clear the entire contents.
CLEAR <itab>.
If <itab> is having header line, this statement will just clear the contents of header line.
CLEAR <itab>[]
There are two types of Explicit Enhancement options available. For this, we now have two new ABAP
statements, viz.
Unrestricted
Enhancement point: It allow you to add your own source code without modifying original source code in
some predefined places.
Enhancement section: It is used to replace a set of code or statements with the customer (custom
code). In this technique the original source code does not get executed but, the customer implementation
(custom code) gets executed.
Enhancement Framework is the new paradigm where all enhancement techniques are under one roof. It
can also be switched using Switch Framework. The following are different enhancement technologies
available under this framework.
• Class Enhancement
• Kernel-BADI Enhancement
Unrestricted
Source Code enhancement
Whenever enhancement needs to be incorporated directly into the ABAP source code, this technology
shall be provided. Implementing this technology is also called as Source Code Plug-In. There are two
types of Source Code enhancements possible.
Implicit enhancements are predefined enhancements provided by SAP in the source code.
There are two types of Explicit Enhancement options available. For this, we now have two new ABAP
statements, viz.
Enhancement section is used to replace a set of code or statements with the customer (custom code). In
this technique the original source code does not get executed but, the customer implementation (custom
code) gets executed.
Note - Explicit enhancements though can be placed anywhere in the source code but, not just anywhere
except some areas where SAP would allow (program allows). They are called explicit because their
position in the standard code is defined explicitly. On the other hand, we have implicit enhancements
whose position is implicit i.e. at the beginning and end of programs.
Unrestricted
STEP 2: Navigate to 'Enhancements' folder of your package. Package (YDEV) Enhancement.
And save it into created package. Observe the enhancement spot created under the 'Enhancement
Spots' folder.
Unrestricted
STEP 3: 'Right Click' the spot created and 'Implement' it (Create an Implementation).
Unrestricted
Fill in all the details in the 'Create Enhancement Implementation' dialog.
STEP 4: Now, we need to 'Activate' the enhancement spot. In addition with the Enhancement spot the
'Enhancement Implementation' will get activated.
Unrestricted
STEP 5: Here we are applying enhancements to a CUSTOM program not a standard program to
demonstrate the functionality. So we create a simple program 'YDEV_CODE' (say) it is retrieving records
from the database table 'VBAK' (Sales Document Header) and displaying a few records.
Now, if the customer wants to replace the set of logic with his own logic (say) like retrieving records from
database table 'VBAP' (Sales Document Item) and then display a few records, he/she will create an
enhancement section which goes like,
Unrestricted
OUTPUT
Unrestricted
STEP 6: Right click the area which is appropriate to apply the enhancement
Note - Explicit enhancements though can be placed anywhere in the source code but, not just anywhere
except some areas where SAP would allow (program allows).
Unrestricted
Now, in the 'Create Enhancement Option' fill in the details, here fill the name under 'Enhancement-
section' only. Then fill in the Enhancement Spot Implementation Name which we created earlier.
Unrestricted
Now we are able to see program lines have Enhancement-Section…End Enhancement-Section.
Note - Make sure that the code which has to be replaced is within the 'ENHANCEMENT-SECTION...' and
'END-ENHANCEMENT-SECTION'.
Unrestricted
TEP 7: Now to include the custom code in the program which will replace the original code, enable the
'Enhancement Mode' by clicking on the 'Spiral' button.
Fill in the details for the 'Create Enhancement Implementation' dialog. Click on 'Create' button for the
'Select or Create Enhancement Implementation' dialog.
Unrestricted
STEP 8: Now, write the code within the 'ENHANCEMENT' and 'ENDENHANCEMENT' statements as the
replacement code.
Unrestricted
STEP 9: Don't forget to 'Activate' the enhancement Switch the 'Enhancement' mode OFF and
'Activate' the entire program.
Unrestricted
STEP 10: Execute the transaction/program to find out the difference.
Before Enhancement:
After Enhancement:
Unrestricted
Summary:
2. There can be only one and only one 'ACTIVE' implementation for an 'ENHANCEMENT-
SECTION'.
Source Code:
*&---------------------------------------------------------------------*
*& Report YDEV_CODE
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT YDEV_CODE.
TABLES : VBAK, VBAP.
DATA : IT_VBAK TYPE STANDARD TABLE OF VBAK INITIAL SIZE 0,
WA_VBAK TYPE VBAK,
IT_VBAP TYPE STANDARD TABLE OF VBAP INITIAL SIZE 0,
WA_VBAP TYPE VBAP.
INITIALIZATION.
REFRESH : IT_VBAK,
Unrestricted
IT_VBAP.
CLEAR : WA_VBAK,
WA_VBAP.
START-OF-SELECTION.
ENHANCEMENT-SECTION YDEV_ENHANCE_SECTION SPOTS YDEV_IMPLEMENT_SPOT .
SELECT *
FROM VBAP
INTO TABLE IT_VBAP[]
UP TO 15 ROWS.
WRITE: /02 'Sales Document',
20 'Date',
40 'Time',
65 'Name of Person'.
ULINE .
IF IT_VBAP[] IS NOT INITIAL.
LOOP AT IT_VBAP INTO WA_VBAP.
WRITE: /02 WA_VBAP-VBELN,
20 WA_VBAP-POSNR,
40 WA_VBAP-MATNR,
65 WA_VBAP-MATWA.
ENDLOOP.
ENDIF.
END-ENHANCEMENT-SECTION.
*$*$-Start: YDEV_ENHANCE_SECTION----------------------------------------------------------------$*$*
ENHANCEMENT 1 YDEV_IMPLEMENT_ENHC_SECTION. "active version
SELECT *
FROM VBAP
INTO TABLE IT_VBAP[]
UP TO 10 ROWS.
WRITE: /02 'Sales Document',
20 'Sales Item',
40 'Material Number',
65 'Material entered'.
ULINE .
IF IT_VBAP[] IS NOT INITIAL.
LOOP AT IT_VBAP INTO WA_VBAP.
WRITE: /02 WA_VBAP-VBELN,
20 WA_VBAP-POSNR,
40 WA_VBAP-MATNR,
65 WA_VBAP-MATWA.
ENDLOOP.
ENDIF.
ENDENHANCEMENT.
*$*$-End: YDEV_ENHANCE_SECTION----------------------------------------------------------------$*$*
Unrestricted
Difference between implicit enhancement option and explicit
enhancement option ?
• At the end of all the programs (Includes, Reports, Function pool, Module pool, etc.), after the last
statement
We can not have Implicit enhancement option inside Module module in module pool program.
Please see below screen shot.
To view all the implicit options available in a source code, choose ‘Edit -> Enhancement Operations ->
Show Implicit Enhancement Options’ from the editor.
Step2: In order to implement any of these Source code enhancements, you need to be in ‘change
Enhancement mode’ (the spiral icon available in the editor).
Unrestricted
Step3: Go to EDIT option in the menu and choose Enhancement Operations -> Show
implicit Enhancement Options
All Implicit Enhancement option will be displayed. All yellow lines indicate Implicit
Enhancement options. In ABAP programs, implicit enhancement options are predefined
at the following places:
•Begin/End of an include.
•End of a structure
Unrestricted
•End of Private/Protected/Public Section of a local class
Unrestricted
Step6: Here Editor will be enabled to write the code. Write your own code save and
activate it.
Step 7:Once the development is completed you can test the scenario.
When Sales person (lzn4rm) tries to Open Sales order no 174, it gives error message ’Access Denied’.
Unrestricted
Explicit enhancement option: The explicit enhancements can be implemented by the customers or
partners.
There are two types of Explicit Enhancement options available. For this, we now have two new ABAP
statements, viz.
Enhancement point: It allow you to add your own source code without modifying original source code in
some predefined places.
Enhancement section: It is used to replace a set of code or statements with the customer (custom
code). In this technique the original source code does not get executed but, the customer implementation
(custom code) gets executed.
Unrestricted
Internal Session and External Session
External session is nothing but the window you have opened in your screen .By Default you can open 6
external sessions ( 6 windows = you can increase it via basis’s setting). Internal session is created
when you call any Functional module or any other task in your program. counts for internal sessions
are 9.
Unrestricted