Mapping CDS Views as Data Source

Objective

After completing this lesson, you will be able to operate the CDS/SADL generator

CDS View Data Model Import

Development flow: Operating the CDS/SADL generator

The CDS/SADL Generator is operated in the SAP Gateway Service Builder (SEGW). The first step is to define the data model by importing the SQL view as DDIC structure. The second step is to generate the service implementation by mapping the CDS view as CDS/SADL.

Caution

Since ABAP Platform 7.55, SAP recommends using CDS view entities instead of CDS DDIC-based views due to technical improvements, such as performance at activation and so on. A CDS view entity does not have an SQL view anymore, which could be imported. Other DDIC structures may be used or the entity type may be defined declaratively.
Development steps: CDS/SADL generator

The starting point is a project in the SEGW. First the SQL view of the CDS view is imported as DDIC structure. This also creates the entity set, which can then be mapped to the CDS view as a whole. Generating the runtime artifacts includes generating the source code for all read operations based on SADL. After registering the service, these read operations can be tested.

Write operations can be implemented by mapping the CUD-methods to function modules or implementing them directly. But this is then separated from mapping CDS views.

Screenshot flow about importing SQL view as DDIC structure in SEGW

Importing an SQL view as DDIC structure works the same as importing other structures. You choose ImportDDIC Structure on the Data Model node of the SAP Gateway project and enter the SQL view in the ABAP Structure field. After that, you select the fields and the key(s).

Hint

SQL views always have the MANDT field. But in the majority of cases, the OData service should work cross-client. To achieve this, the MANDT field must not be selected.
Screenshot flow about how to find the right SQL view in SE80

If you do not have an ADT available to find the right SQL view fitting to the CDS data definition, which should be mapped, you can also use the SE80. Since AS ABAP 7.50, SQL views provide the Data Definition field for this. Since AS ABAP 7.51, you can also view the source code of the data definition in the SE80.

In the code, you can identify the SQL view by searching for the @AbapCatalog.sqlViewName annotation. You cannot navigate directly to the SQL view in the SE80 like in ADT, but you can enter the name of the SQL view in the Object Selection popup.

Note

In AS ABAP 7.40, the report RUTDDLSSHOW allowed to search for CDS source code by name and code fragment. It was replaced by the improvements of the SE80 above.

CDS View Code Mapping

Screenshot flow about how to map a CDS view as data source in SEGW

A CDS view is mapped as a whole to an entity set implementation. Therefore right-click the entity set under Service Implementation and choose Map to Data Source. The only available type for the mapping is Business Entity. The value help for the Name field supports in building the correct string for mapping a CDS data definition (CDS~<data definition>). In the value help, select CDS Core Data Services as SADL Model Type and enter the name of the data definition as SADL Model.

Note

When introducing SADL for SAP Gateway, there were several model types planned as source. But CDS rapidly outruns all other model types.

Mapping of a CDS data source to a business entity results in the generation of the READ and QUERY method in the DPC class.

Screenshot flow about how to map properties and associations to a CDS view in SEGW

In the mapping process, you can use the Generate Mapping button or drag & drop to map the business entity elements to the properties of the entity type of the OData service. The mapping of the business entity associations to the navigation properties of an entity type can be done equally after choosing Associations at the top. The same applies to Actions if the business entity provides these.

Hint

If you map an association to a business entity, the SEGW assumes that the target of the association should also be mapped to a suitable business entity. So first finish all mappings before checking for consistency.

Create an SAP Gateway Service by Mapping CDS Views

Business Example

You are a developer or solution architect in your company. You have been asked to import and map CDS views to read and query operations for two entity sets of an SAP Gateway service and also map the association between the two.

Template:
None
Solution:
GW100_S_CDS (SAP Gateway Project)

Note

This exercise requires an SAP Learning system. Login information is provided by your system setup guide.

Note

Whenever the values or object names in this exercise include ##, replace ## with the number of your user.

Prerequisites

The packages were created in exercise Define a Data Model Based on a Data Dictionary Structure .

Task 1: Create an SAP Gateway Project for Mapping a CDS View

Steps

  1. In the SAP Gateway Service Builder of your S4H, create a new project named ZGW100_##_CDS in your ZGW100_##_BES package.

    1. In the SAP Easy Access of your S4H, search for SAP Gateway Service Builder or start transaction SEGW.

    2. In the SAP Gateway Service Builder, choose Create Project.

    3. In the Create Project popup, enter the following values:

      FieldValue
      ProjectZGW100_##_CDS
      Description<your choice>
      PackageZGW100_##_BES
    4. Choose Continue.

    5. Choose Save.

    6. In the Prompt for transportable workbench request popup, in the Request field, select the transport request provided to you.

    7. Choose Continue.

