Skip to Content

RFC: Test the Business API (BAPI) with An ABAP Console App

Requires Customer/Partner License
Test that the connection to the BAPI works (A BAPI is a type of RFC).
You will learn
  • How to call a BAPI using RFC in an ABAP class
  • How to test this class by outputting the data to the ABAP Console
julieplummer20Julie PlummerApril 10, 2024
Created by
julieplummer20
March 9, 2023
Contributors
julieplummer20

Prerequisites

The class:

  1. Connects to the backend system, such as an S/4HANA on-premise system.
  2. Calls the BAPI remotely via RFC.
  3. Reads the data from the back end into a local table.
  4. Outputs that local table to the ABAP Console.

In future, we hope to provide a helper class that generates the appropriate DDL source code for a custom entity and associated type definition - for any BAPI in your backend system. In the meantime, I have provided all the necessary code to use with BAPI_EPM_PRODUCT_GET_LIST and BAPI_EPM_PRODUCT_GET_DETAIL.

Throughout this tutorial, replace ### or 000 with your initials or group number.

  • Step 1
    1. As before, in the Package Explorer, select your package and choose New > ABAP Class from the context menu.

    2. Enter the following for your class, then choose Next.

      • Name: ZCL_OUTPUT_TEST_000
      • Description for your class, e.g. Test RFC BTP to on-premise
      • Interface: if_oo_adt_classrun. This enables you to run the class in the console.

    Remember to change 000 to your group number.

    1. Choose the transport request, then choose Finish.

    The class is displayed in a new editor.

  • Step 2

    First you need to define the table type for the local table that will be filled with the data:

    ABAP
    Copy

    TYPES : BEGIN OF ty_bapi_epm_product_header, productid TYPE c LENGTH 10, typecode TYPE c LENGTH 2, category TYPE c LENGTH 40, name TYPE c LENGTH 255, description TYPE c LENGTH 255, supplierid TYPE c LENGTH 10, suppliername TYPE c LENGTH 80, taxtarifcode TYPE int1, measureunit TYPE c LENGTH 3, weightmeasure TYPE p LENGTH 7 DECIMALS 3, weightunit TYPE c LENGTH 3, price TYPE p LENGTH 12 DECIMALS 4, currencycode TYPE c LENGTH 5, width TYPE p LENGTH 7 DECIMALS 3, depth TYPE p LENGTH 7 DECIMALS 3, height TYPE p LENGTH 7 DECIMALS 3, dimunit TYPE c LENGTH 3, productpicurl TYPE c LENGTH 255, END OF ty_bapi_epm_product_header.
  • Step 3

    Since you are trying to connect to a remote system, you need to catch any exceptions that occur.

    ABAP
    Copy

    TRY. CATCH cx_root INTO DATA(lx_root). out->write( lx_root->get_longtext( ) ). ENDTRY.
  • Step 4

    Define the connection, replacing 000 in each artifact.

    ABAP
    Copy

    DATA(lo_destination) = cl_rfc_destination_provider=>create_by_comm_arrangement( comm_scenario = 'Z_OUTBOUND_RFC_000_CSCEN' " Communication scenario service_id = 'Z_OUTBOUND_RFC_000_SRFC' " Outbound service ). DATA(lv_destination) = lo_destination->get_destination_name( ).
  • Step 5

    Define the variables for the local internal table and structure for the data; and a character-type variable for messages:

    ABAP
    Copy

    DATA lt_product TYPE STANDARD TABLE OF ty_bapi_epm_product_header. DATA ls_product TYPE ty_bapi_epm_product_header. DATA msg TYPE c LENGTH 255.
  • Step 6

    Finally, call the remote BAPI. The exception handling is mandatory to avoid serious system errors:

    ABAP
    Copy

    CALL FUNCTION 'BAPI_EPM_PRODUCT_GET_LIST' DESTINATION lv_destination * EXPORTING * max_rows = 25 TABLES headerdata = lt_product EXCEPTIONS system_failure = 1 MESSAGE msg communication_failure = 2 MESSAGE msg OTHERS = 3.
  • Step 7
    1. Handle any exceptions that occur using a CASE statement.

      ABAP
      Copy
      CASE sy-subrc.
          WHEN 0.
            LOOP AT lt_product INTO ls_product.
              out->write( ls_product-name && ls_product-price && ls_product-currencycode ).
            ENDLOOP.
          WHEN 1.
            out->write( |EXCEPTION SYSTEM_FAILURE | && msg ).
          WHEN 2.
            out->write( |EXCEPTION COMMUNICATION_FAILURE | && msg ).
          WHEN 3.
            out->write( |EXCEPTION OTHERS| ).
      ENDCASE.
      
      
    2. Save and activate the class

  • Step 8
    ABAP
    Copy

    CLASS ZCL_OUTPUT_TEST_000 DEFINITION PUBLIC FINAL CREATE PUBLIC . PUBLIC SECTION. INTERFACES if_oo_adt_classrun. PROTECTED SECTION. PRIVATE SECTION. ENDCLASS. CLASS ZCL_OUTPUT_TEST_000 IMPLEMENTATION. METHOD if_oo_adt_classrun~main. " ABAP source code for type definition for BAPI_EPM_PRODUCT_HEADER " generated on: ... TYPES : BEGIN OF ty_bapi_epm_product_header, productid TYPE c LENGTH 10, typecode TYPE c LENGTH 2, category TYPE c LENGTH 40, name TYPE c LENGTH 255, description TYPE c LENGTH 255, supplierid TYPE c LENGTH 10, suppliername TYPE c LENGTH 80, taxtarifcode TYPE int1, measureunit TYPE c LENGTH 3, weightmeasure TYPE p LENGTH 7 DECIMALS 3, weightunit TYPE c LENGTH 3, price TYPE p LENGTH 12 DECIMALS 4, currencycode TYPE c LENGTH 5, width TYPE p LENGTH 7 DECIMALS 3, depth TYPE p LENGTH 7 DECIMALS 3, height TYPE p LENGTH 7 DECIMALS 3, dimunit TYPE c LENGTH 3, productpicurl TYPE c LENGTH 255, END OF ty_bapi_epm_product_header. TRY. DATA(lo_destination) = cl_rfc_destination_provider=>create_by_comm_arrangement( comm_scenario = 'Z_OUTBOUND_RFC_000_CSCEN' " Communication scenario service_id = 'Z_OUTBOUND_RFC_000_SRFC' " Outbound service comm_system_id = 'Z_OUTBOUND_RFC_CSYS' " Communication system ). DATA(lv_destination) = lo_destination->get_destination_name( ). "variables needed to call BAPI DATA lt_product TYPE STANDARD TABLE OF ty_bapi_epm_product_header. DATA ls_product TYPE ty_bapi_epm_product_header. DATA msg TYPE c LENGTH 255. "Exception handling is mandatory to avoid dumps CALL FUNCTION 'BAPI_EPM_PRODUCT_GET_LIST' DESTINATION lv_destination EXPORTING max_rows = 25 TABLES headerdata = lt_product EXCEPTIONS system_failure = 1 MESSAGE msg communication_failure = 2 MESSAGE msg OTHERS = 3. CASE sy-subrc. WHEN 0. LOOP AT lt_product INTO ls_product. out->write( ls_product-name && ls_product-price && ls_product-currencycode ). ENDLOOP. WHEN 1. out->write( |EXCEPTION SYSTEM_FAILURE | && msg ). WHEN 2. out->write( |EXCEPTION COMMUNICATION_FAILURE | && msg ). WHEN 3. out->write( |EXCEPTION OTHERS| ). ENDCASE. CATCH cx_root INTO DATA(lx_root). out->write( lx_root->get_longtext( ) ). ENDTRY. ENDMETHOD. ENDCLASS.
  • Step 9

    Test the class in the ABAP Console by choosing F9.
    Your output should look like this:

    Image depicting test-rfc-console
  • Step 10

    Which of the following is code snippets is correct? Choose only one answer.

  • Step 11

    Note: If your on-premise system is down, or you have made an error configuring the connectivity, you will get a misleading message

    EXCEPTION COMMUNICATION_FAILURE
    Error when opening an RFC connection
    (User is locked; notify the person responsible).
    
    

    Check the following :

    • Audits in the Cloud Connector (after a simulated RFC call): Should be empty
    • Trace in the Cloud Connector (after a simulated RFC call): Should be empty
    • Function Module is listed as an accessible resource in Cloud Connector
    • Internal Host is reachable in Cloud Connector
    • Correct NEO Subaccount is configured in Cloud Connector
    • Destination Service Communication Arrangement SAP_COM_0276 is configured
    • Communication Arrangement SAP_COM_0200 is configured

Back to top