8

Consume CDS View inside CDS Table Function by using AMDP

 1 year ago
source link: https://blogs.sap.com/2022/12/03/consume-cds-view-inside-cds-table-function-by-using-amdp/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
December 3, 2022 2 minute read

Consume CDS View inside CDS Table Function by using AMDP

0 1 190

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

demo_000.png

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

A little bit CDS View and AMDP. If you don’t have any experience, please follow link:

https://blogs.sap.com/2020/08/25/first-program-with-amdp-method/

Solution

Step 1: Create CDS View:

demo_001.png
@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

demo_002.png
@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

demo_003.png
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

demo_004.png

That’s right, we have successfully used CDS view inside a CDS View table function, I hope it can help you!!!

If this blog is good, please like and share it. If you don’t understand something, please leave a comment in the article.

Thanks so much!!!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK