ABAP on Cloud Development Part – 4

11 hours ago 2

ABAP on Cloud Development

Welcome to Part 4 of our “ABAP on Cloud Development” series! In the previous parts, we explored the foundational concepts and practical implementation strategies for developing ABAP-based applications in the SAP BTP environment. As we move forward, this article dives into the next crucial topic—Table Functions, a powerful feature in the ABAP RESTful Application Programming Model (RAP) that enables custom logic execution through CDS views..
Table functions are particularly useful when you need to model complex logic or retrieve data from non-standard sources that cannot be represented through regular CDS views alone. They allow the execution of ABAP-managed logic via AMDP (ABAP Managed Database Procedures), providing flexibility and performance in scenarios where native SQL logic is needed..
In this article, we will focus solely on the coding aspects of table functions. You’ll see how to define a table function using CDS, implement its logic in AMDP, and consume it in your application.

Table Functions :

A Table Function in ABAP is a special kind of CDS entity that delegates its data retrieval logic to an AMDP (ABAP Managed Database Procedure) method written in ABAP. Unlike standard CDS views that directly access database tables or views, table functions offer more flexibility and performance when dealing with:

  • Complex logic that’s hard to express in CDS
  • Data sourced from multiple systems or external APIs
  • Scenarios requiring dynamic data processing
  • Temporary or runtime-only data generation

In RAP-based applications, table functions serve as the data source for CDS views and can be cleanly integrated into your BO (Business Object) model if needed.

Here’s what you typically define:

  1. A CDS table function interface, specifying the output structure.
  2. An AMDP implementation class, where the actual logic resides.

In the next section, we’ll jump directly into the code—defining the CDS table function, writing the AMDP method, and explaining each part step by step.

⚠️ Reminder:
Always append your own ID as a suffix to object names, even if the sample code doesn’t explicitly show it. This ensures your custom objects don’t conflict with others in the same environment that you are practicing.

Requirement: To show all Material Characteristics in a Single Row

