Wednesday, 7 December 2022

Consume CDS View inside CDS Table Function by using AMDP

We are familiar with getting data from a table using AMDP procedure or AMDP table function. But, how about CDS View, I tried a case and got the following error

SAP HANA Exam, SAP HANA Exam Prep, SAP HANA Tutorial and Materials, SAP HANA Skills, SAP HANA Jobs, SAP HANA Guides

Basically, AMDP will get data from database, and it will get all client (assume your system have several clients), that is a reason why you catch this error when working with CDS view.

So, how to resolve it? Please read this article with me.

Prerequsites:

ABAP 7.55 or newer for using view entity. In case of using CDS DDIC View, don’t worry it

Solution

Step 1: Create CDS View:

SAP HANA Exam, SAP HANA Exam Prep, SAP HANA Tutorial and Materials, SAP HANA Skills, SAP HANA Jobs, SAP HANA Guides

@AbapCatalog.viewEnhancementCategory: [#NONE]
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'Demo CDS View'
@Metadata.ignorePropagatedAnnotations: true
@ObjectModel.usageType:{
    serviceQuality: #X,
    sizeCategory: #S,
    dataClass: #MIXED
}

define view entity zchau_acdoca as select from I_JournalEntryItem {
    key CompanyCode             as BUKRS,
    key AccountingDocument      as BELNR,
    key FiscalYear              as GJAHR,
    key LedgerGLLineItem        as DOCLN,
        GLAccount               as RACCT,
        CostCenter              as RCNTR,
        ProfitCenter            as PRCTR 
}
where SourceLedger = '0L' and Ledger = '0L'

Step 2: Create CDS Table function

SAP HANA Exam, SAP HANA Exam Prep, SAP HANA Tutorial and Materials, SAP HANA Skills, SAP HANA Jobs, SAP HANA Guides

@EndUserText.label: 'Demo CDS Table function'
@ClientHandling.type: #CLIENT_DEPENDENT
@ClientHandling.algorithm: #SESSION_VARIABLE
define table function zchau_cds_001 with parameters
    @Environment.systemField: #CLIENT
    p_date: mandt
returns {
  
  key CLIENT     : abap.clnt;
  key RBUKRS     : bukrs;
  key DOC_NUM    : belnr_d;
  key GJAHR      : gjahr;
  key DOCLN      : docln6;
      RACCT      : racct;
      RCNTR      : kostl;
      PRCTR      : prctr;
}
implemented by method ZCL_DEMO_AMDP=>GET_ACDOCA;

We need declare Client by adding annotation to this CDS View, If you don’t do it, you can’t create AMDP to consume CDS View. These annotations are very important

Step 3: Create Class to consume CDS View

We will create Global Class and handle method to get data from CDS View which was created in step 1

SAP HANA Exam, SAP HANA Exam Prep, SAP HANA Tutorial and Materials, SAP HANA Skills, SAP HANA Jobs, SAP HANA Guides

CLASS zcl_demo_amdp DEFINITION
  PUBLIC
  FINAL
  CREATE PUBLIC .

  PUBLIC SECTION.
    INTERFACES if_amdp_marker_hdb .
    CLASS-METHODS:
      get_acdoca     FOR TABLE FUNCTION zchau_cds_001.
  PROTECTED SECTION.
  PRIVATE SECTION.
ENDCLASS.

CLASS zcl_demo_amdp IMPLEMENTATION.
  METHOD get_acdoca BY DATABASE FUNCTION FOR HDB LANGUAGE SQLSCRIPT OPTIONS READ-ONLY USING zchau_acdoca.
    RETURN SELECT _raw.mandt as client,
            _raw.bukrs as rbukrs,
            _raw.belnr as doc_num,
            _raw.gjahr as gjahr,
            _raw.docln as docln,
            _raw.RACCT as RACCT,
            _raw.RCNTR as RCNTR,
            _raw.PRCTR as PRCTR
     from zchau_acdoca as _raw;
  endmethod.

ENDCLASS.

Result:

Run CDS Table Function and you will see data is shown

SAP HANA Exam, SAP HANA Exam Prep, SAP HANA Tutorial and Materials, SAP HANA Skills, SAP HANA Jobs, SAP HANA Guides

That’s right, we have successfully used CDS view inside a CDS View table function.

Source: sap.com

No comments:

Post a Comment