Using ABAP Annotations in CDS Views

Objectives

After completing this lesson, you will be able to:

  • Use ABAP annotations in CDS views
  • Work with translatable texts in CDS

ABAP Annotations for CDS Views

SAP annotations are CDS annotations created and delivered by SAP. Dependent on the evaluation, we distinguish two groups of SAP annotations: ABAP annotations and framework-specific annotations.

ABAP annotations

ABAP annotations define technical and semantic attributes of a CDS object. They are evaluated when the object defined in the CDS source code is activated or when the object is used in the ABAP runtime environment. ABAP annotations can modify the behavior of ABAP SQL statements that access a CDS object.

Framework-specific annotations

Framework-specific annotations define the technical and semantic properties of CDS objects that are required by specific software components, such as ODATA, UI, Analytics, and so on. They are evaluated by the frameworks of the corresponding software components using a suitable API and are not relevant for activations and the ABAP runtime environment.

Dependent on the scope, we distinguish five groups of annotations:

Entity annotations

Annotations for CDS views and CDS view entities. Placed in front of the DEFINE VIEW statement or the DEFINE VIEW ENTITY statement.

Element annotations
Annotations for an element of the SELECT list of a CDS view. Placed in front of the element.
Parameter annotations
Annotations for a parameter of a CDS view. Placed In front of the parameter.
Extension annotations
Annotations for a CDS view extension. Placed in front of the EXTEND VIEW statement or the DEFINE VIEW ENTITY statement.
Function annotations
Annotations for a CDS table function. Placed in front of the DEFINE TABLE FUNCTION statement.

Important ABAP Annotations for CDS View Entities

For CDS view entities, the following important ABAP annotations exist:

AbapCatalog.dataMaintenance

Can be used to restrict data preview for a CDS view entity.

ABAPCatalog.viewEnhancementCategory

Specifies how the view entity is extended using CDS view extensions. A comma-separated list of values can be specified in the square brackets of the annotation array.

AccessControl.authorizationCheck

Defines the implicit access control when ABAP SQL is used to access the CDS view.

ObjectModel.usageType.<...>

Characterize CDS view entities with respect to their performance-relevant properties.

AbapCatalog.entityBuffer.definitionAllowed

Can be used to enable or disable table buffering for a CDS view entity.

ABAP Annotations for View Elements

Most Semantics annotations are just this: semantic information to be evaluated by the frameworks that build on top of CDS.

There are only a few annotations in the Semantics category, which are ABAP annotations.

In CDS view entities, it is mandatory to use the annotation @Semantics.amount.currencyCode to link amount elements (predefined dictionary type CURR) to a currency code element (predefined dictionary type CUKY). In the same way, it is mandatory to use annotation @Semantics.quantity.unitOfMeasure to link quantity elements (predefined dictionary type QUAN) to a unit element (predefined dictionary type UNIT). Without these annotations, a user of the CDS entity will have difficulties correctly interpreting and processing the values provided in these fields.

Grouped Annotations

Annotations from the same annotation category can be grouped to make it visible that they belong together. In the example, there are three annotations from the category ObjectModel. All of them belong to the sub-category ObjectModel.usageType. In the lower code example, you find a group starting with @ObjectModel: followed by a pair of curly brackets. Within the brackets, there is usually a comma-separated list of annotations, without the first part (ObjectModel). In our example, there is only a sub-group that starts with usageType: and which has three annotations in its comma-separated list.

Hint

If you prefer this syntax, you can set it as the default when inserting annotations by auto-completion. In the ADT menu, go to WindowPreferences. Go to ABAP DevelopmentEditorsSource Code EditorsCDSCode Completion and under Insertion of CDS Annotations, choose Structured (multiple lines with all brackets).

ABAP Annotations for Translatable Texts

ABAP annotations starting with EndUserText are used to define translatable semantic texts for CDS objects.

EndUserText.label is available as view annotation or as element annotation. The value is limited to a length of 60 characters.

EndUserText.quickinfo is only available as element annotation. The value is stored as a string and can have an arbitrary length.

Note

When a view element is based on a dictionary data element, the CDS framework derives a label and a quickinfo from the Field labels that are maintained in data element. For such fields the text annotations are only needed, in case you want to override the texts that come from the data element.

Active Annotations View

The Active Annotations view summarizes the semantic information of the CDS entity and its elements. It lists all entity and element annotations contained in the data definition. It also lists semantic information that is derived (inherited) from other repository objects, for example dictionary table definitions and data elements.

To open the Active Annotations view, proceed as follows:

  1. Right-click Data Definition. (Either in the editor window or in the Project Explorer.)
  2. Choose Open WithActive Annotations.

In the default configuration of the ABAP perspective, the Active Annotations view is displayed in a new tab in the tabstrip below the editor view.

In the example above, you can see that element AnnualSalary of CDS view entity Z00_R_Employee has a label Annual Salary that is not defined in the CDS data definition. This label is derived (inherited) from the source field in database table Z00_EMPLOYEE and originates from data element Z00_SALARY, with which the table field is typed.

How to Use ABAP Annotations in CDS Views

Play the video to see how to use ABAP annotations in CDS views.

Use Annotations in Data Definitions

You want to enrich your CDS view entity with metadata. In a first step, you define and maintain ABAP annotations for both, the entire view and individual view elements.