@AbapCatalog.viewEnhancementCategory: [#NONE]
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: ‘CDS Example without AMDP’
@Metadata.ignorePropagatedAnnotations: true
@ObjectModel.usageType:{
serviceQuality: #X,
sizeCategory: #S,
}
define view entity zaoc_amdp1_cds_example_ as select from inob
inner join ausp
on ausp.objek = inob.cuobj {
key ausp.objek,
key ausp.atinn,
key ausp.atzhl,
ausp.atwrt,
inob.objek as Material
}

NEW TRAINING ANNOUCEMENT

Enrolment Link SAP BTP Overview and Administration
Day wise break upSAP BTP Overview and Administration
Course Days – 26, 27 July, 2, 3, 9, 10, 16, 17, 23 & 24 Aug 2025 IST Dates
Time – 7:30 AM to 10 AM IST

Before we proceed with our tutorial, we would like to give you an opportunity to join our ZAPYard’s learning community where we have more than 35 groups and more than 1850 real SAP Consultants interacting with each other daily. Only SAP topics and not BS. Else, they will be banned from the community without warning. 👇👇👇👇

If you want to be part of ZAPYard’s Discussion Community, please feel free to check the below Link. We Ask, Answer, Help and Learn Together. There are more than 35 groups from different topics like Generative AI, SAP Joule, CAPM, BTP, RAP, BPT, Fiori, iRPA, CAI, CPI, PI/PO, ABAP on HANA, SAPUI5, SAP Build, SAP Adobe Forms, ChatBots, SAC etc. Join any group of your interest and interact with our Community.

Join ZAPYard’s WhatsApp Community – Ask, Answer, Propose & Defend SAP Topics

Now the above view will return Material data in multiple rows showing all characteristics.
But the requirement is to show all Material Characteristics in one row.

@ClientHandling.type: #CLIENT_DEPENDENT
@AccessControl.authorizationCheck:#NOT_ALLOWED
define table function ZAOC_AMDP1_TF
returns
{

client :s_mandt;
material: matnr;
characteristics: atwrt;
}
implemented by method
ZCL_AOC_AMDP1=>GET_MATERIAL_FOR_CDS;
CLASS zcl_aoc_amdp1 DEFINITION
PUBLIC
FINAL
CREATE PUBLIC .
PUBLIC SECTION.
INTERFACES if_amdp_marker_hdb .
CLASS-METHODS get_material_for_cds
FOR TABLE FUNCTION ZAOC_AMDP1_TF
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.
CLASS zcl_aoc_amdp1 IMPLEMENTATION.
METHOD get_material_for_cds
BY DATABASE FUNCTION FOR HDB
LANGUAGE SQLSCRIPT
OPTIONS READ-ONLY
USING inob ausp.
–init MANDT variable
DECLARE lv_CLNT “$ABAP.type( MANDT )” := session_context(‘CLIENT’);
rank_table = SELECT distinct ausp.mandt as client,
inob.objek as material,
ausp.atwrt as temp
FROM ausp
INNER JOIN inob ON ausp.mandt = inob.mandt AND
ausp.objek = inob.cuobj
where ausp.mandt = :lv_CLNT;
RETURN SELECT client,
material,
STRING_AGG(temp ORDER by temp) as characteristics
FROM :rank_Table
group BY client, material;
ENDMETHOD.
ENDCLASS.

Second Example: To show the Accounting Transaction data along with Vendor and Customer Details.

@EndUserText.label: ‘ZCDS_TABLE_FUNC’
@ClientHandling.type: #CLIENT_DEPENDENT
define table function Zaoc_amdp2_tf
returns {
CLIENT : abap.clnt; RBUKRS : abap.char( 4 );
BELNR : belnr_d;
GJAHR : gjahr;
BUZEI : buzei;
BUDAT : budat;
BLART : blart_d;
RACCT : racct;
RACCT_TEXT : abap.char( 50 );
HSL : wertv12;
OSL : wertv12;
LIFNR : lifnr;
KUNNR : kunnr;
VENDOR_TEXT : abap.char( 50 );
CUST_TEXT : abap.char( 50 );
}
implemented by method ZCL_AOC_AMDP2=>GET_DATA;

CLASS zcl_aoc_amdp2 DEFINITION
PUBLIC
FINAL
CREATE PUBLIC .
PUBLIC SECTION.

INTERFACES if_amdp_marker_hdb .
TYPES:
BEGIN OF TY_ACDOCA,
RBUKRS TYPE ACDOCA-RBUKRS,
BELNR TYPE ACDOCA-BELNR,
GJAHR TYPE ACDOCA-GJAHR,
BUZEI TYPE ACDOCA-BUZEI,
BUDAT TYPE ACDOCA-BUDAT,
BLART TYPE ACDOCA-BLART,
RACCT TYPE ACDOCA-RACCT,
RACCT_TEXT TYPE SKAT-TXT50,
HSL TYPE ACDOCA-HSL,
OSL TYPE ACDOCA-OSL,
LIFNR TYPE ACDOCA-LIFNR,
KUNNR TYPE ACDOCA-KUNNR,
VENDOR_TEXT TYPE TXT50,
CUST_TEXT TYPE TXT50,
END OF TY_ACDOCA,
TT_ACDOCA TYPE STANDARD TABLE OF TY_ACDOCA,

BEGIN OF TY_VENDOR,
LIFNR TYPE BUT000-PARTNER,
TEXT TYPE TXT50,
END OF TY_VENDOR,
TT_VENDOR TYPE STANDARD TABLE OF TY_VENDOR,
BEGIN OF TY_CUST,
KUNNR TYPE BUT000-PARTNER,
TEXT TYPE TXT50,
END OF TY_CUST,
TT_CUST TYPE STANDARD TABLE OF TY_CUST.

CLASS-METHODS:
GET_DATA FOR TABLE FUNCTION ZAOC_AMDP2_TF.
PROTECTED SECTION.
PRIVATE SECTION.
class-METHODS:
GET_DATA_FROM_VENDOR EXPORTING VALUE(EV_VENDOR) TYPE TT_VENDOR,
GET_DATA_FROM_CUST EXPORTING VALUE(EV_CUST) TYPE TT_CUST,
GET_DATA_FROM_ACDOCA
EXPORTING VALUE(EV_ACDOCA) TYPE TT_ACDOCA.

ENDCLASS.

CLASS zcl_aoc_amdp2 IMPLEMENTATION.
METHOD GET_DATA_FROM_ACDOCA BY DATABASE PROCEDURE FOR HDB
LANGUAGE SQLSCRIPT
OPTIONS READ-ONLY
USING ACDOCA SKAT.
–init MANDT variable
DECLARE lv_CLNT “$ABAP.type( MANDT )” := session_context(‘CLIENT’);

–init SPRAS variable
DECLARE lv_langu “$ABAP.type( SPRAS )” := ‘E

–GET DATA FROM SOURCE
EV_ACDOCA =
SELECT T1.RBUKRS,
T1.BELNR,
T1.GJAHR,
T1.BUZEI,
T1.BUDAT,
T1.blart,
LTRIM( T1.RACCT,” ) AS RACCT,
T2.TXT50 AS RACCT_TEXT,
T1.HSL,
T1.OSL,
T1.LIFNR,
T1.KUNNR,
” AS VENDOR_TEXT,
” AS CUST_TEXT
FROM ACDOCA AS T1
INNER JOIN SKAT AS T2
ON T1.RCLNT = T2.MANDT
AND T1.rclnt = :lv_clnt
AND T2.MANDT = :LV_CLNT
AND T1.RACCT = T2.saknr
AND T2.SPRAS = :LV_LANGU
AND T1.KTOPL = T2.KTOPL;
** –USING APPLY FILTER TO TABLE
** EV_ACDOCA = APPLY_FILTER( :EV_ACDOCA, :IV_STRING );
ENDMETHOD.
“”””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””

“”””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””
METHOD GET_DATA_FROM_CUST BY DATABASE PROCEDURE FOR HDB LANGUAGE SQLSCRIPT
OPTIONS READ-ONLY
USING KNA1.
–init MANDT variable
DECLARE lv_CLNT “$ABAP.type( MANDT )” := session_context(‘CLIENT’);

EV_CUST = SELECT T1.KUNNR,
T1.NAME1 AS TEXT
FROM KNA1 AS T1
WHERE T1.MANDT = :lv_CLNT;
ENDMETHOD.

“”””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””

“”””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””
METHOD GET_DATA_FROM_VENDOR BY DATABASE PROCEDURE FOR HDB LANGUAGE SQLSCRIPT
OPTIONS READ-ONLY
USING LFA1.

–init MANDT variable
DECLARE lv_CLNT “$ABAP.type( MANDT )” := session_context(‘CLIENT’);

EV_VENDOR = SELECT T1.LIFNR,
T1.NAME1 AS TEXT
FROM LFA1 AS T1
WHERE T1.MANDT = :lv_CLNT;

ENDMETHOD.

“”””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””

“”””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””
METHOD GET_DATA BY DATABASE FUNCTION FOR HDB LANGUAGE SQLSCRIPT
OPTIONS READ-ONLY
USING ZCL_AOC_AMDP2=>GET_DATA_FROM_VENDOR
ZCL_AOC_AMDP2=>GET_DATA_FROM_CUST
ZCL_AOC_AMDP2=>GET_DATA_FROM_ACDOCA.

–init MANDT variable
DECLARE lv_CLNT “$ABAP.type( MANDT )” := session_context(‘CLIENT’);

–GET DATA FROM VENDOR MASTER DATA
CALL “ZCL_AOC_AMDP2=>GET_DATA_FROM_VENDOR”( EV_VENDOR => :EV_VENDOR );

–GET DATA FROM CUSTOMER MASTER DATA
CALL “ZCL_AOC_AMDP2=>GET_DATA_FROM_CUST”( EV_CUST => :EV_CUST );

–GET DATA FROM FI TRANSACTION DATA
CALL “ZCL_AOC_AMDP2=>GET_DATA_FROM_ACDOCA”(
EV_ACDOCA => :EV_ACDOCA
);

–RETURN VALUE
RETURN SELECT :lv_CLNT AS CLIENT,
T1.RBUKRS,
T1.BELNR,
T1.GJAHR,
T1.BUZEI,
T1.BUDAT,
T1.BLART,
T1.RACCT,
T1.RACCT_TEXT,
T1.HSL,
T1.OSL,
T1.LIFNR,
T1.KUNNR,
T3.TEXT AS VENDOR_TEXT,
T2.TEXT AS CUST_TEXT
FROM :EV_ACDOCA AS T1
LEFT JOIN :EV_CUST AS T2
ON T2.KUNNR = T1.KUNNR
LEFT JOIN :EV_VENDOR AS T3
ON T3.LIFNR = T1.LIFNR
ORDER BY T1.RBUKRS,
T1.BELNR,
T1.GJAHR;
ENDMETHOD.
ENDCLASS.

If you found this article helpful, we’d love to hear from you! Share your thoughts, questions, or experiences in the comments below—your feedback is always welcome and helps us craft even more valuable content for your ABAP on Cloud journey. Stay tuned for the next part in the series, where we’ll continue exploring essential tools and techniques to level up your SAP development skills.

Please follow our LinkedIn PageLinkedIn Group , Facebook PageFacebook GroupTwitter & Instagram.

Do not forget to SUBSCRIBE to our YouTube Channel for Free Courses and Unconventional Interesting Videos.

Do join ZAPYard’s Learning Community.

Read Entire Article