SAP BDL FUNCTION ABEXA



Get Example source ABAP code based on a different SAP table
  



ABAP_RAP - function
This example demonstrates how a function is defined, implemented, and consumed in a managed RAP BO.
Data model
The CDS data model consists of the root entity DEMO_CDS_FUNCTION_1 , which represents a purchase order.
Root entity:
DDLS DEMO_CDS_FUNCTION_1
Behavior definition
The RAP behavior definition DEMO_CDS_FUNCTION_1 is defined in RAP BDL as follows:
BDEF DEMO_CDS_FUNCTION_1
Function definition
Three functions are defined:
getDetails: instance function that reads the values of the fields PurchaseDocument, Price, and Status for the specified entity instances and returns them in the result structure.
calculateTotal: static function that sums up the prices of all entity instances with status 'O' and returns the total in its result structure. ABAP_NOTE This function uses a DDIC data element as output parameter. Note that the recommended procedure is to use only CDS abstract entities as BDEF parameters.
calculateDiscount: function with an input parameter for a discount percentage. It subtracts the discount in percent from the initial price of an entity instance and returns the reduced price in its result structure.
All three functions merely deliver information in their result structure . No modify operation is carried out and no data is changed on the database.
Behavior implementation
For the above RAP behavior definition, one ABAP behavior pool (ABP) is created. It can be accessed under BP_DEMO_CDS_FUNCTION_1========CCIMP.

ABAP_SOURCE_CODE
DEMO CL_DEMO_CDS_FUNCTION

ABAP_DESCRIPTION
Access with ABAP using EML
The above source code uses EML to access the RAP business object from an ABAP class and executes all three functions:
First, the persistent database table demo_purch_doc is filled with values in the method fill_table using ABAP SQL. This way, BO instances already exist and do not have to be created using EML.
The function getDetails is executed for two entity instances. These entity instances are determined via the values of their key fields. The function reads the values of the fields PurchaseDocument , Price, and Status for the specified entity instances and returns them in its result structure. The values from the result structure are appended to the internal structure instance_function and a screen output is generated.
The function calculateTotal is a static function and thus applies to the complete BO, not to any specific entity instance. The function first filters all entity instances that have the status 'O' (in this example, all entity instances have the status 'O). Then, it sums up the values of the field Price of all entity instances with status 'O'. The values from the result structure are appended to the internal structure static_function and a screen output is generated.
The function calculateDiscount is an instance function with an input parameter. To execute the function, it is required to specify an entity instance and also a value for the input parameter. In this example, the input parameter represents a discount percentage and the function subtracts the discount from the value of the field Price of an entity instance. It returns the reduced price in its result structure. The values from the result structure are appended to the internal structure function_input_param and a screen output is generated.