Code examples · CDS views · Intermediate
CDS text view: show a readable status text in the right language
The problem
The leave list shows status codes N, A and R.
How we solve it
- 1Text tableZLEAVE_STAT_T
- 2Text viewZI_LeaveStatusText
- 3Main viewassociation + annotation
The result
The leave view exposes a text association.
1. The requirement
The leave list shows status codes N, A and R. Users want to read 'New', 'Approved', 'Rejected' in their own logon language, and a translator must be able to add a language without touching the leave view.
2. How we design it
Keep the code and its texts apart. A small text table holds one row per code and language. A text view marks which field is the language and which is the text. The main view points to it with one association and one annotation, and Fiori shows the text for the user's language automatically.
Why we decided it this way
- 1A separate text tableA new language is a new row, not a code change.
- 2Semantic annotations on language and textConsumers know which field is which without guessing.
- 3An association, not a joinThe text is only read when a consumer needs it.
Before you start
- The table ZLEAVE_REQ from the managed RAP example.
- A way to add a few rows to the text table, for example a small console class.
3. The code, step by step
Step 1 of 3: Create the text table
One row per status and language.
// Table definition
@EndUserText.label : 'Leave status texts'
@AbapCatalog.enhancement.category : #NOT_EXTENSIBLE
@AbapCatalog.tableCategory : #TRANSPARENT
@AbapCatalog.deliveryClass : #A
@AbapCatalog.dataMaintenance : #RESTRICTED
define table zleave_stat_t {
key client : abap.clnt not null;
key status : abap.char(1) not null;
key spras : abap.lang not null;
text : abap.char(30);
}
Part by part (plain text)
- Table definition. The key is client, status and language. The language field uses the standard language type. The text column holds the readable text.
Open in the portal
The SAP objects named in this code are linked in the code above and listed here. Each opens its page in the SAP Library.
If it does not work
4. The final outcome
The leave view exposes a text association. Fiori elements shows the status as text, in the language of the user.
What you should see
Logon language EN: Approved. Logon language DE with a German text row: Genehmigt. If no text row exists for the language, the code A is shown.
Try it and check the result
- Add rows to ZLEAVE_STAT_T with a small console class: (N, E, 'New'), (A, E, 'Approved'), (R, E, 'Rejected'), (A, D, 'Genehmigt').
- In ADT open the view ZI_LeaveWithText with Data Preview (F8), right-click a row and choose Follow Association, then _StatusText: the text rows for that request appear.
- Use the view in a Fiori elements app and set the field to show text first. Change the logon language to German: A shows as Genehmigt.
Common mistakes
- Without @Semantics.language the framework cannot pick the text of the user's language.
- A missing text row is not an error. The code is shown instead, so fill every language you promise.
- The association name in the annotation must match the association name exactly, including the underscore.
- How the text is shown (text only, text first, code first) is a UI setting in the annotations of the consuming layer, not part of the text view.