Skip to Content

Call a Remote Function Module From SAP Business Technology Platform (BTP), ABAP Environment

test
0 %
Call a Remote Function Module From SAP Business Technology Platform (BTP), ABAP Environment
Details

Call a Remote Function Module From SAP Business Technology Platform (BTP), ABAP Environment

Requires Customer/Partner License
April 2, 2021
Created by
April 29, 2019
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 open a secure tunnel connection between your SAP BTP, ABAP Environment and an on-premise SAP System, e.g. SAP S/4HANA
  • How to create a destination service instance with an RFC connection
  • How to test the connection using an ABAP handler class
QR code

Prerequisites

Throughout this tutorial, replace XXX or JP with your initials or group number.

The problem:

There are two problems when setting up connectivity between the SAP BTP, ABAP Environment and an on-premise:

  • The ABAP Environment “lives” in the Internet, but customer on-premise systems are behind a firewall
  • RFC is not internet-enabled

The solution:

  • Set up a secure, tunnel connection from the on-premise system to the SAP BTP, ABAP Environment

Specifically:

  1. The ABAP environment tenant fetches the destination from the Destination service instance.
  2. The ABAP environment tenant requests to open the tunnel connection through the Connectivity service.
  3. The Connectivity service tells the Cloud Connector to open the connection to this specific ABAP environment tenant using the admin connection.
  4. The Cloud Connector opens a tunnel connection to the ABAP environment tenant using its public tenant URL.
  5. After the tunnel is established, it can be used for actual data connection using the RFC or HTTP(S) protocols.
Image depicting overview-cf-only
Step 1: Configure SAP Cloud Connector

First, you need to connect your ABAP on-premise system to a Cloud Foundry subaccount 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. Choose Add Subaccount:

    Field Name Value
    Region Your region. You can find this in SAP BTP cockpit (see screenshot below) - e.g. here, Europe (Frankfurt) - AWS
    Subaccount Cloud Foundry Subaccount ID. You can find this by choosing your subaccount in SAP BTP cockpit and choosing the information (i) icon. (see screenshot below)
    Display Name (Subaccount) Display Name. You can find this in by choosing your subaccount in SAP BTP cockpit (see screenshot below)
    Subaccount User
    Password
    Location ID Optional here. However, it is mandatory if you want to connect several Cloud Connectors to your subaccount. This can be any text, e.g. XXX for your initials or group number as here
    step1a-cf-name-id-subac

Your configuration should now look like this. Note down the Location ID, here XXX. You will need it later.

step1b-add-subaccount
step1c-cf-check-scc-xxx

Log on to answer question
Step 2: Add On-Premise System
  1. In the menu in the left pane, expand the subaccount and choose Cloud To On-Premise > Access Control.

    Image depicting step2a-cf-add-onP-system
  2. In the Mapping Virtual to Internal System pane, choose Add (+).

    step2b-cf-add-onP-system
  3. Enter the following values, and choose Save.

    Field Name Value
    Backend Type ABAP
    Protocol RFC
    Without Load Balancing
    Application Server IP address of the on-premise server, e.g. of NPL
    Instance Number 00
    Virtual Host e.g. nplhost. 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.
    Virt. Inst. No. 00
    Principal Type None
    Description Optional
    Check Internal Host Ticked
    step2c-cf-map-system

The mapping should now look something like this. Check that the status = Reachable. If not, check that you chose the correct port, or whether an internal firewall is preventing communication:

step2d-cf-mapped-to-virtual-system
Log on to answer question
Step 3: Specify remote function modules and BAPIs

Now, still in the Cloud to On-Premise > Access Control tab, enter the resource you need, RFC_SYSTEM_INFO.

  1. Add the resource RFC_SYSTEM_INFO by choosing the Protocol = RFC, then choosing +.

    step3a-cf-add-rfc-resource
  2. Enter the name of the RFC, e.g. RFC_SYSTEM_INFO. Alternatively, add RFC as a Prefix. Then choose Save

    step3b-cf-name-rfc
  3. Add BAPIs to the list of resources by choosing + again. (You will need this BAPI in a later tutorial.)

  4. Enter the name BAPI_EPM as a Prefix, then choose Save.

  5. The list of resources should now look roughly like this.

    step3c-cf-resources-rfc
Log on to answer question
Step 4: Check connectivity from SAP BTP cockpit

In the SAP BTP cockpit of your Cloud Foundry subaccount, choose Cloud Connectors:

step4a-cf-cloud-connectors-in-sap-cloud-cockpit

The location ID points to the correct SAP Cloud Connector (located in the on-Premise system); The virtual host points to the on-Premise connection mapped in SAP Cloud Connector.

Log on to answer question
Step 5: Create destination