Template:

  • /LRN/R_EMPLOYEE (Data Definition)

Solution:

  • /LRN/R_EMPLOYEE_ANN (Data Definition)

Task 1: Copy Template (Optional)

Copy the template data definition. If you have finished the previous exercise, you can skip this task and continue editing your data definition Z##_EMPLOYEE.

Steps

  1. Copy the /LRN/R_EMPLOYEE data definition to a data definition in your own package (suggested name: Z##_EMPLOYEE, where ## stands for your group number).

    1. In the Project Explorer view, right-click the /LRN/R_EMPLOYEE data definition to open the context menu.

    2. From the context menu, choose Duplicate ....

    3. Enter the name of your package in the Package field. In the Name field, enter the name for the CDS view entity (Z##_Employee, where ## stands for your group number).

    4. Adjust the description and choose Next.

    5. Confirm the transport request and choose Finish.

  2. Activate the copy.

    1. Press Ctrl + F3 to activate the data definition.

Task 2: Maintain View Annotations

In your data definition, maintain the view annotations. Add an annotation that can be used to restrict data preview for a CDS view entity and adjust the values of the ObjectModel.usageType annotations.

Steps

  1. At the beginning of the data definition, use code-completion to add annotation @ABAPCatalog.dataMaintenance with its default value.

    1. Place the cursor at the beginning of the first code row and press Enter to insert a new empty row.

    2. In the empty row, enter @A and press Ctrl + Space to invoke code-completion.

    3. From the suggestion list, choose @ABAPCatalog: { } (annotation.

    4. Enter a period sign (.) and press Ctrl + Space again.

    5. Choose dataMaintenance: #RESTRICTED (annotation).

  2. Group the entity annotations that start with @ABAPCatalog.

    Hint

    You can use the syntax of the @ObjectModel.usageType annotation group as a blueprint.
    1. Adjust the code as follows:

      Code Snippet
      Copy code
      Switch to dark mode
      123456789
      // @AbapCatalog.dataMaintenance: #RESTRICTED // @AbapCatalog.viewEnhancementCategory: [#NONE] @AbapCatalog: { dataMaintenance: #RESTRICTED, viewEnhancementCategory: [#NONE] } @AccessControl.authorizationCheck: #NOT_REQUIRED
  3. Display the documentation for the @ObjectModel.usageType annotations and maintain the values.

    Note

    You should make sure that the annotation values fit the technical table settings of your database table for employee data.
    1. Place the cursor on @ObjectModeland press F1 to open the ABAP language help.

    2. On the ABAP Language Help view that opens below the editor, choose@ObjectModel, CDS View annotations, View Entity.

    3. For the serviceQuality annotation, choose #D because the view does not support transactional processing.

    4. For the sizeCategory annotation, choose #M because we expect less than 100,000 data sets.

    5. For the dataClass annotation, choose #MASTER because employee data are maser data.

Task 3: Maintain Element Annotations

In your data definition, add the mandatory element annotation to establish the link between the annual salary and the currency code. Use the Active Annotations view to analyze the element labels that are derived from data elements and provide an alternative label for the CurrencyCode element.

Steps

  1. In the element list, un-comment the AnnualSalary element and the CurrencyCode element.

    1. Select both code rows and press Ctrl + Shift + < to remove the comment signs from the beginning of each row.

  2. Add the mandatory element annotation that identifies the AnnualSalary element as an amount and links it to theCurrencyCode element.

    Note

    You might need to add this annotation manually with the support of code completion. At the time of writing, the editor for data definitions does not offer a quick fix for this error. (The editor for database table definitions does.)
    1. Adjust the code as follows:

      Code Snippet
      Copy code
      Switch to dark mode
      123456
      department_id as DepartmentId, @Semantics.amount.currencyCode: 'CurrencyCode' annual_salary as AnnualSalary, currency_code as CurrencyCode,
  3. Open the Active Annotations View and analyze the values of the EndUserText.Label annotation for the different view elements.

    1. Right-click anywhere in the source code of the data definition and choose Open WithActive Annotations.

      Result

      The Active Annotations view appears as a new tab below the editor view. The source of the end user texts is listed in the Origin Data Type column.
  4. Return to the source code of your data definition and add element annotation EndUSerText.label for view element CurrencyCode. Change the label from Currency to Currency Key.

    1. Adjust the code as follows:

      Code Snippet
      Copy code
      Switch to dark mode
      123456
      @Semantics.amount.currencyCode: 'CurrencyCode' annual_salary as AnnualSalary, @EndUserText.label: 'Currency Key' currency_code as CurrencyCode,
  5. Activate the data definition.

    1. Press Ctrl + F3 to activate the development object.

  6. Return to the Active Annotations view, refresh the display, and confirm that now the new label is active.

    Hint

    To find a specific annotation more easily, you can first collapse the entire tree and then expand the element and main annotation you are interested in.
    1. From the toolbar on the Active Annotations view, choose Refresh (F5). Alternatively, place the cursor anywhere on the view and press F5.

    2. From the toolbar on the Active Annotations view, choose Collapse All.

    3. Expand Z##_R_EMPLOYEECurrencyCode@EndUserText.

Log in to track your progress & complete quizzes