ABAP BAdIs & Enhancements: GET BADI, CALL BADI

Category
ABAP-Statements
Published
Author
Johannes

BAdIs (Business Add-Ins) are the modern extension mechanism in SAP. They enable modification of SAP standard behavior without changing the original code. With Enhancement Spots, you can define your own extension points.

BAdI Types

TypeDescription
Classic BAdISE18/SE19, deprecated
New BAdIEnhancement Spot, GET/CALL BADI
Filter BAdIContext-dependent implementation
Fallback BAdIDefault implementation

New BAdI Syntax

" Get BAdI handle
GET BADI lo_badi.
" Call BAdI
CALL BADI lo_badi->method_name
EXPORTING param = value
IMPORTING result = lv_result.

Examples

1. Implement BAdI (New BAdI)

" 1. Find Enhancement Spot (SE18)
" Example: BADI_SD_SALES
" 2. Create Implementation (SE19)
" Enhancement Implementation: ZEI_SD_SALES
" BAdI Implementation: ZBI_SD_SALES
" 3. Implementation class
CLASS zcl_sd_sales_badi DEFINITION
PUBLIC FINAL
CREATE PUBLIC.
PUBLIC SECTION.
INTERFACES: if_badi_sd_sales.
ENDCLASS.
CLASS zcl_sd_sales_badi IMPLEMENTATION.
METHOD if_badi_sd_sales~check_document.
" Customer-specific check
IF is_header-auart = 'ZOR'.
" Special handling for order type ZOR
IF is_header-vkorg NOT IN gt_allowed_orgs.
ev_error = abap_true.
ev_message = 'Order type ZOR not allowed for this sales org'.
ENDIF.
ENDIF.
ENDMETHOD.
METHOD if_badi_sd_sales~modify_data.
" Modify data before saving
IF cs_header-bstkd IS INITIAL.
cs_header-bstkd = |AUTO-{ sy-datum }|.
ENDIF.
ENDMETHOD.
ENDCLASS.

2. Call BAdI in Your Own Code

" Find BAdI definition
DATA: lo_badi TYPE REF TO badi_sd_sales.
" Get handle
GET BADI lo_badi.
" Call method
CALL BADI lo_badi->check_document
EXPORTING
is_header = ls_header
IMPORTING
ev_error = lv_error
ev_message = lv_message.
IF lv_error = abap_true.
MESSAGE lv_message TYPE 'E'.
ENDIF.

3. Create Your Own BAdI

" 1. Create Enhancement Spot (SE18)
" Name: ZES_ORDER_PROCESSING
" Short description: Order Processing Extensions
" 2. Create BAdI Definition
" Name: ZBADI_ORDER_CHECK
" Interface: ZIF_ORDER_CHECK
" 3. Define interface
INTERFACE zif_order_check PUBLIC.
METHODS: validate_order
IMPORTING
is_order TYPE ty_order
EXPORTING
ev_valid TYPE abap_bool
et_messages TYPE bapiret2_t.
METHODS: enrich_order
CHANGING
cs_order TYPE ty_order.
ENDINTERFACE.
" 4. Use in your own code
CLASS zcl_order_processor DEFINITION.
PUBLIC SECTION.
METHODS: process_order
IMPORTING is_order TYPE ty_order
RAISING zcx_order_error.
ENDCLASS.
CLASS zcl_order_processor IMPLEMENTATION.
METHOD process_order.
DATA: lo_badi TYPE REF TO zbadi_order_check,
lv_valid TYPE abap_bool,
lt_messages TYPE bapiret2_t.
" Get and call BAdI
GET BADI lo_badi.
CALL BADI lo_badi->validate_order
EXPORTING is_order = is_order
IMPORTING ev_valid = lv_valid
et_messages = lt_messages.
IF lv_valid = abap_false.
RAISE EXCEPTION TYPE zcx_order_error
EXPORTING messages = lt_messages.
ENDIF.
" Enrich data
DATA(ls_order) = is_order.
CALL BADI lo_badi->enrich_order
CHANGING cs_order = ls_order.
" Further processing...
ENDMETHOD.
ENDCLASS.

4. BAdI with Filter

