Sub topic 10
Custom CDS Views and Custom Business Objects
This chapter covers two key user tools that go beyond a single custom field: custom CDS views for reading and reporting on data, and custom business objects for storing new data. It assumes you can create and publish a custom field, you know the released-objects rule from the previous chapters, and you can read basic SQL.
Where these tools fit
Custom fields add one attribute to an existing standard object. Sometimes that is not enough. You may need a report that joins several released views, or a small table of your own, such as a list of approved suppliers per plant. Two apps cover these cases:
- Custom CDS Views lets a key user define a read-only data model on top of released objects.
- Custom Business Objects lets a key user define a new persistent object, with its own fields, its own database storage and a generated service.
Both apps follow the clean core rule. They accept only released objects as sources and generate their runtime artifacts for you, so an upgrade of the standard system does not break them.
| Question | Custom CDS view | Custom business object |
|---|---|---|
| Stores data? | No. It reads data at query time. | Yes. It creates database tables. |
| Typical use | Reporting, analytics, value helps, data sources for other extensions | New master data, configuration tables, documents the standard lacks |
| Built on | Released CDS views and other released objects | Its own fields, with optional links to released objects |
| Can write data? | No | Yes, through the generated service, UI or custom logic |
Custom CDS views
A CDS view (Core Data Services view) is a database view defined in a declarative language. It selects fields from other views, joins them, filters them and exposes the result under a new name. A custom CDS view is a CDS view that a key user creates in the Custom CDS Views app instead of in ABAP development tools.
What you can use as a source
The data sources you may select are limited to released objects. In practice these are released CDS views, usually the interface views whose names start with I_, such as I_Product, I_SalesOrder or I_BusinessPartner. The app shows only objects that SAP has released for key user use. If a view is not in the list, you cannot use it, and you should not look for a workaround that reads the underlying tables.
Creating a custom CDS view
- Open the Custom CDS Views app from the launchpad. You need the key user authorizations covered earlier in this tutorial.
- Choose New and enter a name and a label. The system adds the
YY1_prefix, so the view is easy to recognize later. - Add one or more released views as data sources. The first is the primary source. Additional sources are joined to it.
- Define the join conditions between sources, for example matching the product number in both.
- Select the fields to expose. Rename them if the standard names are unclear to report users.
- Optionally add calculated elements and parameters, and set the Usage options described in the next section.
- Save the draft, then choose Publish.
Publishing generates the runtime CDS view. Until you publish, the view exists only as a draft and no other tool can use it.
Usage options
Where a custom CDS view can be consumed depends on the options you set:
- Analytical data source: makes the view available in analytical apps such as the custom analytical queries tools.
- Value help: lets the view supply the choices in a drop-down of a custom field.
- Extension include view: ties the view to extension scenarios.
Choose only what you need. Each option adds checks and generated artifacts, and a view that is meant only as a reusable building block does not need them.
Using the result
After publication, you can:
- Use the view as a data source in analytical apps and custom queries.
- Read from it in custom logic, with the same ABAP SQL rules as for released views.
- Combine it with other custom CDS views, as long as each source is itself published.
The following snippet reads from a custom CDS view in custom logic. The view name YY1_ProductOverview is an example.
" Read from a published custom CDS view in key user custom logic.
DATA lv_product TYPE matnr VALUE 'TG11'.
DATA lv_product_type TYPE I_Product-ProductType.
SELECT SINGLE ProductType
FROM YY1_ProductOverview
WHERE Product = @lv_product
INTO @lv_product_type.
IF sy-subrc <> 0.
" No row found: handle the case, for example by raising a message.
ENDIF.Notice that the syntax is the same as for I_Product. Host variables are escaped with @, and the result is checked with sy-subrc.
Dependencies and changes
A custom CDS view that uses a custom field must come after that field's publication. If you later change a published view, for example by removing an exposed field, anything that depends on that field can fail. The app warns you about known dependencies, but it cannot see every consumer, such as a query built by another team. Add new fields instead of removing old ones, and check usage before you delete.
Custom business objects
A business object (BO) is a unit of data and behavior that the system treats as a whole, such as a sales order with its items. A custom business object is a BO that a key user defines in the Custom Business Objects app. You choose the fields. The system then generates what a developer would otherwise write by hand.
What the system generates
When you publish a custom business object, the system creates:
- A database table to hold the data.
- A CDS-based data model with a root node and, if you define them, child nodes.
- An OData service so that UIs and other systems can read and write instances.
- An optional UI for display and maintenance, if you request one.
- Hooks where you can add custom logic, such as validations and determinations.
A validation is logic that checks data and rejects invalid input. A determination is logic that fills or changes fields automatically when data is saved or changed.
Defining the model
- Open Custom Business Objects and choose New.
- Enter a name and label. The generated object carries the
YY1_prefix. - Define the fields. Each field has a name, a label and a data type such as text, number, date or amount. Mark which fields make up the key.
- Decide whether the object has a single level or a node hierarchy.
- Set the options for services and UI generation.
- Save and publish.
Plan the key and the data types carefully. Once data exists, changing the type or length of a field is restricted, and a wrong choice can mean creating a new object and migrating the data.
Choosing generation options
| Option | What it gives you | When to choose it |
|---|---|---|
| Service generation | An OData service for the object | When UIs, reports or other systems need access |
| UI generation | A simple maintenance app | When users must create and edit entries directly |
| Draft handling | Users can save unfinished work | When entries are long or entered over several sessions |
| Authorization check | Access control on the object | When not every user may read or change the entries |
If you skip service generation, the data can be reached only from custom logic in the same system. That is acceptable for an internal helper table, but it limits reuse.
Combining the two tools
The two apps work well together. A common pattern is:
- Create a custom business object that stores a small table, for example approved suppliers per plant.
- Create a custom CDS view that joins the business object's data with released views such as
I_Supplier. - Use the CDS view as an analytical data source, or as a value help in a custom field.
- Add custom logic in a BAdI implementation that reads the view to check a rule during document processing.
The diagram shows how data flows through that pattern.
Writing data from custom logic
Custom logic attached to a custom business object can read and write that object. It remains subject to the same ABAP restrictions as other key user logic. You may use released APIs and released CDS views, but not unreleased function modules, direct table access or arbitrary classes.
The following snippet reads rows from a custom business object's CDS view and returns a flag. The object and field names are examples.
" Check whether a supplier is approved for a plant, using a custom CDS view.
DATA lv_supplier TYPE lifnr VALUE '0010000001'.
DATA lv_plant TYPE werks_d VALUE '1010'.
DATA lv_approved TYPE abap_bool VALUE abap_false.
SELECT SINGLE @abap_true
FROM YY1_ApprovedSupplier
WHERE Supplier = @lv_supplier
AND Plant = @lv_plant
INTO @lv_approved.
IF lv_approved = abap_false.
" The supplier is not approved: raise a message or stop the process.
ENDIF.If the SELECT SINGLE finds no row, lv_approved keeps its initial value abap_false, so the check works without evaluating sy-subrc.
Limits and edge cases
- Released objects only. A custom CDS view cannot read an unreleased view or a table. If the data you need is not in a released view, check whether SAP offers a released alternative before considering a different extension model.
- Draft versus published. Both apps use drafts. Nothing takes effect until you publish. A published object that has unpublished changes behaves according to its last published version.
- Naming. Names are fixed after creation, and the
YY1_prefix applies. Pick clear names, because they appear in APIs and query tools. - Performance. A custom CDS view runs its joins every time it is read. Wide joins across large views, such as item-level documents, can be slow. Filter early, expose only the fields you need, and test with realistic data volumes.
- Authorizations. A custom CDS view does not automatically restrict who sees the data. The access control of the underlying released views usually applies, but you should verify with a test user who has limited authorizations. Custom business objects need their own authorization setup.
- Data growth. A custom business object stores data in your system. Define how long entries must be kept and who removes old ones, since there is no automatic archiving.
- Transport. Both kinds of object are captured in a transport request when you publish them in a development system. Dependent objects must arrive in the target system in a valid order. The next chapter covers this in detail.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| A released view does not appear as a data source | The view is not released for key user use | Search for a released alternative, or ask whether SAP offers one |
| Publishing a view fails | A source field or custom field was removed or is unpublished | Publish or restore the source, then publish again |
| A report shows no data from a custom field | The field was not added to the view or its usage was not enabled | Add the field to the view and republish |
| A user sees the custom business object app but gets no data | Missing authorization for the object | Assign the required role and test again |
| The service returns an error after a model change | The generated service was not republished | Republish the object and refresh the metadata cache of the client |
Exercise
A purchasing team wants a list of plants with the suppliers that are approved for each plant. The standard system has no such list. Which two key user tools would you combine, what does each one do, and why not use only one?
Show answer
Use a custom business object to store the approved-supplier entries per plant, because a CDS view cannot store data. Use a custom CDS view to join those entries with released views such as I_Supplier, so a report can show supplier names alongside the approvals. The business object alone would give you a table and a maintenance UI but no joined reporting model. The CDS view alone would have no place to hold the approvals.
Exercise
A key user wants to build a custom CDS view directly on a database table that holds delivery data, because the released views do not expose one needed field. Is this possible, and what should the key user do instead?
Show answer
It is not possible. The Custom CDS Views app accepts only released objects as data sources, which protects the clean core. The key user should check whether another released view exposes the field, or whether a custom field on the standard object can provide it. If neither exists, the requirement needs a different extension model, such as developer or side-by-side extensibility.