Skip to Content

Connect to Your On-Premise System from SAP BTP, ABAP Environment Using RFC

Requires Customer/Partner License
Call a remote function module located in an on-premise system, such as a SAP S/4HANA System, from the ABAP Environment
You will learn
  • How to create a secure RFC connection between your SAP BTP, ABAP Environment and an on-premise SAP System, e.g. SAP S/4HANA
  • How to test the connection using an ABAP handler class. This class will retrieve system information using a Remote Function Call (RFC), RFC_GET_SYSTEM_INFO
julieplummer20Julie PlummerJuly 16, 2025
Created by
julieplummer20
March 9, 2023
Contributors
julieplummer20

Prerequisites

This tutorial mission was written for SAP BTP ABAP Environment. However, you should also be able to use it in SAP S/4HANA Cloud Environment in the same way.

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

This tutorial is part of a mission, Get Data from an On-Premise System Using a Remote Function Call - RFC. Make sure you have fulfilled the prerequisites in the previous tutorial and that you follow each tutorial in order.

Introduction

To learn more about this scenario, see the tutorial: Get Data from a Remote System Using a Remote Function Call (RFC): Scenario

  • Step 1

    First, you need to connect your ABAP on-premise system to your BTP sub-account by means of SAP Cloud Connector.

    1. In your browser, log on to SAP Cloud Connector:

      • Address = e.g. https://localhost:<port> (Default = 8443)
      • User = Administrator
      • Initial password = manage (You will change this when you first log in)
    2. Your configuration should look like this.

      step1b-scc-location-id

    > The Location ID is optional. It enables you to connect multiple Cloud Connectors to a subaccount.

  • Step 2
    1. In the menu in the left pane, expand the sub-account and choose Cloud To On-Premise > Access Control.

    2. Note down the Virtual Host, e.g. s4h_rfc:sapgw<instance> as here. This represents an external hostname, so that you can hide the internal hostname from the outside world. You will need this external hostname and port later, when creating a destination from SAP BTP cockpit.

    3. Check that Result = Reachable. If not, check that you chose the correct port, or whether an internal firewall is preventing communication.

      step2a-scc-virtual-host
    4. Add the following resources:

      • RFC_GET_SYSTEM_INFO (as exact name)
      • BAPI_EPM (as prefix)
      step2b-scc-resources
  • Step 3

    In the SAP BTP cockpit of your Cloud Foundry sub-account, choose Cloud Connectors:

    step3a-btp-cockpit-cloud-connectors

    The Virtual host points to the on-Premise connection mapped in SAP Cloud Connector. If used, The Location ID points to the correct SAP Cloud Connector (located in the on-Premise system). Later, in your Communication System in Fiori Launchpad, you will use these values, as and Target Host and (optionally) SCC Location ID respectively.

  • Step 4

    Now, in SAP BTP ABAP Environment, you need to create the necessary ABAP artifacts in ABAP Development Tools (ADT), starting with a package.

    1. In ABAP Development Tools (ADT), select the ABAP Cloud Project and choose New > ABAP Package from the context menu.

    2. Enter the following and choose Next:

      • Name = Z_OUTBOUND_RFC_###
      • Description = Get product data from S/4HANA using RFC
      • Package type = Development
      step3a-create-package
    3. Choose Create new transport request, enter a description, such as Get product data from S/4HANA using RFC, then choose Finish.

      step1b-transport-request
  • Step 5

    Next, you will establish outbound communication from the BTP instance to the S/4HANA instance.

    1. Select your ABAP package and choose New > Other Repository Object from the context menu; then enter Outbound Service, then choose Next.

      step2a-new-repo-object



      step2a-new-outbound-service

    2. Enter the following and choose Next.

      • Outbound service: Z_OUTBOUND_RFC_###. The SRFC suffix will be added automatically.
      • Description: Get product data from S/4HANA using RFC
      • Service type: RFC Service
      step2b-outbound-service-name
    3. Choose the transport request you just created, then choose Finish.

    The outbound service appears. Optional: Add the relevant RFC, RFC_GET_SYSTEM_INFO, then choose Save from the main toolbar.


  • Step 6
    1. Still in ADT, choose Cloud Communication Management > New > Communication Scenario from the context menu, then choose Next.

      step3a-new-comm-scen
    2. Enter the following and choose Next.

      • Name: Z_OUTBOUND_RFC_###_CSCEN
      • Description: Comm Scen: Call API from S/4HANA using RFC
      step3b-new-comm-scen-name
    3. Choose the transport request you just created, then choose Finish.

      The communication scenario appears in a new editor. Ensure the following have been entered:

      • Communication Scenario Type: Customer Managed
      • Allowed Instances: One instance per scenario and communication system
    4. On the Outbound tab, add your service by choosing Add.

    5. Choose Browse, select your outbound service Z_OUTBOUND_RFC_###_SRFC, then choose Finish. The service type RFC is entered automatically.

    6. Choose Finish. Your communication scenario should look like this.

      Make sure that Supported Authentication Methods > Basic is ticked, since you will use this method later.

      step3d-comm-scen-finished
    7. Leave the other default settings, choose Save, then choose Publish locally.

  • Step 7

    Now, you need to create the necessary communication artifacts in Fiori Launchpad (of the BTP instance), starting with the communication system.
    This artifact specifies the URL of the API (minus the HTTP(S) protocol) and port.

    1. In the Fiori Launchpad home page, choose Communication Systems, then choose New.

      step4a-comm-system

      step4b-new-comm-system
    2. Enter the following and choose Create.

      • System ID: Z_OUTBOUND_RFC_CSYS_###
      • System Name: Z_OUTBOUND_RFC_CSYS_###
    3. In General and below, enter:

      Field Name Value
      Host name the host name of your ABAP on-premise system, found in the URL of that system’s Fiori Launchpad, e.g.: myhost.s4hana.ondemand.com
      Port Default = 443
      Destination Off
      Cloud Connector On
      SCC Location ID: SAPDEV
    4. In RFC Settings, enter:

      Load Balancing Off
      Target Host s4h_rfc
      Client Client of your ABAP on-premise system, also found in the URL e.g. 100
      Instance Number Default = 00
      step4c-comm-system-settings
    5. Under Users for Outbound Communication, enter the following, then choose Close:

      • Authentication method: User Name and Password (basic authentication)
      • User name: Your user name from the on-premise system
      • Password: Your password from the on-premise system
      step4d-comm-user-btp-s4h-cu-000
    6. Leave the other default settings as they are and choose Save.

  • Step 8

    Next, you will create communication arrangement, pointing to the communication scenario and communication system.

    1. Choose Communication Arrangements, then choose New.

      step4a-comm-management-tiles
    2. Choose your communication scenario, Z_OUTBOUND_RFC_###_CSCEN. This name is also entered automatically for the communication arrangement. You can accept this default.

      step4a-new-comm-arr
      step4b-select-comm-scenario
    3. Choose the communication system you created, Z_OUTBOUND_RFC_CSYS_###.
      The other details, e.g. Communication User, RFC Outbound Service, and RFC Function Module are entered automatically.

    4. Check the connection.

  • Step 9
    1. In ABAP Development Tools (ADT), in Project Explorer, open your ABAP Environment instance.
      For more information, see Set Up SAP BTP, ABAP Environment and create Your First Console Application.

    2. In the project, create a new ABAP package.

    3. Create a new ABAP class: Choose File > New > Other… > ABAP Class.

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

      • Name: ZCL_SYSTEM_INFO_RFC_###
      • 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.
    5. Create or assign a transport request.

  • Step 10

    First, you need to create the types and variables that specify your remote connection information.

    Since the structure rfcsi is not a released object in SAP BTP, ABAP Environment, or in S/4HANA, Cloud edition, you need to create appropriate types for the variable lv_result. To generate these types for other remote function modules, use the transaction ACO_PROXY.

    For readability and reuse, you will create these in an interface.

    1. Select your package and choose New > Other ABAP Object > ABAP Interface from the context menu.

    2. Enter the following and choose Next.

    • Name: ZIF_TYPES_FOR_SYSTEM_INFO
    • Description: Types for RFC_GET_SYSTEM_INFO
    1. Choose the transport request, then choose Finish.

    2. Add the following to the interface:

      ABAP
      Copy
        types:
          RFCPROTO TYPE C LENGTH 000003 .
        types:
          RFCCHARTYP TYPE C LENGTH 000004   .
        types:
          RFCINTTYP TYPE C LENGTH 000003   .
        types:
          RFCFLOTYP TYPE C LENGTH 000003   .
        types:
          RFCDEST TYPE C LENGTH 000032   .
        types:
          RFCCHAR8 TYPE C LENGTH 000008  .
        types:
          SYSYSID TYPE C LENGTH 000008  .
        types:
          RFCDBHOST TYPE C LENGTH 000032  .
        types:
          SYDBSYS TYPE C LENGTH 000010  .
        types:
          SYSAPRL TYPE C LENGTH 000004  .
        types:
          RFCMACH TYPE C LENGTH 000005  .
        types:
          SYOPSYS TYPE C LENGTH 000010  .
        types:
          RFCTZONE TYPE C LENGTH 000006  .
        types:
          SYDAYST TYPE C LENGTH 000001  .
        types:
          RFCIPADDR TYPE C LENGTH 000015  .
        types:
          SYKERNRL TYPE C LENGTH 000004  .
        types:
          SYHOST TYPE C LENGTH 000032  .
        types:
          RFCSI_RESV TYPE C LENGTH 000012  .
        types:
          RFCIPV6ADDR TYPE C LENGTH 000045  .
        types:
          BEGIN OF RFCSI                         ,
                RFCPROTO                       TYPE RFCPROTO                      ,
                RFCCHARTYP                     TYPE RFCCHARTYP                    ,
                RFCINTTYP                      TYPE RFCINTTYP                     ,
                RFCFLOTYP                      TYPE RFCFLOTYP                     ,
                RFCDEST                        TYPE RFCDEST                       ,
                RFCHOST                        TYPE RFCCHAR8                      ,
                RFCSYSID                       TYPE SYSYSID                       ,
                RFCDATABS                      TYPE SYSYSID                       ,
                RFCDBHOST                      TYPE RFCDBHOST                     ,
                RFCDBSYS                       TYPE SYDBSYS                       ,
                RFCSAPRL                       TYPE SYSAPRL                       ,
                RFCMACH                        TYPE RFCMACH                       ,
                RFCOPSYS                       TYPE SYOPSYS                       ,
                RFCTZONE                       TYPE RFCTZONE                      ,
                RFCDAYST                       TYPE SYDAYST                       ,
                RFCIPADDR                      TYPE RFCIPADDR                     ,
                RFCKERNRL                      TYPE SYKERNRL                      ,
                RFCHOST2                       TYPE SYHOST                        ,
                RFCSI_RESV                     TYPE RFCSI_RESV                    ,
                RFCIPV6ADDR                    TYPE RFCIPV6ADDR                   ,
          END OF RFCSI                           .
      
      
    3. Format, save, and activate ( Sh+F1, Ctrl + Save, Ctrl + F3) your interface.

  • Step 11

    In the class implementation, in the method if_oo_adt_classrun~main, add the following variables.

    ```ABAP
    DATA(lo_destination) = cl_rfc_destination_provider=>create_by_comm_arrangement(
    
        comm_scenario          = 'Z_OUTBOUND_RFC_###_CSCEN'   " Communication scenario
        service_id             = 'Z_OUTBOUND_RFC_###_SRFC'    " Outbound service
    
                          ).
    
    DATA(lv_destination) = lo_destination->get_destination_name( ).
    
    DATA lv_result type ZIF_TYPES_FOR_SYSTEM_INFO=>rfcsi.
    DATA msg TYPE c LENGTH 255.
    
    ```
    
  • Step 12
    ABAP
    Copy
    CALL function 'RFC_GET_SYSTEM_INFO'
    destination lv_destination
      IMPORTING
        RFCSI_EXPORT      = lv_result.
    
    
  • Step 13

    Output the result of the RFC call to the ABAP Console

    ABAP
    Copy
    out->write( lv_result ).
    
  • Step 14

    Wrap the whole method in an exception using TRY…CATCH.

    ABAP
    Copy
    catch cx_root into data(lx_root).
      out->write( lx_root->get_text( ) ).
    endtry.
    
    
  • Step 15
    1. Go back to your RFC class.

      In the method implementation for if_oo_adt_classrun~main, add a DATA statement (before you call the function):

      ABAP
      Copy

      DATA msg TYPE c LENGTH 255.
    2. Now, in the function call for RFC_GET_SYSTEM_INFO, remove the period (.) after the IMPORTING parameter and add the following exception parameters to the function call RFC_GET_SYSTEM_INFO:

      ABAP
      Copy

      EXCEPTIONS system_failure = 1 MESSAGE msg communication_failure = 2 MESSAGE msg OTHERS = 3.
    3. Now evaluate sy-subrc by changing out->write( lv_result ). to include the following CASE...ENDCASE statement:

      ABAP
      Copy

      CASE sy-subrc. WHEN 0. out->write( lv_result ). WHEN 1. out->write( | EXCEPTION SYSTEM_FAILURE | && msg ). WHEN 2. out->write( | EXCEPTION COMMUNICATION_FAILURE | && msg ). WHEN 3. out->write( | EXCEPTION OTHERS | ). ENDCASE.
  • Step 16

    Your code should look roughly like this:

    ABAP
    Copy
    CLASS ZCL_SYSTEM_INFO_RFC_### DEFINITION
      public
      final
      create public .
    
    public section.
      interfaces if_oo_adt_classrun.
    
    protected section.
    private section.
    ENDCLASS.
    
    CLASS ZCL_SYSTEM_INFO_RFC_### IMPLEMENTATION.
      METHOD IF_OO_ADT_CLASSRUN~MAIN.
        TRY.
            DATA(lo_destination) = cl_rfc_destination_provider=>create_by_comm_arrangement(
    
                comm_scenario          = 'Z_OUTBOUND_RFC_###_CSCEN'   " Communication scenario
                service_id             = 'Z_OUTBOUND_RFC_###_SRFC'    " Outbound service
                comm_system_id         = 'Z_OUTBOUND_RFC_CSYS_###'        " Communication system
    
            ).
    
            DATA(lv_destination) = lo_destination->get_destination_name( ).
    
            DATA lv_result TYPE ZIF_TYPES_FOR_SYSTEM_INFO=>RFCSI.
            DATA msg TYPE c LENGTH 255.
    
            CALL FUNCTION 'RFC_GET_SYSTEM_INFO'
              DESTINATION lv_destination
              IMPORTING
                rfcsi_export          = lv_result
              EXCEPTIONS
                system_failure        = 1 MESSAGE msg
                communication_failure = 2 MESSAGE msg
                OTHERS                = 3.
    
            CASE sy-subrc.
              WHEN 0.
                out->write( lv_result ).
              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_text( ) ).
        ENDTRY.
        
      ENDMETHOD.
    
    ENDCLASS.
    
    

    When specifying the Communication System, what information do you require for the SAP Cloud Connector connection? Choose all that may apply:

  • Step 17
    1. Save and activate the class, using Ctrl+S, Ctrl+F3.

    2. Run the class by choosing F9. The ABAP Console output should look roughly like this:

    3. step15a-console-output-2023
  • Step 18
  • Step 19

    For information on ACO_PROXY, see:


Back to top