" BAdI definition with filter (SE18):
" Filter: VKORG (Sales Organization)
" Implementation for Sales Org 1000
CLASS zcl_badi_vkorg_1000 DEFINITION.
PUBLIC SECTION.
INTERFACES: zif_sales_badi.
ENDCLASS.
CLASS zcl_badi_vkorg_1000 IMPLEMENTATION.
METHOD zif_sales_badi~process.
" Logic only for Sales Org 1000
cs_data-discount = '10.00'.
ENDMETHOD.
ENDCLASS.
" Implementation for Sales Org 2000
CLASS zcl_badi_vkorg_2000 DEFINITION.
PUBLIC SECTION.
INTERFACES: zif_sales_badi.
ENDCLASS.
CLASS zcl_badi_vkorg_2000 IMPLEMENTATION.
METHOD zif_sales_badi~process.
" Different logic for Sales Org 2000
cs_data-discount = '15.00'.
ENDMETHOD.
ENDCLASS.
" Call with filter
DATA: lo_badi TYPE REF TO zbadi_sales.
GET BADI lo_badi
FILTERS
vkorg = lv_vkorg. " Only matching implementation
CALL BADI lo_badi->process
CHANGING cs_data = ls_data.

5. Fallback Class (Default Implementation)

" BAdI with fallback (SE18):
" Fallback class: ZCL_DEFAULT_HANDLER
CLASS zcl_default_handler DEFINITION
PUBLIC FINAL.
PUBLIC SECTION.
INTERFACES: zif_custom_badi.
ENDCLASS.
CLASS zcl_default_handler IMPLEMENTATION.
METHOD zif_custom_badi~process.
" Default behavior when no implementation is active
rv_result = 'DEFAULT'.
ENDMETHOD.
ENDCLASS.
" Call - fallback is used when no implementation is active
DATA: lo_badi TYPE REF TO zbadi_custom.
GET BADI lo_badi.
" Check if implementation exists
IF lo_badi IS BOUND.
CALL BADI lo_badi->process
IMPORTING rv_result = lv_result.
ENDIF.

6. Multiple Implementations

" BAdI allows multiple implementations (Multiple Use)
" All active implementations are called
DATA: lo_badi TYPE REF TO zbadi_validators,
lt_messages TYPE bapiret2_t.
GET BADI lo_badi.
" All implementations are iterated
CALL BADI lo_badi->validate
EXPORTING is_data = ls_data
CHANGING ct_messages = lt_messages.
" lt_messages contains messages from all implementations
IF line_exists( lt_messages[ type = 'E' ] ).
" At least one implementation reported an error
ENDIF.

7. Enhancement Implementations

" Implicit enhancement points in standard code
" Can be extended without modification
" In standard code exists:
" ENHANCEMENT-POINT ep_before_save SPOTS es_order.
" Own implementation (SE19):
ENHANCEMENT zei_order_enhancement.
" Code is inserted at this point
IF ls_order-custom_field IS INITIAL.
ls_order-custom_field = 'DEFAULT'.
ENDIF.
ENDENHANCEMENT.

8. Enhancement Sections

" In standard code:
" ENHANCEMENT-SECTION es_validation SPOTS es_order.
" " Original code
" IF lv_amount < 0.
" lv_error = abap_true.
" ENDIF.
" END-ENHANCEMENT-SECTION.
" Own implementation replaces original:
ENHANCEMENT zei_validation_replacement.
" Own code replaces original
IF lv_amount < 0 OR lv_amount > 1000000.
lv_error = abap_true.
ENDIF.
ENDENHANCEMENT.

9. BAdI in RAP

" Extension Include in RAP Behavior Definition:
" managed implementation in class zbp_i_order unique;
" with additional save
" {
" ...
" determination SetDefaults on save { create; }
" }
" BAdI for RAP Extensions
INTERFACE zif_rap_order_extension PUBLIC.
METHODS: on_before_save
CHANGING ct_orders TYPE zt_orders.
METHODS: on_after_save
IMPORTING it_orders TYPE zt_orders.
ENDINTERFACE.
" Call in Behavior Handler
CLASS lhc_order DEFINITION INHERITING FROM cl_abap_behavior_handler.
PRIVATE SECTION.
METHODS save_modified FOR SAVE
IMPORTING it_orders FOR CREATE order.
ENDCLASS.
CLASS lhc_order IMPLEMENTATION.
METHOD save_modified.
DATA: lo_badi TYPE REF TO zbadi_rap_order.
" BAdI before save
GET BADI lo_badi.
DATA(lt_orders) = CORRESPONDING zt_orders( it_orders ).
CALL BADI lo_badi->on_before_save
CHANGING ct_orders = lt_orders.
" Save...
" BAdI after save
CALL BADI lo_badi->on_after_save
EXPORTING it_orders = lt_orders.
ENDMETHOD.
ENDCLASS.

