SAP Home Learn Build Integrate Model Operate Extend with AI ConnectTutorial navigator Knowledge Graph API Devtoberfest Developer Advocates App Space

Manage my Account SAP Devs YouTube ↗ Learnings ↗ Community ↗ Provide Feedback ↗
Logout
โคข Open full site

Enhance an ABAP Core Data Services (CDS) View in SAP Business Technology Platform (BTP) ABAP Environment

Add features like value help, aggregations, and calculated fields to an existing CDS entity

Overview

🎓 beginner 60 min. ABAP DevelopmentBeginnerSAP BTP ABAP EnvironmentSAP Business Technology Platform

You will learn

  • โœ”How to add value help using a filter
  • โœ”How to add textual information using associations
  • โœ”How to concatenate two elements, using a built-in function for CDS
  • โœ”How to convert currencies using a built-in function for CDS
  • โœ”How to create an object page
  • โœ”How to create a parent-child hierarchy in your business object
  • โœ”How to add a CASE statement
Julie Plummer J Julie Plummer November 14, 2024
Created by March 9, 2023
Contributors

Prerequisites

Steps

Intro

You can then use some of these features in productive development to make your applications more powerful and more user-friendly. By the end of this tutorial, your application should include a list page and an object page, which should look like this:

fep-final-enhance-no-CASE
fep-final-enhance-no-CASE

step13c-fep-CASE
step13c-fep-CASE

You can see the code at the end of this tutorial. Throughout the tutorial, objects name include a suffix, such as 000. Always replace this with your group number or initials.


Step 1 Add value help
โ€”

