BAdI Part 3: Designing Your Own BAdI — Fallback Class and Filters
Lessons are open to try during our launch period. Some advanced content may move to Pro later. AI practice limits apply separately.
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.
In this lessonShow sectionsHide sections
Visual overview
Expert visual walkthrough
Designing Your Own BAdI — Fallback Class and Filters
BAdI · Chapter-specific technical teaching map
Architecture hierarchy
BAdI definition
Interface
Fallback class
Filter values
Tutor's chapter 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.
In a real project
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…
Watch out for
• 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. •…
The situation: a 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.
Explanation
Parts 1 and 2 implemented SAP's own BAdIs. Now it's time to design one of your own — a typical need in global templates used across several countries or companies.
A few situations call for defining your own BAdI: a Z program running across several countries or companies, each with different rules; other teams needing to extend your logic independently, without touching your code; or simply a default behavior that needs exceptions for particular units. You can define your own new BAdIs in Z programs so others can extend them without changing your code.
Building one starts in SE18 and SE20. First, create an enhancement spot, say ZES_INVOICE_CHECK, with its package and transport. Inside that spot, create a BAdI definition, ZBADI_INVOICE_CHECK. Its settings include whether it's single use or multiple use — a distinction covered in Part 5 — along with "Limited filter use" and an instance-creation mode.
Define an interface, ZIF_EX_INVOICE_CHECK, with a method CHECK that takes IMPORTING is_invoice and either EXPORTING et_messages or RAISING a dedicated exception class. Add a filter on BUKRS, typed as BUKRS, with the usual comparison operators allowed. Filters (e.g. BUKRS, LAND1) select the implementation for the current context.
And create a fallback class, ZCL_INVOICE_CHECK_DEFAULT, implementing both the interface and IF_BADI_INTERFACE — this provides default behaviour when no implementation matches the filter.
Calling it looks like GET BADI lo_badi FILTERS bukrs = lv_bukrs. followed by CALL BADI lo_badi->check EXPORTING … . With a fallback class in place, a single-use BAdI always finds an implementation to run. A multiple-use BAdI, by contrast, calls every matching implementation it finds, and if none match, the call simply does nothing. For single-use BAdIs without fallback, GET BADI raises CX_BADI_NOT_IMPLEMENTED if no implementation exists — handle it, since there's no silent default to fall back on in that case.
From there, company-specific teams create their own implementations with their own filter values — BUKRS = 1000 for one, BUKRS = 2000 for another — and the core program itself never needs to change.
A few design tips matter here: keep the interface stable, since changing it breaks every existing implementation at once; pass data as structures or tables rather than loose parameters; and return messages rather than displaying them directly, leaving that choice to the caller. It's also worth documenting clearly what each method is expected to do, and exactly when it gets called.
As an exercise, create the spot, BAdI, interface, filter and fallback class as described above, call it from a Z report as shown in the code example, and implement it for company code 1000, comparing its output against company code 2000, which falls back to the default. By the end, you should be able to provide your own filter-dependent BAdI, complete with sensible default behavior.
Code example
* ===== 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.Watch out
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.
Do this
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.
In the interview
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
0 of 3 questions attempted
Question 1 of 3
A single-use custom BAdI has a fallback class. What happens for a filter value with no implementation?
Question 2 of 3
If a BAdI has no active implementation at all, what happens when the program reaches that enhancement call point?
Question 3 of 3
A fallback class runs when:
Practise this in an interview
Answer in your own words and get instant feedback, the way an interviewer would judge it.
1A 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?
2Difference between classic and new BAdI.
Try a related Code Lab
This published exercise covers the same SAP topic. Apply the ideas in a hands-on scenario, then return to review your answers.
Open: SELECT Inside BAdI Item Loop →See it in code
Each example starts from a requirement, shows the design, then the code step by step with a plain-language explanation of every part.
Was this lesson helpful?
Keep exploring
Interview questions on this topic
Related SAP topics
Referenced SAP objects
Reviewed reference pages for the objects this page mentions.
Check before use: lesson examples are educational, not a guarantee of correctness for your SAP edition or release. Sample code has not been executed on your SAP system. Confirm API availability, syntax, authorizations and business impact in an approved development or test environment.