Recalling from the previous step ( 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.
The filter is now visible as a property below the BAdI.
A new dialog opens:
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'.
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.