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.
Creating an Enhancement Spot
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.
...
1.
In the Object
Navigator (transaction SE80), navigate to the package in which you want to
create the enhancement spot.
2.
In the context menu
of the package, choose Create ® Enhancement ®
Enhancement Spot.
The following
dialog appears:
3.
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:
The BAdI Interface
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.
...
1.
Choose the arrow in
front of the BAdI name.
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.
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.
4.
Determine the
parameters of the method:
5.
Save and activate
the interface and the spot.
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.
Writing the Source Code
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.
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:
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.
No comments:
Post a Comment