You will now create a destination in the ABAP Environment. This must be created at subaccount (not Space) level.

  1. In the SAP BTP cockpit of your Cloud Foundry subaccount, choose Destinations, then choose New Destinations.

    step4a-cf-cockpit-new-destination
  2. Enter the following values:

    Field Name Value
    Name e.g. NPL_JP as here
    Type RFC
    Description Can be anything, here NPL
    Location ID same as in step 1, e.g. XXX
    User Your user for the on-premise system, e.g. DEVELOPER
    Password Your password
  3. Add the following additional properties and values, by choosing New Property:

    Field Name Value
    jco.client.ashost Virtual hostname of your on-premise ABAP System, defined in SAP Cloud Connector, e.g. <nplhost>
    jco.client.client <Your ABAP System client, e.g. 001
    jco.client.sysnr <Your ABAP System number>, e.g. 00
    step5b-cf-destination-created

    .
    step4b-cf-connection-successful

Log on to answer question
Step 6: Create ABAP class for RFC connection
  1. Create a new ABAP class: Choose File > New > Other… > ABAP Class.

  2. Enter a name and description. The name should be in the form ZCL_...RFC_XXX. Replace XXX with your group number or initials.

  3. Create or assign a transport request.

Log on to answer question
Step 7: Add interfaces statement; implement main method
  1. Implement the interface by adding this statement to the public section:

    interfaces if_oo_adt_classrun.

    This allows you to test your class by displaying the output in the ABAP Console.

  2. In the implementation section, add the METHOD and ENDMETHOD statements:

    METHOD IF_OO_ADT_CLASSRUN~MAIN.
    ENDMETHOD.

Log on to answer question
Step 8: Create variables

Create the data types that specify your remote connection information, replacing the i_name with your the name of the specific RFC destination, which you created in SAP BTP cockpit (in step 5 of this tutorial).

```ABAP
DATA(lo_destination) = cl_rfc_destination_provider=>CREATE_BY_CLOUD_DESTINATION(
                        i_name                  = 'NPL_JP'

                       ).

DATA(lv_destination) = lo_destination->get_destination_name( ).

DATA lv_result type c length 200.

```
Log on to answer question
Step 9: Call remote function from on-premise system
CALL function 'RFC_SYSTEM_INFO'
destination lv_destination
  IMPORTING
    RFCSI_EXPORT      = lv_result.

Log on to answer question
Step 10: Output result

Output the result of the RFC call to the ABAP Console

out->write( lv_result ).
Log on to answer question
Step 11: Wrap method in an exception

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

catch cx_root into data(lx_root).
  out->write( lx_root->get_text( ) ).
endtry.
Image depicting step15-try-catch
Log on to answer question
Step 12: Check your code

Your code should look roughly like this:

CLASS ZCL_A4C_RFC_XXX DEFINITION
  public
  final
  create public .

public section.
  interfaces if_oo_adt_classrun.
protected section.
private section.
ENDCLASS.

CLASS ZCL_A4C_RFC_XXX IMPLEMENTATION.
  METHOD IF_OO_ADT_CLASSRUN~MAIN.
    TRY.
      DATA(lo_destination) = cl_rfc_destination_provider=>CREATE_BY_CLOUD_DESTINATION(
                              i_name                  = 'NPL_JP'
                             ).

      DATA(lv_destination) = lo_destination->get_destination_name( ).

      DATA lv_result type c length 200.

      CALL function 'RFC_SYSTEM_INFO'
      destination lv_destination
        IMPORTING
          RFCSI_EXPORT      = lv_result.

        out->write( lv_result ).
    catch cx_root into data(lx_root).
      out->write( lx_root->get_text( ) ).
    endtry.
  ENDMETHOD.

ENDCLASS.
Log on to answer question
Step 13: Test the class
  1. Save and activate the class, using Ctrl+S, Ctrl+F3.

  2. Run the class by choosing F9. Some system information, such as the hostname, the System ID ( <SID> ), and the IP address should be displayed.

Log on to answer question
Step 14: Test yourself
When creating a destination in SAP Cloud Cockpit, which information do you require from SAP Cloud Connector in your on-Premise system. Choose all that apply:
×
Step 15: Add error handling to the class for the RFC connection
  1. Go back to your RFC class. Remove the period (.) after the IMPORTING parameter and add the following exception parameters to the function call RFC_SYSTEM_INFO:


    EXCEPTIONS system_failure = 1 MESSAGE msg communication_failure = 2 MESSAGE msg OTHERS = 3.
  2. Now evaluate sy-subrc by adding the following CASE…ENDCASE statement:


    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.
    Image depicting step20-error-handling
Log on to answer question

More Information

For more information on SAP Business Technology Platform (BTP)
- SAP Help Portal: What is SAP Business Technology Platform (BTP)

For more information on connectivity in this context, see:
- SAP Help Portal: SAP Cloud Connector

For more information on OData services and SAP Gateway in general, see:


Next Steps

Back to top