Saturday, October 26, 2024

Steps to create SAP CDS View with some examples

1. Set Up Your Development Environment

  • Eclipse with ADT: Ensure you have Eclipse installed with the ABAP Development Tools (ADT) plugin. This provides the necessary tools for CDS view development.
  • ABAP System Connection: Establish a connection to your ABAP system (e.g., SAP S/4HANA or SAP ERP) from Eclipse.

2. Create an ABAP Project

  • In Eclipse, go to File -> New -> ABAP Project.
  • Provide your ABAP system connection details and choose a suitable project name.

3. Create a New CDS View

  • In your ABAP project, right-click on your package and select New -> Other ABAP Repository Object.
  • In the wizard, search for Data Definition under Core Data Services and click Next.
  • Enter a name for your CDS view (e.g., Z_MY_SALES_DATA) and click Finish.

4. Define the CDS View Source Code

This is where you define the structure and data source of your CDS view using a SQL-like syntax. Here's a basic example:

@AbapCatalog.sqlViewName: 'Z_MY_SALES_DATA'  define view ZMySalesData as select from vbap {     key vbeln,    posnr,    matnr,    kwmeng   }  
  • @AbapCatalog.sqlViewName: This annotation specifies the technical name of the view in the ABAP Dictionary.
  • define view: This keyword starts the view definition.
  • ZMySalesData: This is the name of your CDS view.
  • select from vbap: This specifies the data source (table vbap in this case).
  • key vbeln, posnr: These are the key fields of the view.
  • matnr, kwmeng: These are additional fields you want to include in the view.

5. Add More Complex Logic (Optional)

  • Joins: You can join multiple tables using join clauses.
    define view ZMySalesData as   select from vbap as p   inner join vbak as h on p.vbeln = h.vbeln {    // fields from vbap and vbak  }  
  • Associations: Define relationships between entities using @ObjectModel.association.
    @ObjectModel.association: [      {         from: 'VBAP',        to: 'VBAK',        on: 'VBAP.VBELN = VBAK.VBELN'      }    ]  
  • Calculated Fields: Create new fields based on existing ones using expressions.
    net_amount as (netwr * 1.19) // Example calculation  
  • Filters: Use where clauses to filter data.
    where erdat > '20240101'   
  • Annotations: Use annotations to control various aspects of the view, such as:
    • UI Annotations: (@UI.lineItem, @UI.text) for labels and formatting.
    • OData Annotations: (@OData.publish) for OData service generation.
    • Search Annotations: (@Search.searchable) for enabling search capabilities.

6. Activate the CDS View

  • Save your CDS view source code.
  • Right-click on the view in the Project Explorer and select Activate.

7. Preview the Data

  • After activation, you can right-click on the view and select Open Data Preview to see the data that your CDS view retrieves.

Example with Joins and Annotations:

@AbapCatalog.sqlViewName: 'Z_SALES_ORDER_DETAILS'  define view ZSalesOrderDetails     as select from vbap as p     inner join vbak as h on p.vbeln = h.vbeln {      key p.vbeln,      p.posnr,      @UI.lineItem: { position: 10 }      p.matnr as Material,      @UI.lineItem: { position: 20 }      p.kwmeng as Quantity,      h.auart as OrderType    }  

This example joins the vbap (sales order items) and vbak (sales order header) tables, selects specific fields, and adds @UI.lineItem annotations for better display in UIs.

Key Considerations:

  • Naming Conventions: Use clear and consistent naming for your views and elements.
  • Data Types: Choose appropriate data types for fields.
  • Performance: Think about performance implications when joining tables or using complex expressions.
  • Security: Consider authorization and data privacy when designing your views.
  • Documentation: Add comments to your code to explain the purpose and logic of your CDS view.

No comments:

Post a Comment

Fiori Development - Style

Okay, here is a rewritten version incorporating the detailed information about developing preformatted layout reports, including a Table of ...