A developer needs to calculate the VAT of different ledger entries. The entries have to be passed to a method that calculates and returns the VAT. Since the developer does not know what the VAT in a specific country is, he or she defines a BAdI with the method get_vat . This method must return the VAT for a particular value that is passed to the method get_vat .
The first thing you need when creating a BAdI is a container for the BAdI. For that purpose, you need to create a (simple) enhancement spot. This is the container in which you develop your BAdI.
The following dialog appears:
You now have a BAdI in your enhancement spot.
Up to now, you still do not have a BAdI. You need an interface where you can define the methods which determine what you can do with your BAdI.
Enter a name for the interface. You can choose an existing name or create a new one.
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.
A BAdI interface has to implement the interface if_badi_interface . But if you create a BAdI interface in the way shown above, this marker interface is already integrated in the BAdI interface.
By completing the steps above, you have created an enhancement spot and a BAdI with an interface. The interface has one method so far.
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.
Now you need to write some ABAP code to use the BAdI. You need a variable that can refer to the BAdI and some variables that are given as actual parameters to the BAdI method.
Next, you need to create a handle for that BAdI and call the BAdI method get_vat . The respective commands are GET BADI and CALL BADI .
DATA: handle TYPE REF TO z_badi_calc_vat,
sum TYPE p,
vat TYPE p,
percent TYPE p.
sum = 50.
GET BADI handle.
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 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:
To add a fallback class, proceed as follows:
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.
The result should be:
percentage: 20 VAT: 10.
Next step is: How to Implement a BAdI and Use a Filter