Code examples · Modern ABAP · Intermediate
ABAP BAdI: define one, call it from your code and implement it
The problem
An order check program must allow customers to add their own rule on top of the standard check, for example refusing orders above a limit, without anyone changing the original program.
How we solve it
- 1Defineinterface + SE18
- 2Call itGET BADI, CALL BADI
- 3ImplementSE19 + class
The result
The check program calls the BAdI.
SAP objects in play
1. The requirement
An order check program must allow customers to add their own rule on top of the standard check, for example refusing orders above a limit, without anyone changing the original program.
2. How we design it
A BAdI (Business Add-In) is a planned place for extra code. We define the contract in an interface, publish it as a BAdI inside an enhancement spot, and call it from the program. An implementation is a separate class that follows the interface and is activated on its own. The original program never changes when a new implementation is added.
Why we decided it this way
- 1Interface as the contractImplementations know exactly which inputs they get and which results they must return.
- 2Enhancement spot as the containerThe spot groups BAdIs and lets the system switch implementations on and off.
- 3A message as the result, not an exceptionThe caller decides what to do with it. The BAdI only gives an opinion.
Before you start
- An on-premise system, transactions SE18 and SE19 (or ADT), and a package such as $TMP.
3. The code, step by step
Step 1 of 3: Define the contract and the BAdI
An interface and a BAdI that uses it.
" The interface
INTERFACE zif_ex_order_check PUBLIC.
INTERFACES if_badi_interface.
METHODS check_order
IMPORTING iv_amount TYPE p
EXPORTING ev_accepted TYPE abap_bool
ev_message TYPE string.
ENDINTERFACE.
Enhancement spot : ZES_ORDER
BAdI definition : ZBADI_ORDER_CHECK
Interface : ZIF_EX_ORDER_CHECK
Multiple use : off
Part by part (plain text)
- The interface. A BAdI interface must include IF_BADI_INTERFACE. The method receives the amount and returns whether the order is accepted, plus a text.
- Enhancement spot and BAdI definition (SE18). In SE18 create the enhancement spot ZES_ORDER. Inside it create a BAdI definition named ZBADI_ORDER_CHECK and enter ZIF_EX_ORDER_CHECK as its interface. Leave 'Multiple Use' off for one active implementation. Save, activate, assign a package.
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
The check program calls the BAdI. With no implementation, orders pass. With the implementation active, an order above the limit is refused with a text.
What you should see
No implementation active: 'Order of 600.00 accepted'. Implementation active with limit 500: 'Order refused: amount 600.00 is above the limit 500.00'.
Try it and check the result
- Run ZDEMO_BADI_CALLER before you activate the implementation: 'Order of 600.00 accepted'.
- Activate the implementation in SE19 and run again with 600: the refusal text appears.
- Run with 400: 'Order of 400.00 accepted'. Deactivate the implementation and the first result returns, without any change to the report.
Common mistakes
- An implementation that is created but not activated does nothing, and gives no error.
- With 'Multiple Use' off only one implementation can be active. A second one needs a filter or multiple use.
- A single-use BAdI without an active implementation raises CX_BADI_NOT_IMPLEMENTED at GET BADI. Catch it or define a fallback class, and set safe defaults first.
- A BAdI should not change the caller's data in surprising ways. Keep the contract small and clear.