10. Classic BAdI (Legacy)

" Classic BAdI from SE18 (before Enhancement Spots)
DATA: lo_badi TYPE REF TO if_ex_badi_name.
" Get handle
CALL METHOD cl_exithandler=>get_instance
CHANGING instance = lo_badi.
" Call method
IF lo_badi IS BOUND.
CALL METHOD lo_badi->method_name
EXPORTING param = value
CHANGING data = ls_data.
ENDIF.

11. Activate/Deactivate BAdI Implementation

" Programmatically check if BAdI is active
DATA: lo_badi TYPE REF TO zbadi_test.
GET BADI lo_badi.
" Check if implementations exist
DATA(lv_has_impl) = boolc( lo_badi IS BOUND ).
" In SE19:
" - Open implementation
" - Set/remove "Active" checkbox
" - Activate

12. BAdI with Context

" BAdI with additional context
INTERFACE zif_context_badi PUBLIC.
METHODS: process
IMPORTING
iv_context TYPE string
is_data TYPE ty_data
EXPORTING
es_result TYPE ty_result.
ENDINTERFACE.
" Call with context for filtering
DATA: lo_badi TYPE REF TO zbadi_context.
GET BADI lo_badi
FILTERS
context = 'SPECIAL_CASE'.
" Only implementations for this context are called
CALL BADI lo_badi->process
EXPORTING
iv_context = 'SPECIAL_CASE'
is_data = ls_data
IMPORTING
es_result = ls_result.

13. Source Code Plugins (SCP)

" Source Code Plugins for ABAP Cloud
" Defined in the Behavior Definition
" managed implementation in class zbp_i_product;
"
" define behavior for ZI_Product
" {
" ...
" // Extension point for partner
" extension point productExtension;
" }
" Partner extension implements:
" enhance behavior for ZI_Product
" {
" extend productExtension
" {
" determination CalculateDiscount on modify { field price; }
" }
" }

14. User Exit (Legacy)

" Classic User Exits (CMOD/SMOD)
" Included via includes
" Include ZXaaau01 for Exit EXIT_SAPMM06E_001
FORM userexit_save_document_prepare.
" Customer-specific logic before save
IF sy-tcode = 'ME21N'.
" Modify purchase order header
ENDIF.
ENDFORM.

15. Enhancement Framework Overview

" Find all enhancements for an object
" 1. SE84 - Repository Information System
" Search for Enhancement Spots for the object
" 2. Search in code for:
" - ENHANCEMENT-POINT
" - ENHANCEMENT-SECTION
" - GET BADI
" 3. SE18 - Search BAdI definitions
" Where-used list shows implementations
" 4. SE19 - Display implementations
" All active/inactive implementations

Create Your Own Enhancement Spot (Step by Step)

1. SE18 → Create Enhancement Spot
- Name: ZES_MY_APPLICATION
- Enter short description
- Save
2. Add BAdI Definition
- Name: ZBADI_MY_BADI
- Create interface: ZIF_MY_BADI
- Define methods in interface
- Optional: Define filter
- Optional: Specify fallback class
3. Activate Enhancement Spot
4. Include in code:
DATA: lo_badi TYPE REF TO zbadi_my_badi.
GET BADI lo_badi.
CALL BADI lo_badi->my_method( ... ).
5. Create Implementation (SE19)
- Enhancement Implementation: ZEI_MY_IMPL
- BAdI Implementation: ZBI_MY_IMPL
- Implementing Class: ZCL_MY_BADI_IMPL

Important Transactions

TransactionDescription
SE18BAdI Definition / Enhancement Spot
SE19BAdI Implementation
SE80Repository Browser (Enhancements)
SE84Repository Information System
SPAUModification Adjustment

Important Notes / Best Practice

  • Prefer new BAdIs (GET/CALL BADI) over classic ones.
  • Use filters for context-specific implementations.
  • Define fallback class for default behavior.
  • Multiple Use when multiple implementations are allowed.
  • Use Enhancement Spots for your own extension points.
  • Implementations can be activated/deactivated.
  • Don’t forget documentation of the BAdI interface.
  • Use Extension Points in RAP.
  • No modifications – always prefer enhancements.
  • Combine with Unit Testing for testability.