BAdI
ABAP Developmentintermediate

BAdI Part 3: Designing Your Own BAdI — Fallback Class and Filters

Key takeaway

Create your own new BAdI for Z programs: enhancement spot, BAdI definition, interface, filter (e. g. country or company code), fallback class for default behaviour, calling it with GET BADI / CALL BADI, and handling CX BADI NOT IMPLEMENTED.

Explanation

📌 KEY POINTS
✅ You can define your own new BAdIs in Z programs so others can extend them without changing your code.

✅ Filters (e.g. BUKRS, LAND1) select the implementation for the current context.

✅ A fallback class provides default behaviour when no implementation matches the filter.

⚠️ For single-use BAdIs without fallback, GET BADI raises CX_BADI_NOT_IMPLEMENTED if no implementation exists — handle it.

━━━━━━━━━━━━━━━━━━━━
▶ WHERE WE ARE

Parts 1–2 implemented SAP's BAdIs. Now you design one for your own code — typical for global templates.

━━━━━━━━━━━━━━━━━━━━
▶ 1. WHEN TO CREATE AN OWN BADI

  • A Z program runs in several countries or companies with different rules.
  • Other teams must extend your logic independently.
  • A default behaviour exists, with exceptions for some units.

━━━━━━━━━━━━━━━━━━━━
▶ 2. BUILD IT (SE18 / SE20)

  1. Create enhancement spot ZES_INVOICE_CHECK (package, transport).
  2. In the spot, create BAdI definition ZBADI_INVOICE_CHECK.
  3. Settings: single use or multiple use (Part 5); "Limited filter use"; instance creation mode.
  4. Interface ZIF_EX_INVOICE_CHECK with method CHECK (IMPORTING is_invoice, EXPORTING et_messages or RAISING an exception class).
  5. Filter: BUKRS (type BUKRS), compare operators allowed.
  6. Fallback class: ZCL_INVOICE_CHECK_DEFAULT implementing the interface (and IF_BADI_INTERFACE).

━━━━━━━━━━━━━━━━━━━━
▶ 3. CALL IT

GET BADI lo_badi FILTERS bukrs = lv_bukrs.

CALL BADI lo_badi->check EXPORTING … .

  • With a fallback class, a single-use BAdI always finds an implementation.
  • Multiple-use BAdIs call all matching implementations; with none, the call does nothing.

━━━━━━━━━━━━━━━━━━━━
▶ 4. IMPLEMENTATIONS

Company-specific teams create implementations with filter values (BUKRS = 1000, BUKRS = 2000). The core program stays unchanged.

━━━━━━━━━━━━━━━━━━━━
▶ 5. DESIGN TIPS

  • Keep the interface stable — changing it breaks all implementations.
  • Pass data as structures/tables; return messages instead of displaying them.
  • Document what each method must do and when it is called.

━━━━━━━━━━━━━━━━━━━━
▶ STEP BY STEP

  1. Create spot, BAdI, interface, filter and fallback class as above.
  2. Call it from a Z report (code example).
  3. Implement it for company code 1000 and compare output with company code 2000 (fallback).

✅ CHECKPOINT: you can provide your own filter-dependent BAdI with default behaviour.

Code example

ABAP Code
* ===== Interface ZIF_EX_INVOICE_CHECK (BAdI interface) =====* INTERFACE zif_ex_invoice_check PUBLIC.*   INTERFACES if_badi_interface.*   METHODS check*     IMPORTING is_invoice  TYPE zs_invoice*     EXPORTING et_messages TYPE bapiret2_t.* ENDINTERFACE. * ===== Fallback class: default rule for all company codes =====* CLASS zcl_invoice_check_default DEFINITION PUBLIC FINAL CREATE PUBLIC.*   PUBLIC SECTION.*     INTERFACES if_badi_interface.*     INTERFACES zif_ex_invoice_check.* ENDCLASS.* CLASS zcl_invoice_check_default IMPLEMENTATION.*   METHOD zif_ex_invoice_check~check.*     " default: amount must be positive*   ENDMETHOD.* ENDCLASS. * ===== Core program calling the own BAdI =====REPORT zinvoice_check. PARAMETERS p_bukrs TYPE bukrs DEFAULT '1000'. DATA: lo_badi     TYPE REF TO zbadi_invoice_check,      ls_invoice  TYPE zs_invoice,      lt_messages TYPE bapiret2_t,      ls_message  TYPE bapiret2. START-OF-SELECTION.  ls_invoice-bukrs  = p_bukrs.  ls_invoice-amount = '-10'.   TRY.      GET BADI lo_badi FILTERS bukrs = p_bukrs.      " fallback if no filter match      CALL BADI lo_badi->check        EXPORTING is_invoice  = ls_invoice        IMPORTING et_messages = lt_messages.    CATCH cx_badi_not_implemented.                  " only without fallback class      WRITE: / 'No implementation for company code', p_bukrs.  ENDTRY.   LOOP AT lt_messages INTO ls_message.    WRITE: / ls_message-type, ls_message-message.  ENDLOOP. * ===== Company-specific implementation (filter BUKRS = 1000) =====* METHOD zif_ex_invoice_check~check.*   " company 1000: additionally require a reference*   IF is_invoice-xblnr IS INITIAL.*     ls_msg-type = 'E'. ls_msg-id = 'ZFI'. ls_msg-number = '010'.*     APPEND ls_msg TO et_messages.*   ENDIF.* ENDMETHOD.

Real project scenario

A shared service centre ran one invoice verification program for 12 company codes. Local rules were first coded as IF bukrs = … blocks, and every change required retesting all companies.
A custom BAdI with filter BUKRS and a fallback class for the group-wide rule allowed each company team to maintain its own implementation. The core program was frozen, and a new company code only needed an implementation if it deviated from the default.

Common mistakes

  • No fallback class for a single-use BAdI (CX_BADI_NOT_IMPLEMENTED).
  • Changing the interface after implementations exist.
  • Displaying messages inside implementations.
  • Filters that are too fine-grained or overlapping.
  • Creating an own BAdI where a simple customizing table would do.

Best practices

  • Use a fallback class for default behaviour.
  • Design a stable interface returning messages.
  • Choose filters matching the organisational split.
  • Document the BAdI contract.
  • Catch CX_BADI_NOT_IMPLEMENTED where no fallback exists.

Interview angle

Questions: "How do you create your own BAdI?", "What is a fallback class?", "What happens if no implementation exists?"
Strong answer: enhancement spot + BAdI definition (single/multiple use, filter) + interface with IF_BADI_INTERFACE + optional fallback class; call with GET BADI … FILTERS and CALL BADI; fallback provides default behaviour; single-use without implementation raises CX_BADI_NOT_IMPLEMENTED, multiple-use simply calls nothing.

Practice this lesson

Quick check

1. A single-use custom BAdI has a fallback class. What happens for a filter value with no implementation?

2. If a BAdI has no active implementation at all, what happens when the program reaches that enhancement call point?

3. A fallback class runs when:

Interview questions

1. A new sales organization went live, and suddenly a custom BAdI-based check was not applied to it. The BAdI is filter-dependent by sales organization. What happened, and how should the BAdI be designed?

Answer it in your own words first, then compare.

2. Difference between classic and new BAdI.

Answer it in your own words first, then compare.

Practice more with Interview AI →

Reference-verified: the steps are checked against official SAP documentation and tutorials, and object and field names in the sample code are checked for consistency. The code has not been compiled or run on an SAP system by ERPClimb. Syntax marked release-dependent varies by SAP release, so check it in your own system.

More lessons in this topic

Interview questions on this topic

Related SAP topics