Recalling from the
previous post (How to Implement a
BAdI), you need a way to select between different BAdI implementations.
This is where you need the BAdI filter.
What you need is to
change the BAdI definition. You can define one or many filters for a BAdI. For
the purpose of this example, you create only one filter. Therefore, in this
step you define the filter in the BAdI definition, determine filter values for
the respective BAdI implementation, and use the filter in the instantiation of
the BAdI handle.
When modifying
your example you do not take into account the differentiation between the BAdI
provider and the implementer. In real life it is the BAdI provider who defines
a BAdI with a filter or adds a filter to an existing BAdI. It is also part of
this role to use the filter condition to select the respective BAdI
implementation in the ABAP code. It is the implementer who determines the
filter value(s) or an interval for one or many BAdI
implementations.
...
1.
Navigate to your
enhancement spot and open the Enh. Spot Element
Definition tab that shows a list of all the enhancement elements of the
spot.
2.
Switch to a change
mode, select your BAdI in the list, and in the small toolbar above choose
Create BAdI Subobject icon and then Filter.
3.
The following
dialog appears, where you fill in the corresponding fields:
4.
Confirm the
entries.
The filter is
now visible as a property below the BAdI.
5.
Activate the
enhancement spot, double-click on the implementation and navigate to the
respective BAdI implementation by double-clicking the respective row in the
table of BAdI implementations.
6.
Switch to change
mode, double-click the triangle in front of the BAdI implementation in the
tree and then double-click on the filter icon below.
7.
On the Filter Values screen, choose the Create
Filter Combination pushbutton.
8.
Select Country as the filter and confirm
9.
Double-click on the
row below Combination 1.
A new dialog
opens:
10.
Activate the
(simple) enhancement implementation and navigate back to the spot.
The other
implementation, that is the one for USA,
also needs the respective filter value. So you go to the respective (simple)
enhancement implementation and change the BAdI implementation in the same way
as you just did above. The respective filter value for this country is
'US'.
11.
Return to your
program and adapt it to the modified BAdI. Running the syntax check shows you
that you need to have a filter parameter in the GET
BADI command if the BAdI
you try to instantiate is defined with a filter. The required addition to the
GET
BADI command is
FILTERS. It is after this keyword that you insert the name
of the BAdI filter and a value the filter is compared to. You also have to
take care that an appropriate value can be passed to the GET
BADI routine:
REPORT
Z_DEMO_ENH.
parameters: ctry(2) type c.
DATA: handle TYPE REF TO
z_badi_calc_vat,
sum TYPE p,
vat TYPE p,
percent TYPE p.
sum = 50.
GET BADI handle FILTERS Country =
ctry.
CALL BADI handle->get_vat
EXPORTING im_amount = sum
IMPORTING ex_amount_vat = vat
ex_percent_vat = percent.
WRITE: 'percentage:', percent, 'VAT:'
,vat.
If you pass
GB to the parameter ctry, you get a VAT rate of 17.5 percent. If the
value of the parameter ctry is US, the VAT rate is 4 percent. When the parameter
ctry has any other value, you still get a calculated
value for the data field percent. This is because
you have defined a fallback class for your BAdI. The fallback class is not
only selected if no BAdI implementation is available, but also if none of the
existing BAdI implementations meets the filter conditions in the GET
BADI command. Accordingly,
you get a VAT rate of 20 percent, which is the VAT rate you have implemented
in the method get_vat in the fallback class.