Sub topic 9
Adding Custom Logic Through BAdI Implementations
Prerequisites and goals
This chapter assumes you can open the Custom Fields and Logic app in the SAP Fiori launchpad. It also assumes you have created and published a custom field (the earlier chapters on creating and publishing fields), and that you can read basic ABAP SQL. You do not need to know classic ABAP development.
By the end you should be able to:
- Explain what a BAdI is and why key users can only use some of them.
- Create, check, test and publish a custom logic implementation.
- Write logic that reads data only through released objects.
- Recognize the most common reasons an implementation fails.
What a BAdI is
A Business Add-In (BAdI) is a hook that SAP places inside a standard process. At that point, the standard code calls any active implementations and passes them data. Your implementation can read that data, change the parts the BAdI allows you to change, or reject the operation with a message.
SAP defines each hook, its name, its parameters and what you may do with them. You cannot add a hook of your own to standard code. This is the main difference from a modification, which changes SAP's code directly and can break during an upgrade.
A BAdI has two sides:
- The definition belongs to SAP. It declares an interface with one or more methods, and it sits inside an enhancement spot.
- The implementation belongs to you. It is a piece of code that fills in a method of that interface.
How key user BAdIs differ from developer BAdIs
Developers also use BAdIs, in ABAP development tools in Eclipse. Key users use a smaller and more restricted version of the same idea.
| Aspect | Key user custom logic | Developer extensibility |
|---|---|---|
| Tool | Custom Fields and Logic app | ABAP development tools in Eclipse |
| Available BAdIs | Only those SAP has released for key users | Any BAdI released for the developer's extensibility model |
| ABAP language | Restricted subset | Broader, still limited to released objects in a clean core setup |
| Data access | Released objects only, such as released CDS views | Released objects, plus more structuring options |
| Typical author | Business expert or functional consultant | ABAP developer |
| Packaging | Created and transported as an extension | Development objects in packages |
Both models follow the same rule: your code may call only objects that SAP has released for that use. The key user model enforces this rule more tightly, and the editor checks it for you.
Where custom logic runs
The sequence below shows what happens when a user saves a business object that has an active BAdI implementation.
Two consequences follow. First, your code runs inside the standard transaction, so slow logic slows down every user who saves. Second, a runtime error in your code can interrupt a business process, so treat custom logic as production code even though a key user writes it.
Creating an implementation step by step
The exact screens vary a little between releases. The workflow is the same.
- Open the Custom Fields and Logic app and go to the Custom Logic tab.
- Choose New. The app lists the BAdIs available for key users, grouped by business context. Select the one that matches your scenario.
- Enter an implementation identifier and a description. Use a clear description. Other key users will see it in the list of implementations and need to know what it does.
- Open the editor. It generates the method with its signature and shows the parameters you can use. Do not change the signature.
- Write your logic in the method body.
- Run the syntax check and fix every finding.
- Test the logic with the simulation function (if the app offers it for your BAdI), or test it in a non-productive client.
- Publish the implementation. Only a published implementation runs in the standard process.
Your implementation also appears in the transport flow described in the lifecycle chapter. Publish it in the development system first, then transport it.
Choosing the right BAdI
Before you write code, check three things about the BAdI:
- When it runs. For example, a BAdI that runs on save can validate or default values. A BAdI that runs after posting is too late to stop the operation.
- What it exposes. Look at the importing and changing parameters. If a field you need is not in the parameters, you must read it yourself from a released object, or the BAdI is the wrong choice.
- Whether it can return messages. Validation needs a message parameter. A BAdI without one can fill in values but cannot block a save.
Each BAdI documents these points, so read the documentation in the editor before you pick one.
Writing the logic: released objects only
Key user logic may read data only through released objects. A released object is one that SAP has marked as stable for use in extensions. Released CDS views (names starting with I_) are the main source of data. Standard tables such as MARA are not released for this purpose, so the syntax check rejects them.
This restriction supports the clean core idea from the earlier chapter. SAP can change the internal tables behind a released view, but it keeps the view's contract stable. Your code keeps working after an upgrade.
The example reads the product type of a product from the released CDS view I_Product. The first line of each statement block shows where to place it inside your method.
" Read data through a released CDS view, as allowed in key user custom logic.
DATA lv_product TYPE I_Product-Product 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.Points to notice in this snippet:
- Host variables carry an
@prefix. ABAP SQL in the restricted language requires escaped host variables (@lv_product). Without the@, the check fails. - The clauses are in modern order. The
INTOclause comes last, afterWHERE. - Types come from the released view. Declaring
lv_product_typewithI_Product-ProductTypeties your variable to a released element. - The hard-coded
'TG11'is for illustration. In real logic, take the product from the BAdI's input parameters. - You check
sy-subrcafterSELECT SINGLE. If the product does not exist, the variable stays initial. Code that assumes a hit gives wrong results or confusing behavior.
Using your custom fields in logic
A custom field you created and published is part of the business object's structure. Once it is published, the BAdI parameters (for BAdIs on that business context) can carry it, and released CDS views for that context expose it. It appears with its YY1_ prefix, for example YY1_CostCenterNote_PRD. The field must be published before the editor offers it, so a field that is missing from the editor is usually unpublished or in a different business context.
Returning messages
For validations, return a message through the BAdI's message parameter instead of ending the program. The method signature in the editor shows the parameter and the structure it expects. Write messages for the end user: say which field is wrong and what to enter. A message such as "Error in custom logic" gives the user nothing to act on.
Common errors and how to read them
| Symptom | Likely cause | What to do |
|---|---|---|
| Syntax check reports an object is not released | You used a table, class or function module outside the released set | Find a released CDS view with the data, or reconsider the approach |
| `@` or `INTO` error in a SELECT | Old-style ABAP SQL syntax | Use escaped host variables and put `INTO` last |
| Custom field not offered in the editor | Field is unpublished, or belongs to another business context | Publish the field, then check its context in the Custom Fields tab |
| Logic never runs | Implementation is saved but not published, or the BAdI is called in a different step than you expected | Publish it, then read the BAdI documentation for when it runs |
| Save fails with a dump or generic error | Unhandled case, such as an empty `SELECT` result, or a division by zero | Check `sy-subrc` and guard every calculation |
| Dialog is slow after publishing | A `SELECT` inside a loop, or reading too many rows | Read once before the loop, restrict with `WHERE`, select only needed columns |
| Works in development, missing in quality | Implementation was not transported, or the custom field it needs was not | Transport the field and the logic together |
Edge cases and design guidance
Several implementations for one BAdI. Some BAdIs allow more than one active implementation, and others allow only one. If the app blocks a second implementation, extend the existing one. When more than one runs, do not rely on the order, because SAP does not guarantee it. Make each implementation independent.
Empty and initial values. A field the user left blank arrives as its initial value, not as a null. For an amount, that is 0. For a date, it is 00000000. Decide whether an initial value means "not entered" and handle it explicitly.
Validation versus defaulting. A check that only rejects bad input is easy to reason about. Logic that silently changes values is harder to trace, because the user sees a result they did not enter. Prefer validation, and use defaulting only where the BAdI is meant for it and the business agrees.
Keep logic small. Key user logic is best for short rules: a check, a default, a derived field. If you need many selects, loops or shared helper code, that suggests a developer or side-by-side extension. The chapter on custom CDS views and business objects covers some alternatives.
Authorizations. Reading through a released CDS view does not mean the user may see that data in other apps. Check whether the view applies its own authorization checks, and do not use the logic to expose data a user could not otherwise open.
Test with realistic data. Simulate with at least three cases: a normal case, a case where the lookup returns nothing, and a case with initial input values. Most production failures come from the second and third.
Summary
A BAdI implementation lets a key user add a rule to a standard process at a point SAP has defined. Choose the BAdI by when it runs, what it exposes and whether it can return messages. Read data only through released objects, using escaped host variables, and check sy-subrc after every lookup. Keep the logic short, test the empty and initial cases, and publish only after testing.
Exercise
A key user writes custom logic that reads the product type with SELECT SINGLE ProductType FROM I_Product WHERE Product = lv_product INTO lv_product_type. The syntax check fails. Name the problem, give the corrected statement, and describe what else the logic must do after the statement so it does not fail on unknown products.
Show answer
The host variable lv_product lacks the @ escape that ABAP SQL requires, and the INTO target should be written as @lv_product_type. The corrected statement is SELECT SINGLE ProductType FROM I_Product WHERE Product = @lv_product INTO @lv_product_type. After the statement, the logic must check sy-subrc. If it is not 0, no product was found, and the logic should handle that case, for example by returning a clear message through the BAdI's message parameter, instead of using the initial value of lv_product_type.
Exercise
Your validation logic works in the development system. After the transport, the quality system never shows your message, and the editor there does not list your custom field YY1_CostCenterNote_PRD. Give two checks, in order, to find the cause.
Show answer
First, check whether the custom field was transported and published in the quality system, because the logic depends on it and an unpublished or missing field is not offered in the editor. Second, check whether the implementation itself is published and active in the quality system, because saved but unpublished logic does not run. If both are fine, read the BAdI documentation to confirm the BAdI is called at the step where you expect the message.