BAPIs & RFC

RFC is SAP's protocol for calling RFC-enabled function modules across systems (ABAP ↔ ABAP, or external via SAP Connectors: JCo for Java, NCo for .NET, PyRFC for Python, node-rfc).

27.1 Remote Function Call (RFC)

RFC is SAP's protocol for calling RFC-enabled function modules across systems (ABAP ↔ ABAP, or external via SAP Connectors: JCo for Java, NCo for .NET, PyRFC for Python, node-rfc).

Synchronous (sRFC) CALL FUNCTION … DESTINATION 'D' Asynchronous (aRFC)

CALL FUNCTION … STARTING NEW TASK 'T' DESTINATION … CALLING meth ON END OF TASK Parallel, result via callback Transactional (tRFC)

CALL FUNCTION … IN BACKGROUND TASK DESTINATION … + COMMIT WORK Exactly once, unordered tRFC with queue (TRFC_SET_QUEUE_NAME) Exactly once, in order Background RFC (bgRFC)

CALL FUNCTION … IN BACKGROUND UNIT lo_unit Modern successor of tRFC/qRFC

Destinations are maintained in SM59 (type 3 = ABAP connection, T = TCP/IP, G/H = HTTP). DESTINATION 'NONE' calls locally in a new session.

Always handle system_failure and communication_failure for RFC calls. RFC function modules must use pass-by-value parameters and DDIC types.

CALL FUNCTION 'RFC_SYSTEM_INFO'
  DESTINATION 'QAS_CLNT100'
  IMPORTING
    rfcsi_export          = ls_info
  EXCEPTIONS
    system_failure        = 1 MESSAGE lv_msg
    communication_failure = 2 MESSAGE lv_msg
    OTHERS                = 3.

27.2 Parallel processing with aRFC

DO lines( lt_packages ) TIMES.
  CALL FUNCTION 'Z_PROCESS_PACKAGE'
    STARTING NEW TASK |TASK{ sy-index }|
    DESTINATION IN GROUP DEFAULT
    CALLING on_task_end ON END OF TASK
    EXPORTING it_data = lt_packages[ sy-index ]-data
    EXCEPTIONS resource_failure = 1 OTHERS = 2.
ENDDO.
WAIT FOR ASYNCHRONOUS TASKS UNTIL gv_finished = lines( lt_packages ) UP TO 600 SECONDS.

METHOD on_task_end.          " IMPORTING p_task TYPE clike
  RECEIVE RESULTS FROM FUNCTION 'Z_PROCESS_PACKAGE' IMPORTING ev_count = DATA(lv_cnt).
  gv_finished += 1.
ENDMETHOD.

27.3 BAPIs

BAPIs (Business Application Programming Interfaces) are stable, documented, RFC-enabled function modules that implement methods of business objects in the Business Object Repository (BOR, transaction SWO1). Browse them in transaction BAPI.

Name BAPI_<OBJECT>_<METHOD> (e.g. BAPI_SALESORDER_CREATEFROMDAT2, BAPI_PO_CREATE1, BAPI_MATERIAL_SAVEDATA, BAPI_ACC_DOCUMENT_POST).

They do not commit — the caller must call BAPI_TRANSACTION_COMMIT (or ROLLBACK).

Errors are returned in a RETURN parameter (BAPIRET2: TYPE, ID, NUMBER, MESSAGE…), not as exceptions.

X-structures (e.g. ORDER_HEADER_INX) flag which fields to change.

Test BAPIs interactively in SE37 with a test sequence (BAPI + commit in the same session). In S/4HANA Cloud and ABAP Cloud, many BAPIs are replaced by released RAP business objects accessed via EML (§34).

DATA: ls_header  TYPE bapisdhd1,
      ls_headerx TYPE bapisdhd1x,
      lt_items   TYPE STANDARD TABLE OF bapisditm,
      lt_itemsx  TYPE STANDARD TABLE OF bapisditmx,
      lt_partner TYPE STANDARD TABLE OF bapiparnr,
      lt_sched   TYPE STANDARD TABLE OF bapischdl,
      lt_return  TYPE STANDARD TABLE OF bapiret2,
      lv_vbeln   TYPE vbeln_va.

ls_header  = VALUE #( doc_type = 'OR' sales_org = '1000' distr_chan = '10' division = '00' ).
ls_headerx = VALUE #( updateflag = 'I' doc_type = 'X' sales_org = 'X' distr_chan = 'X' division = 'X' ).
lt_partner = VALUE #( ( partn_role = 'AG' partn_numb = '0000001000' ) ).
lt_items   = VALUE #( ( itm_number = '000010' material = 'MAT-001' ) ).
lt_itemsx  = VALUE #( ( itm_number = '000010' updateflag = 'I' material = 'X' ) ).
lt_sched   = VALUE #( ( itm_number = '000010' sched_line = '0001' req_qty = 5 ) ).

CALL FUNCTION 'BAPI_SALESORDER_CREATEFROMDAT2'
  EXPORTING
    order_header_in    = ls_header
    order_header_inx   = ls_headerx
  IMPORTING
    salesdocument      = lv_vbeln
  TABLES
    return             = lt_return
    order_items_in     = lt_items
    order_items_inx    = lt_itemsx
    order_partners     = lt_partner
    order
…

27.4 Calling external systems via HTTP

(CL_WEB_HTTP_CLIENT_MANAGER is the released HTTP client for ABAP Cloud; classic code uses CL_HTTP_CLIENT. Use /UI2/CL_JSON on-premise or XCO_CP_JSON / CALL TRANSFORMATION id for JSON.)

DATA(lo_dest) = cl_http_destination_provider=>create_by_url( `https://api.example.com/rates` ).
DATA(lo_client) = cl_web_http_client_manager=>create_by_http_destination( lo_dest ).
DATA(lo_response) = lo_client->execute( if_web_http_client=>get ).
DATA(lv_json) = lo_response->get_text( ).
lo_client->close( ).