July 2023 - Sap 4 All

Latest

Wednesday, 19 July 2023

 Introduction:

In today's digital age, businesses increasingly require real-time access to integrated data from various sources. SAP ABAP offers powerful tools like Core Data Services (CDS) and Function Modules to address this need. In this blog, we'll explore a real-time example of how to create a CDS table function that utilizes an ABAP function module with parameters to retrieve and enrich data from an external system, all without using ABAP Managed Database Procedures (AMDP).

Scenario: Employee Details Enrichment Consider an SAP system managing employee data with basic attributes such as Employee ID, First Name, Last Name, and Department. To provide a more comprehensive view of employees, we want to enrich this data with additional information such as Manager Name and Contact Number, which resides in an external HR system.

Solution Approach: To achieve this, we'll follow these steps:

Step 1: Create an ABAP Function Module Develop an ABAP function module, say Z_ENRICH_EMPLOYEE_DATA, that accepts employee_id as an input parameter and returns the manager_name and contact_number as output parameters. This function module will fetch the required details from the external HR system using web service calls or any other integration method.

Function Module Logic:

abap
FUNCTION Z_ENRICH_EMPLOYEE_DATA. *"---------------------------------------------------------------------- *"*"Local Interface: *" IMPORTING *" VALUE(IV_EMPLOYEE_ID) TYPE STRING *" EXPORTING *" VALUE(EV_MANAGER_NAME) TYPE STRING *" VALUE(EV_CONTACT_NUMBER) TYPE STRING *"---------------------------------------------------------------------- DATA: lt_employee_data TYPE TABLE OF zemployee_data, ls_employee_data TYPE zemployee_data. " Call external HR system or web service to fetch data for employee_id " In this example, we'll assume data is fetched from a custom table. SELECT manager_name contact_number INTO CORRESPONDING FIELDS OF TABLE lt_employee_data FROM zexternal_hr_data WHERE employee_id = iv_employee_id. " In case the data is found for the employee_id IF lines( lt_employee_data ) > 0. READ TABLE lt_employee_data INTO ls_employee_data INDEX 1. ev_manager_name = ls_employee_data-manager_name. ev_contact_number = ls_employee_data-contact_number. ENDIF. ENDFUNCTION.

Step 2: Register Function Module as RFC-enabled Ensure that the function module Z_ENRICH_EMPLOYEE_DATA is registered as Remote-Enabled (RFC-enabled) to allow external access.

Step 3: Create a CDS Table Function Develop a CDS table function named Z_ENRICHED_EMPLOYEE_DATA with parameters client and employee_id. The output structure should include fields for client, employee_id, manager_name, and contact_number.

CDS Table Function Logic:

abap
@AbapCatalog.sqlViewName: 'Z_ENRICHED_EMPLOYEE_DATA' @AccessControl #CHECK define table function Z_Enriched_Employee_Data with parameters @DefaultClientHandling: #CLIENT client : abap.clnt, employee_id : char8 as select from dummy as d { client, employee_id, manager_name, contact_number };

Step 4: Implement CDS View Logic In the CDS view definition, use Open SQL expressions to call the function module Z_ENRICH_EMPLOYEE_DATA with the provided employee_id and retrieve the Manager Name and Contact Number. The CDS view should return the enriched employee data.

CDS View Logic:

abap
@AbapCatalog.sqlViewName: 'Z_ENRICHED_EMPLOYEE_DATA' @AccessControl #CHECK define view Z_Enriched_Employee_Data as select from dummy as d left outer join @FunctionName: 'Z_ENRICH_EMPLOYEE_DATA' @Environment.systemField: #CLIENT client, @Environment.systemField: #CLIENT employee_id, manager_name, contact_number from dummy as d;

Step 5: Activate and Test the CDS View Activate the CDS table function and view. Test the CDS view by running a query in the ABAP Development Tools or SAP GUI, passing different employee_id values to validate the real-time data enrichment.

Conclusion: By leveraging CDS table functions and ABAP function modules, businesses can seamlessly enrich their existing data with real-time information from external systems. This powerful integration approach allows for a unified view of employee data, supporting better decision-making and more efficient HR processes.