Task 2: Define the Entities Supplier and Article

Steps

  1. Import the data dictionary structure GW100CSUPPLIER as entity Supplier, selecting all fields except MANDT. Set BUSINESSPARTNERUUID as key.

    1. In the SAP Gateway Service Builder of your S4H, right-click the Data Model node and choose ImportDDIC Structure.

    2. In the Import from DDIC Structure popup, in the Name field, enter Supplier.

    3. In the ABAP Structure field, enter GW100CSUPPLIER.

    4. Choose Next.

    5. Select the GW100CSUPPLIER checkbox.

    6. Deselect the MANDT checkbox.

    7. Choose Next.

    8. In the BUSINESSPARTNERUUID field, select the Is Key checkbox.

    9. Choose Finish.

    10. Choose Save.

  2. Import the data dictionary structure GW100CARTICLE as entity Article, selecting all fields except MANDT. Set PRODUCTUUID as key.

    1. Right click-the Data Model node and choose ImportDDIC Structure.

    2. In the Import from DDIC Structure popup, in the Name field, enter Article.

    3. In the ABAP Structure field, enter GW100CARTICLE.

    4. Choose Next.

    5. Select the GW100CARTICLE checkbox.

    6. Deselect the MANDT checkbox.

    7. Choose Next.

    8. In the PRODUCTUUID field, select the Is Key checkbox.

    9. Choose Finish.

    10. Choose Save.

Task 3: Model a Navigation between Supplier and Article

Steps

  1. Model navigation Supplier_Articles between the Supplier and Article entities, connecting one Businesspartneruuid to many Supplieruuid. Create the navigation properties Article and Supplier in the according entity types.

    1. In the SAP Gateway Service Builder of your S4H, expand the Data Model node.

    2. Right-click the Associations node and choose Create.

    3. In the Create Association popup, in the Association Name field, enter Supplier_Articles.

    4. For the Principal Entity, enter the following values:

      FieldValue
      Entity Type NameSupplier
      Cardinality1
      Navigation PropertyArticle
    5. For the Dependent Entity, select the Create related Navigation Property checkbox.

    6. For the Dependent Entity, enter the following values:

      FieldValue
      Entity Type NameArticle
      Cardinality0..n
      Navigation PropertySupplier
    7. Choose Next.

    8. In the Dependent Property field, enter Supplieruuid by using the value help.

    9. Choose Next.

    10. Choose Finish.

    11. Choose Save.

Task 4: Map Read and Query Operations to CDS Data Definitions

Steps

  1. In the SAP Gateway Service Builder of your S4H, map the read and the query operations of entity set SupplierSet to the CDS view GW100_C_Supplier using the association _ARTICLES for navigation.

    1. In the SAP Gateway Service Builder of your S4H, expand the Service Implementation node.

    2. Right-click the SupplierSet node and choose Map to Data Source.

    3. In the Map to Data Source popup, in the Type dropdown, select Business Entity.

    4. In the Name field, use the value help.

    5. In the Select Business Entity popup, enter the following values:

      FieldValue
      SADL Model TypeCDS Core Data Services
      SADL ModelGW100_C_Supplier
    6. Choose Continue.

      Note

      The Data Source Attribute Name field should now contain the value CDS~GW100_C_Supplier.
    7. Choose Continue.

    8. Choose Generate Mapping.

    9. Choose Associations.

    10. Choose Generate Mapping.

    11. Choose Save.

  2. Map the read and the query operations of entity set ArticleSet to the CDS view GW100_C_Article using the association _SUPPLIER for navigation.

    1. Right-click the ArticleSet node and choose Map to Data Source.

    2. In the Map to Data Source popup, in the Type field, enter Business Entity.

    3. In the Name field, use the value help.

    4. In the Select Business Entity popup, enter the following values:

      FieldValue
      SADL Model TypeCDS Core Data Services
      SADL ModelGW100_C_Article
    5. Choose Continue.

      Note

      The Name for the Data Source Attribute field should now contain the value CDS~GW100_C_Article.
    6. Choose Continue.

    7. Choose Generate Mapping.

    8. Choose Properties.

    9. Choose Generate Mapping.

    10. Choose Save.

Task 5: Generate and Register a CDS-based SAP Gateway Service

