Skip to Content
Technical Articles
Author's profile photo Chau Nguyen Phuc

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

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:

@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

@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

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

 

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!!!

Assigned Tags

      4 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Roberto F. S.
      Roberto F. S.

      Hi Chau Nguyen Phuc,

      Thanks for sharing this scenario and you structured the information very clear.

      Roberto

      Author's profile photo Chau Nguyen Phuc
      Chau Nguyen Phuc
      Blog Post Author

      Thanks so much!

      Author's profile photo Du Tran Phuoc
      Du Tran Phuoc

      Hi Chau Nguyen Phuc

      Great! it very clear.

      Thanks for your sharing

      Author's profile photo Christian Hannemann
      Christian Hannemann

      Hi Chau,

      thanks for sharing this great article!

      I was just a little bit confused because you can use in SQL script the field MANDT, when it's not declared in the CDS view. But it's working fine!