Posted on Apr. 04, 2009 08:37 AM
In the project, it's quite common for us to implement enhancement into SAP standard objects, and one of them is through BADI. There are 2 types of BADI:
1. Classic BADI, which instantiation is done during runtime, and
2. Kernel BADI, which instantiation is done during compilation or activation.
In this blog, I would like to discuss on how to create classic BADI first. In the future blog, I will discuss the creation of Kernel BADI too.
Now, let's create a simple program, which will display the income tax for a certain country, given the country ID and the payment sum as parameters. To cater for different income tax calculation for different countries, we plan to use classic BADI with country ID as the filter.
1. Create BADI Definition
1.1 Go to SE18 and create BADI definition
1.2 Populate the attributes, including the filter type. In this field, you can place data element which has value table in its domain. Don't forget to save it.
1.3 Go to Interface tab, and create the BADI-Interface by double clicking on the interface name. I would recommend not changing SAP proposed interface name.
It jumps to SE24 Class/Interface Editor, where you can add in attributes and methods in the BADI interface.
1.4. Define Method of the interface. In this example, we are going to create one method called 'CALCULATE_TAX', which will have country specific implementation.
1.5. Define Parameters of the method. Don't forget to save and activate it.
2. Create a program to use BADI definition.
Create a program to use BADI definition using transaction SE38.
2.1 Create a reference variable type to the BADI-Interface
DATA: r_int TYPE REF TO zif_ex_badi_income_tax.
2.2 Get the instance of the class which implement BADI-Interface by calling GET_INSTANCE method of the service class CL_EXITHANDLER.
CALL METHOD cl_exithandler=>get_instance
* EXPORTING
* exit_name =
* null_instance_accepted = SEEX_FALSE
* IMPORTING
* act_imp_existing =
CHANGING
instance = r_int
EXCEPTIONS
no_reference = 1
no_interface_reference = 2
no_exit_interface = 3
class_not_implement_interface = 4
single_exit_multiply_active = 5
cast_error = 6
exit_not_existing = 7
data_incons_in_exit_managem = 8
others = 9
.
IF sy-subrc <> 0.
MESSAGE 'Instance Not Found!'(INF) TYPE 'I'.
ENDIF.
2.3 Execute the method of the instance we derived from the previous step
CALL METHOD r_int->calculate_tax
EXPORTING
flt_val = p_ctry
CHANGING
income_tax = p_tax.
2.4 Excerpt is the coding for the application for your reference.
REPORT zbadi_income_tax.
PARAMETER: p_ctry TYPE land1,
p_amt TYPE netwr.
DATA: r_int TYPE REF TO zif_ex_badi_income_tax.
DATA: v_tax TYPE netwr.
START-OF-SELECTION.
CALL METHOD cl_exithandler=>get_instance
* EXPORTING
* exit_name =
* null_instance_accepted = SEEX_FALSE
* IMPORTING
* act_imp_existing =
CHANGING
instance = r_int
EXCEPTIONS
no_reference = 1
no_interface_reference = 2
no_exit_interface = 3
class_not_implement_interface = 4
single_exit_multiply_active = 5
cast_error = 6
exit_not_existing = 7
data_incons_in_exit_managem = 8
OTHERS = 9
.
IF sy-subrc <> 0.
MESSAGE 'Instance Not Found!'(inf) TYPE 'I'.
ENDIF.
CALL METHOD r_int->calculate_tax
EXPORTING
flt_val = p_ctry
amount = p_amt
CHANGING
income_tax = v_tax.
If you try to run the report at this stage, you will not see any result from it, as we have not created the implementation for the BADI yet.
3. Implement the BADI
3.1 Go to SE19, and create implementation for the BADI
3.2 Provide the implementation name for the BADI
3.3 Populate the attributes and save it. Don't forget to populate the filter type, which in this case is the country ID. The implementation class will only be called if the country ID from the application match with the country ID defined over here.
3.4 Go to 'Interface' tab and implement the method. Over here, SAP will automatically create an implementation class for the BADI Interface which defined in the BADI definition. Don't forget to activate the implementation class for the BADI and later on the BADI implementation itself.
4. Test your BADI
4.1 Execute the application which we created in step 2.
4.2 Result from the application.
Now you can see the result from the application as it executes the implementation part of the BADI.
Congratulations, you have created your own BADI, use it in the application and implement it as well.
Ruslim Chang Senior Process Integrator
Life is beautiful! Let's make it meaningful and colorful!