Steps

  1. In the SAP Gateway Service Builder of your S4H, generate the runtime objects and add them to your package ZGW100_##_BES.

    1. In the SAP Gateway Service Builder of your S4H, choose Generate Runtime Objects.

    2. In the Model and Service Definition popup, to accept the proposed names, choose Continue.

    3. In the Package field, enter ZGW100_##_BES.

    4. Choose Save.

    5. In the Prompt for transportable workbench request popup, in the Request field, select the transport request provided to you.

    6. Choose Continue.

  2. Register and activate the service in your S4H using your package ZGW100_##_FES.

    1. Double-click the Service Maintenance node.

    2. Right-click the LOCAL_GW system.

    3. Select Register.

    4. In the Select System Alias (Hub to Backend) popup, in the System Alias field, enter LOCAL.

    5. Choose Continue.

    6. In the Add Service popup, in the Package Assignment field, enter ZGW100_##_FES.

    7. Choose Continue.

    8. For every popup, choose the transport request provided to you.

    9. Choose Save.

Task 6: Test the Query and Read Operations of Supplier and Article

Steps

  1. In the SAP Gateway Client of your S4H, test the paging operation for suppliers in your service. Query the first two suppliers.

    1. Choose SAP Gateway Client.

    2. In the SAP Gateway Client, select HTTPS as Protocol.

    3. Choose Entity Set.

    4. In the Entity Sets popup, double-click SupplierSet.

    5. Choose Add URI Option.

    6. In the Add URI Option popup, double-click $top=2.

      Example

      /sap/opu/odata/SAP/ZGW100_##_CDS_SRV/SupplierSet?$top=2
    7. Choose Execute.

      Result

      The HTTP Response displays the first two suppliers.
  2. Test the sorting operation for suppliers in your service. Query suppliers sorted by their company name ascending.

    1. In the Request URI field, add &$orderby=Companyname asc to the URI.

      Example

      /sap/opu/odata/SAP/ZGW100_##_CDS_SRV/SupplierSet?$top=2&$orderby=Companyname asc
    2. Choose Execute.

      Result

      The HTTP Response displays the first two suppliers ordered by company name ascending.
  3. Test the filter operation for suppliers in your service. Query the suppliers whose e-mail address starts with "supplier".

    1. In the Request URI field, add $filter=startswith(Emailaddress, 'supplier') to the URI.

      Example

      /sap/opu/odata/SAP/ZGW100_##_CDS_SRV/SupplierSet?$top=2&$orderby=Companyname asc&$filter=startswith(Emailaddress, 'supplier')
    2. Choose Execute.

      Result

      The HTTP Response displays the first two suppliers ordered by company name ascending whose e-mail address starts with "supplier".
  4. Test the read operation for suppliers in your service. Read a single supplier.

    1. In the Request URI field, replace all query options by the key of a supplier with guid in front.

      Example

      /sap/opu/odata/SAP/ZGW100_##_CDS_SRV/SupplierSet(guid'00505604-4e85-1edd-b9b4-a81d353342c0')

      Hint

      Copy the self link of a supplier in the HTTP Response.
    2. Choose Execute.

      Result

      The HTTP Response displays the selected supplier.
  5. Test the navigation between suppliers and articles in your service. Query the articles of a supplier.

    Hint

    If no articles are found for a supplier, make sure to select a supplier with an e-mail address starting with "supplier".
    1. In the Request URI field, add /Article to the URI.

      Example

      /sap/opu/odata/SAP/ZGW100_##_CDS_SRV/SupplierSet(guid'00505604-4e85-1edd-b9b4-a81d353342c0')/Article
    2. Choose Execute.

      Result

      The HTTP Response displays the articles of the supplier.
  6. Test the expand operation for suppliers and articles in your service. Read a supplier including its articles.

    1. In the Request URI field, replace the / in front of Article with ?$expand=

      Example

      /sap/opu/odata/SAP/ZGW100_##_CDS_SRV/SupplierSet(guid'00505604-4e85-1edd-b9b4-a81d353342c0')?$expand=Article
    2. Choose Execute.

      Result

      The HTTP Response displays the supplier including its articles.

Task 7: Document the Query Options Generated via SADL in the Metadata

Steps

  1. In the SAP Gateway Service Builder of your S4H, set entity sets SupplierSet and ArticleSet to be Pageable and set all properties of entity types Supplier and Article to be Filterable and Sortable.

    1. In the SAP Gateway Service Builder of your S4H, expand Data Model.

    2. Double-click Entity Sets.

    3. For the SupplierSet and ArticleSet, set the Pageable checkbox.

    4. Expand Entity TypesSupplier.

    5. Double-click Properties.

    6. For all properties, set the Filterable and Sortable checkbox.

    7. Expand Article.

    8. Double-click Properties.

    9. For all properties, set the Filterable and Sortable checkbox.

    10. Choose Generate Runtime Objects.

Log in to track your progress & complete quizzes