Code examples · Modern ABAP · Intermediate
BAdI from both sides: define your own BAdI (SE18), call it with GET BADI, and implement it
The problem
A program checks an order quantity.
How we solve it
- 1Defineinterface and BAdI
- 2Call pointGET BADI, CALL BADI
- 3Implementclass in SE19
The result
Without an implementation the quantity passes.
SAP objects in play
1. The requirement
A program checks an order quantity. The business wants a project to add its own extra rule later, without changing the program. Provide an enhancement point where an implementation can set an error flag.
2. How we design it
A BAdI has three parts. The definition sits in an enhancement spot (transaction SE18) and points to an interface. The call point in the program uses GET BADI to get a handle and CALL BADI to run the interface method of all active implementations. The implementation is a class that implements the interface, created later in SE19 or ADT by a project team. If no implementation is active, a multiple-use BAdI call simply does nothing.
Why we decided it this way
- 1Multiple use BAdISeveral implementations can be active at the same time and a call without any active implementation does not fail.
- 2Methods with importing and exporting onlyThe caller sends the data in and reads the result out. The implementer cannot damage the caller's variables.
- 3Clear the result before the callIf nobody implemented the BAdI the exporting parameter stays as it was. Starting from a clean value makes the result predictable.
Before you start
- A package and transport request in a development system.
- Transactions SE18 (enhancement spot and BAdI definition) and SE19 (implementation), or the matching editors in ADT.
3. The code, step by step
Step 1 of 3: Create the interface and the BAdI definition
An interface with one method inside an enhancement spot.
" The BAdI interface
INTERFACE zif_order_check PUBLIC.
INTERFACES if_badi_interface.
METHODS check_quantity
IMPORTING iv_quantity TYPE i
EXPORTING ev_error TYPE abap_bool.
ENDINTERFACE.
Part by part (plain text)
- The BAdI interface. In SE18 create the enhancement spot ZES_ORDER, then a BAdI definition ZBADI_ORDER_CHECK with the interface ZIF_ORDER_CHECK and the setting Multiple Use. The interface has one method with an input and an output.
Open in the portal
The SAP objects named in this code are linked in the code above and listed here. Each opens its page in the SAP Library.
If it does not work
4. The final outcome
Without an implementation the quantity passes. With the example implementation a quantity above 1000 is rejected.
What you should see
Without implementation: quantity 1200 accepted With implementation active: quantity 1200 rejected (BAdI says error)
Try it and check the result
- Call ZCL_ORDER_RULES=>IS_QUANTITY_OK( 1200 ) before the implementation is active: it returns true.
- Activate the implementation in SE19 and call again: it returns false for 1200 and true for 900.
- Deactivate the implementation: the behaviour is back to the first result, with no change to any program.
Common mistakes
- A BAdI that is not multiple use fails with an exception when no implementation exists. Decide this when you define it.
- If two implementations set the same exporting parameter, the last one wins. Keep implementations small and independent.
- When you use an SAP-delivered BAdI, find it through the enhancement spot or by searching the standard code for CL_EXITHANDLER and GET BADI, and read its interface documentation before you write code.
- Do not put heavy database reads inside a BAdI that runs for every document line.