Skip to Content
Previous

ABAP Development: Create a CDS View

By D028018

You will learn how to use the Core Data Services (CDS) tools in ABAP in Eclipse.

You will learn

In the following exercise you will learn how to use the new Core Data Services (CDS) tools in ABAP in Eclipse. CDS is an extension of the ABAP Dictionary that allows you to define semantically rich data models in the database and to use these data models in your ABAP programs. CDS is a central part of enabling Code Push-down in ABAP applications.
You will learn to define a new CDS view in a DDL source. You will also learn about some of the CDS enhanced view-building capabilities such as associations and annotations.
You can find more information about CDS in the SAP Help Portal (coming soon) and the SAP Community Network (coming soon).

Details


Step 1: Choosing ABAP Object

In the context menu of your package choose New and then choose Other ABAP Repository Object.

Image depicting step1-newObject
Please log in to access this content.
Step 2: Selecting Data Definition

Select Data Definition, then choose Next.

Image depicting step2-DataDef
Please log in to access this content.
Step 3: Entering values

Enter the following, then choose Next:
a. Name = Z_INVOICE_ITEMS
b. Description = Invoice Items

Image depicting step3-enterValues
Please log in to access this content.
Step 4: Accepting the transport request

Accept the default transport request (local) by simply choosing Next again.

Image depicting step4-AcceptTransportRequest

Please log in to access this content.
Step 5: Defining View

Select the entry Define View, then choose Finish

Image depicting step5-defineView
Please log in to access this content.
Step 6: Entering the data source

The new view appears in an editor. In this editor, enter the following values:
• enter ZINVOICEITEMS as the SQL view name.
• enter the CDS view sepm_sddl_so_invoice_item as the data source for your view.
Use code completion (keyboard shortcut CTRL+SPACE) to get proposals for the data source.

Image depicting step6-enterDataSourceEtc

Note: The SQL view name is the internal/technical name of the view which will be created in the database. Z_Invoice_Items is the name of the CDS view which provides enhanced view-building capabilities in ABAP. You should always use the CDS view name in your ABAP applications.
[DONE]

Step 7: Editing the SELECT statement

You will now insert the fields currency_code and gross_amount into the SELECT list as follows:
• Trigger code completion in the SELECT list (by clicking on the SELECT list and using keyboard shortcut CTRL+SPACE), then double click on the entry Insert all elements - template. All the elements (fields and associations) of the underlying data source are inserted into the SELECT list.
• Remove all the elements in the SELECT list which were inserted by the code completion apart from currency_code and gross_amount. Remember to separate the elements in the SELECT statement with a comma.

Image depicting step7-InsertAllImage depicting step7-SelectStatement1

-crop

Please log in to access this content.
Step 8: Using CDS associations

You will now use some CDS associations.
CDS associations model the relationships between data sources. You can use associations in path expressions to access elements (fields and associations) in related data sources without specifying JOIN conditions. You can now display the element info by positioning the cursor on the data source name sepm_sddl_so_invoice_item and choosing F2.
• To see the related data sources that can be accessed using associations, scroll down.
• To see details about the target data source of the association header, choose the hyperlink sepm_sddl_so_invoice_header

Image depicting step8-CdsAssociations
Please log in to access this content.
Step 9: Adding Fields

You will now add fields of related data sources to the SELECT list of Z_Invoice_Items, using the associations in path expressions. Each element in the path expression must be separated by a period.
• Add the company_name of the business partner to the SELECT list using the associations header and buyer in a path expression
• Add the payment_status from the invoice header to the SELECT list using the association header

Image depicting step9-AddFields
Please log in to access this content.
Step 10: Adding a CASE statement

If the invoice has been paid, you want to set the payment_status to X (true). Do this by implementing a CASE statement

case header.payment_status
    when 'P' then 'X'
    else ' '
end as payment_status
Image depicting step10-CaseStatement
Please log in to access this content.
Step 11: Adding a CAST statement

Cast the type of the transformed payment_status to the data element you created - zso_invoice_payment_status - by using a CAST statement:

cast(
    case header.payment_status
        when 'P' then 'X'
        else ' '
    end
as zso_invoice_payment_status )

as payment_status
Image depicting step11-CAST

Note: You can open the online help for the CAST statement by positioning the cursor on the cast keyword and choosing F1

Please log in to access this content.
Step 12: Adding a WHERE clause

You will now filter the results so that only invoice items with currency_code = 'EUR' are retrieved. Do this by adding a WHERE clause:

WHERE currency_code = 'EUR'
Image depicting step12-WHERE
Please log in to access this content.
Step 12: Adding a WHERE clause

Save the data definition by choosing Save (Ctrl+S).

Please log in to access this content.

Finally, activate the data definition by choosing Activate (Ctrl+F3).

Image depicting step14-saveAndActivate
Please log in to access this content.

[ACCORDION-END]

Next Steps

  • Use a CDS View in ALV with IDA (coming soon).

Updated 04/10/2017

Time to Complete

15-20 Min.

Beginner

Tags

Prerequisites

  • ABAP Development: Create a global ABAP Class and Data Dictionary structure for data retrieval (coming soon).

Next Steps

  • Use a CDS View in ALV with IDA.
Next
Back to top