Sub topic 9
Adding custom logic through BAdI implementations
This chapter assumes you can navigate the SAP Fiori launchpad and have created and published a custom field. It also assumes you can read basic ABAP or SQL. You will learn how a key user hooks custom logic into a standard process and which rules keep that logic upgrade-stable.
What a BAdI is
A Business Add-In (BAdI) is an enhancement spot that SAP places inside a standard process. The standard code calls the BAdI at a defined point. If you have created an implementation, SAP runs it at that point. If you have not, the process continues unchanged.
In key user extensibility you work with released BAdIs only. SAP guarantees that their interfaces stay stable across upgrades. This is why your logic survives a release change without rework.
You create the implementation in the Custom Fields and Logic app, on the Custom Logic tab. A typical use is to fill a custom field automatically or to validate input before the document is saved.
How the call flows
The following diagram shows where your implementation sits in a save operation.
Creating an implementation
Follow these steps.
- Open the Custom Fields and Logic app and switch to the Custom Logic tab.
- Choose New and select the business context that matches your process, for example sales orders.
- Pick the released BAdI. Each BAdI describes when it is called and what data it passes in.
- Enter a name and description for the implementation.
- Write the logic in the editor. The editor opens with a method skeleton whose parameters carry the business data.
- Check the syntax, then save and activate the implementation.
The editor offers a simulation function. Use it to run the logic with test data before you activate it in a productive process.
Reading data with released objects
Key user logic can read data only through released objects. For master data, use released CDS views such as I_Product. Direct access to database tables is not allowed. The ABAP language scope is also limited, and the syntax check rejects statements outside it.
The example below reads the product type for a product through the released view. It uses ABAP SQL with host variables, which are variables written with a leading @.
" Read data through a released CDS view, as allowed in key user custom logic.
DATA lv_product TYPE matnr VALUE 'TG11'.
DATA lv_product_type TYPE I_Product-ProductType.
SELECT SINGLE ProductType
FROM I_Product
WHERE Product = @lv_product
INTO @lv_product_type.
IF sy-subrc <> 0.
" No product found: handle the case here, for example by raising a message.
ENDIF.Always check sy-subrc after a SELECT SINGLE. A missing record is a normal case, and logic that ignores it produces empty values or confusing errors.
Working with custom fields in logic
A published custom field can be used in a BAdI implementation. Its name carries the YY1_ prefix, which marks key user extensions.
Custom field ID pattern: YY1_<FieldName>_<suffix>
Example: YY1_CostCenterNote_PRD
The YY1_ prefix marks key user extensions. The suffix is generated by the system from the business context.The editor lets you insert the field from the field list, so you do not need to type the generated name. This avoids mistakes with the suffix.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Syntax check rejects a statement | The statement or object is not released for key users | Use a released CDS view or a permitted ABAP statement |
| Logic does not run | The implementation is saved but not activated | Activate it and test with a new document |
| Field is unknown in the editor | The field belongs to another business context or is unpublished | Check the context and publish the field |
| Wrong or empty value read | The `SELECT SINGLE` found no row and `sy-subrc` was not checked | Test `sy-subrc` and handle the empty case |
Exercises
Exercise
Why can a key user implementation read product data from I_Product but not from a database table such as MARA?
Show answer
Key user logic may use only released objects. SAP keeps the interface of a released CDS view such as I_Product stable across upgrades, while direct table access is not released and would break the clean core. The syntax check therefore rejects the table access.
Exercise
You activated an implementation that should fill the custom field YY1CostCenterNotePRD, but the editor reports the field as unknown. Name two possible causes.
Show answer
The field has not been published yet, or it was created in a different business context than the BAdI you selected. Publish the field and check that the contexts match.