Benefits:

  • Real-time Data Enrichment: Retrieve up-to-date information from external systems without compromising system performance.
  • Simplified Development: Achieve data integration and enrichment with a straightforward approach, avoiding the complexities of AMDP.
  • Reusability: CDS table functions can be reused across various applications and scenarios, enhancing code maintainability.

Closing Remarks: In this blog, we explored a real-time example of using CDS table functions with ABAP function modules to enrich employee data. This integration approach empowers businesses to access and utilize data seamlessly, facilitating better insights, improved decision-making, and enhanced efficiency in HR processes.

 Let's consider a real-time example where we want to create a CDS view that retrieves material data from an external system using an RFC-enabled function module. The function module receives a material number as input and returns the corresponding material description and quantity from the external system.

  1. Create an ABAP Function Module:

    • Create an ABAP function module named Z_GET_MATERIAL_DATA that accepts a material number as input and returns the material description and quantity from the external system.
  2. Register Function Module as RFC-enabled:

    • Ensure that the function module Z_GET_MATERIAL_DATA is registered as RFC-enabled by setting the corresponding attributes in the function module's properties.
  3. Create CDS View Consuming Function Module:

    • Create a CDS view named Z_Material_Data_View that consumes the data from the function module Z_GET_MATERIAL_DATA.
abap
@AbapCatalog.sqlViewName: 'Z_MATERIAL_DATA_VIEW' @AccessControl #CHECK define view Z_Material_Data_View as select from dummy as d left outer join @FunctionName: 'Z_GET_MATERIAL_DATA' @Environment.systemField: #CLIENT client, @DefaultLanguage: 'EN' @Search.defaultSearchElement: true material, materialDescription, quantity from dummy as d;

In this example, the CDS view Z_Material_Data_View uses an Open SQL expression with the @FunctionName annotation to call the RFC-enabled function module Z_GET_MATERIAL_DATA. The input parameter, material, will be passed from the CDS view to the function module. The result of the function module will be joined with the dummy table to form the output of the CDS view.

  1. Activate and Test the CDS View:
    • Activate the CDS view Z_Material_Data_View and test it by running a query against the CDS view in the ABAP Development Tools or SAP GUI.

When you run a query against the CDS view, it will internally call the function module Z_GET_MATERIAL_DATA with the specified material number as input, retrieve the material description and quantity from the external system, and present the result as output from the CDS view.

Please note that the exact implementation of the function module and CDS view may vary based on your specific business requirements and the data structure from the external system. Additionally, it's crucial to ensure that proper authorization and error handling mechanisms are in place while calling the RFC-enabled function module.

 In SAP ABAP, you can create a CDS (Core Data Services) table function without using AMDP (ABAP Managed Database Procedures) by following these steps:

  1. Open the ABAP Development Tools (ADT) or the SAP GUI ABAP Editor (SE38).

  2. Create a new or select an existing ABAP package where you want to create the CDS table function.

  3. Inside the ABAP package, create a new CDS view.

  4. Define the CDS view as a table function by adding the @AccessControl annotation with the value #CHECK to the CDS view.

  5. Define the input parameters and output structure of the table function. These parameters will determine the structure of the table to be returned.

  6. In the CDS view definition, write the logic to populate the table.

Here's an example of a simple CDS table function that returns a list of material numbers and their descriptions:

abap
@AbapCatalog.sqlViewName: 'Z_MATERIALS_DESC' @AccessControl #CHECK define table function z_get_material_list with parameters @Environment.systemField: #CLIENT client : abap.clnt, @Environment.systemField: #LANGUAGE language : abap.lang, @DefaultLanguage: 'EN' @Search.defaultSearchElement: true @Consumption.valueHelpDefinition: 'Material' material : matnr as select from mara as m join makt as t on m.matnr = t.matnr where t.spras = :language;

In this example, we define a CDS table function z_get_material_list with input parameters client, language, and material. The output structure will be determined by the fields selected in the SELECT statement.

After creating and activating the CDS table function, you can use it in ABAP CDS views or other CDS table functions. The CDS table function will return the list of material numbers and their descriptions based on the provided input parameters.