To make the input fields more useful, you will now add input value help to the field AgencyID.

  1. Specify the source of the value help. This works a bit like a join: You need to point to an entity, and field common to both the value help entity and your CDS entity. In this case, you will point to AgencyID in the CDS entity /DMO/I_Agency. Add the following annotation to your field AgencyID.

    CDS
    @Consumption.valueHelpDefinition: [{  entity: {name: '/DMO/I_Agency', element: 'AgencyID'}  }]
  • You also need to expose this second entity in the OData service. To do this, add the entity /DMO/I_Agency to your service definition, so the complete definition looks like this:

    CDS
    @EndUserText.label: 'Service exposes Travel Data 000'
    define service Z_EXPOSE_TRAVEL_R_000 {
      expose Z_I_TRAVEL_R_000 as Travel;
      expose /DMO/I_Agency as Agency;
    }
  • Format, save, and activate both CDS entity and service definition ( Shift+F1, Ctrl+S, Ctrl+3 ).

  • Refresh your Fiori Elements preview and choose value help for the input field AgencyID.

    step13a-choose-value-help
    step13a-choose-value-help

  • Now the value help appears. Enter the country key DE and choose Go. Only German agencies appear on the list.

    step13b-german-agencies
    step13b-german-agencies

  • Repeat this step for CustomerID:

    CDS
    @Consumption.valueHelpDefinition: [{entity: {name: '/DMO/I_Customer', element: 'CustomerID' }}]
  • Also, expose this entity in the service definition:

    CDS
    expose /DMO/I_Customer as Customer;
  • Step 2 Add text associations
    +
  • Check that the association has been added to the field list. (This should have been done automatically when you inserted the signature of /DMO/I_TRAVEL_U.)

    step14b-association-added
    step14b-association-added

  • Format, save, and activate ( Shift+F1, Ctrl+S, Ctrl+F3 ).

  • Make sure that the entity /DMO/I_Agency has been added to your service definition. (You should have done this in step 1.)

    step2a-sd-added
    step2a-sd-added

  • Refresh your Fiori Elements preview. The agency name should now be shown, with the ID number in parentheses.

    step14c-fep-agency-by-name
    step14c-fep-agency-by-name

  • Repeat for the field CustomerID:

    CDS
    association [1..1] to /DMO/I_Customer as _Customer    on $projection.CustomerID =   _Customer.CustomerID
  • CDS
    @ObjectModel.text.association: '_Customer'
  • Also make sure this association are added to the list of fields:

    CDS
    //Associations
    _Customer

    step2b-associations-added
    step2b-associations-added

  • Step 3 Merge two fields
    +

    step15a-concat
    step15a-concat

  • Also make sure this association is added to the list of fields:

    CDS
    //Associations
    _Customer
  • Format, save, and activate these objects using Shift+F1, Ctrl+S, Ctrl+F3 .

  • Check the result in the data preview, by clicking in the editor and choosing Open With > Data Preview from the context menu. The result should look like this.

    step3-settings-addressee
    step3-settings-addressee

    step15b-concat-result
    step15b-concat-result

  • Now you have the addressee, you may want to comment out the text association for CustomerID.

  • Step 4 Add currency conversion
    +
  • Optional: Test in the Fiori Elements preview. Your app should look like this:

    step16b-convert-to-dollars
    step16b-convert-to-dollars

  • Step 5 Check code for CDS entity
    +
    Step 6 Test yourself
    +
    Step 7 Create object page
    +
  • Add a similar annotation for each of the other elements; see step 7.5 below for complete code:

  • Finally, add a header to your object page, just after the layer annotation (before the annotate view... statement):

    CDS
    @UI: {
            headerInfo: {
            typeName: 'Travel',
            typeNamePlural: 'Travels',
            title: {
                type: #STANDARD,
                label: 'Travel',
                value: 'TravelID'
            }
        }
    }
  • Your complete code should look like this:

    CDS
    @Metadata.layer: #CORE
    
    @UI: {
        headerInfo: {
            typeName: 'Travel',
            typeNamePlural: 'Travels',
            title: {
            type: #STANDARD,
            label: 'Travel',
            value: 'TravelID'
            }
        }
    }
    
    annotate view Z_I_TRAVEL_R_000 with
    {
    
    @UI.facet: [ { id:              'Travel',
                    purpose:         #STANDARD,
                    type:            #IDENTIFICATION_REFERENCE,
                    label:           'Travel',
                    position:        10 } ]
    
    
    @UI           : {
            lineItem      : [{position: 15, label: 'Agency', importance: #HIGH}],
            identification: [{position: 15 }],
            selectionField: [{position: 15 }]
            }
    AgencyID;
    
    @UI           : {
            lineItem      : [{position: 20, importance: #HIGH}],
            identification: [{position: 20 }],
            selectionField: [{position: 20 }]
            }
    CustomerID;
    
    @UI           : {
            lineItem      : [{position: 10, importance: #HIGH}],
            identification: [{position: 10, label: 'Travel' }],
            selectionField: [{position: 10 }]
            }
    TravelID;
    
    @UI           : {
            lineItem      : [{position: 30, importance: #HIGH}],
            identification: [{position: 30 }],
            selectionField: [{position: 30 }]
            }
    BeginDate;
    
    @UI           : {
            lineItem      : [{position: 40, importance: #HIGH}],
            identification: [{position: 40 }],
            selectionField: [{position: 40 }]
            }
    EndDate;
    
    @UI           : {
            lineItem      : [{position: 50, importance: #HIGH}],
            identification: [{position: 50 }]
            }
    TotalPrice;
    
    @UI           : {
            lineItem      : [{position: 50, importance: #HIGH}]
            }
    Memo;
    
    @UI           : {
        lineItem      : [{position: 60, importance: #HIGH}],
        selectionField: [{position: 60 }]
        }
    Status;
    
    }
  • Test your object page in the Fiori Elements preview. It should look roughly like this:

    step6b-fep-preview-object-page
    step6b-fep-preview-object-page

  • Step 8 Create CDS entity for booking
    +
    Step 9 Specify hierarchy
    +
  • Now, choose Activate All (Shift+Ctrl+F3).

    step10a-activate-all-toolbar
    step10a-activate-all-toolbar

  • Choose Deselect All, then choose your two entities Z_I_TRAVEL_R_000 and Z_I_BOOKING_000, then choose Activate.

    step10-activate-all
    step10-activate-all

  • Change the @AccessControl.authorizationCheck: to #NOT_REQUIRED.

  • Add an alias to the define view statement, so that your code looks like this:

    CDS
    @AccessControl.authorizationCheck: #CHECK
    @EndUserText.label: 'Read-only view from /DMO/I_BOOKING_U'
    define view entity Z_I_BOOKING_000 as select from /DMO/I_Booking_U as Booking
  • Format, save, and activate the CDS entity (Shift+F1, Ctrl+S, Ctrl+F3) Z_I_BOOKING_000.

  • Step 10 Add annotations
    +
    Step 11 Create metadata extension for Z_I_BOOKING_000
    +
    Step 12 Add association from Travel to Booking
    +
    Step 13 Add Booking information to object page
    +
  • Test your new facet in the Fiori Elements preview. It should look like this:

    step9b-fep-booking-facet
    step9b-fep-booking-facet

  • Step 14 Add association from Booking to Connection
    +
  • Now add the following two elements to the CDS entity Z_I_BOOKING_R_000:

    CDS
    @Semantics.quantity.unitOfMeasure: 'DistanceUnit'
    _Connection.Distance as Distance,
    
    @Semantics.unitOfMeasure: true
    _Connection.DistanceUnit as DistanceUnit,
  • Optional: Test your Fiori Elements preview again. It should look like this:

    step12a-fep-plus-connection
    step12a-fep-plus-connection

  • Step 15 Add CASE statement
    +

    step13b-add-where-clause
    step13b-add-where-clause

  • Format, save, and activate the CDS entity.

  • Test your Fiori Elements preview again. It should roughly look like this:

    step13c-fep-CASE
    step13c-fep-CASE

  • Step 16 More Information
    +

    Resources

    Discussion

    Share feedback on this tutorial or join the conversation in SAP Community.

    Submit detailed feedback Discuss in Community
    Steps
    Step 1 of 16
    1. Add value help 2. Add text associations 3. Merge two fields 4. Add currency conversion 5. Check code for CDS entity 6. Test yourself 7. Create object page 8. Create CDS entity for booking 9. Specify hierarchy 10. Add annotations 11. Create metadata extension for Z_I_BOOKING_000 12. Add association from Travel to Booking 13. Add Booking information to object page 14. Add association from Booking to Connection 15. Add CASE statement 16. More Information

    Learn more →