Skip to main content

SAP ECC / S4 HANA

This guide walks you through the steps required to connect Docflo.ai to your SAP system, enabling seamless document integration and data exchange.

πŸ“‹ Prerequisites:
  • SAP system with appropriate authorization
  • Access to STRUSTSSO2 and SM59 transactions
  • Docflo.ai account with API access
  • Development access to create RFC functions

πŸš€ Integration Steps​

Follow these steps to establish a secure connection between Docflo.ai and your SAP system:

Step 1: Import SSL Certificate Chain​

  1. Download our SSL certificate chain (link)
  2. Import the certificate chain to STRUSTSSO2 using the anonymous client configuration
  3. Ensure the certificate is properly validated and trusted

Step 2: Create SM59 Destination​

  1. Navigate to SM59 (RFC Destinations)
  2. Create a new destination with the following settings:
    • Use anonymous client authentication
    • Enable SSL connection
    • Configure the target host and port for Docflo.ai API
  3. Test the connection to ensure it's working properly

Step 3: Generate API Credentials​

  1. Go to the "Integrations" section in your Docflo.ai platform
  2. Create an API key for SAP integration
  3. Copy the API key and store it securely
  4. Copy the tenant ID as well - you'll need both for the RFC function

Step 4: Create RFC Function​

Create the RFC function ZDOCFLO_GET_DOCUMENTS in your SAP system using the code below:

FUNCTION zdocflo_get_documents.
*"----------------------------------------------------------------------
*"*"Local Interface:
*" IMPORTING
*" VALUE(DOCUMENT_TYPE) TYPE STRING
*" REFERENCE(TOP) TYPE STRING
*" REFERENCE(SKIP) TYPE STRING
*" REFERENCE(INLCUDE_RESULTS) TYPE STRING
*" EXPORTING
*" VALUE(DOCUMENT_IDS) TYPE STRING_TABLE
*"----------------------------------------------------------------------

TYPES: BEGIN OF ty_description,
type TYPE string,
valueString TYPE string,
content TYPE string,
END OF ty_description,

BEGIN OF ty_valueObject,
Description TYPE ty_description,
END OF ty_valueObject,

BEGIN OF ty_item,
type TYPE string,
valueObject TYPE ty_valueObject,
END OF ty_item,

tt_items TYPE STANDARD TABLE OF ty_item WITH DEFAULT KEY,

BEGIN OF ty_items_field,
value TYPE tt_items,
type TYPE string,
END OF ty_items_field,

BEGIN OF ty_modelFields,
Items TYPE ty_items_field,
END OF ty_modelFields,

BEGIN OF ty_docflo_results,
modelFields TYPE ty_modelFields,
END OF ty_docflo_results,

BEGIN OF ty_document,
_id TYPE string,
sourceId TYPE string,
sourceDesc TYPE string,
status TYPE string,
numOfPages TYPE string,
createdAt TYPE string,
docflo_results TYPE ty_docflo_results,
END OF ty_document,

tt_documents TYPE STANDARD TABLE OF ty_document WITH DEFAULT KEY,

BEGIN OF ty_response,
data TYPE tt_documents,
END OF ty_response.

DATA: ls_response TYPE ty_response,
ls_json_document TYPE ty_document.

" Data declarations
DATA: lv_docflo_api TYPE string,
lv_tenant_id TYPE string,
lv_api_key TYPE string,
lv_url TYPE string,
lo_http_client TYPE REF TO if_http_client,
lv_response TYPE string,
lv_status_code TYPE i,
lv_reason TYPE string.

" JSON processing
DATA: lo_reader TYPE REF TO cl_sxml_string_reader,
lo_writer TYPE REF TO cl_sxml_string_writer,
lv_json TYPE string.

DATA: lv_status_string TYPE string.

" Internal table for JSON parsing
DATA: BEGIN OF ls_document,
id TYPE string,

END OF ls_document,
lt_documents LIKE TABLE OF ls_document.

" Structure for output table
DATA: ls_document_id TYPE zdocflo_document_id.
* DATA: document_ids TYPE TABLE OF zdocflo_document_id.

" Variables for STVARV reading
DATA: lv_varname TYPE rvari_vnam,
lv_low TYPE rvari_val_255.

" Clear output table
REFRESH document_ids.

* Read DOCFLO_API parameter from STVARV
lv_varname = 'DOCFLO_API'.
SELECT SINGLE low FROM tvarvc
INTO lv_low
WHERE name = lv_varname
AND type = 'P'.

IF sy-subrc = 0.
lv_docflo_api = lv_low.
ELSE.
MESSAGE 'DOCFLO_API parameter not found in STVARV' TYPE 'E'.
RETURN.
ENDIF.

" Read DOCFLO_TENANT_ID parameter from STVARV
lv_varname = 'DOCFLO_TENANT_ID'.
SELECT SINGLE low FROM tvarvc
INTO lv_low
WHERE name = lv_varname
AND type = 'P'.

IF sy-subrc = 0.
lv_tenant_id = lv_low.
ELSE.
MESSAGE 'DOCFLO_TENANT_ID parameter not found in STVARV' TYPE 'E'.
RETURN.
ENDIF.

" Read DOCFLO_API_KEY parameter from STVARV
lv_varname = 'DOCFLO_API_KEY'.
SELECT SINGLE low FROM tvarvc
INTO lv_low
WHERE name = lv_varname
AND type = 'P'.

IF sy-subrc = 0.
lv_api_key = lv_low.
ELSE.
MESSAGE 'DOCFLO_API_KEY parameter not found in STVARV' TYPE 'E'.
RETURN.
ENDIF.

CONCATENATE '/docs/v1/document?type=' document_type '&includeResults=' INLCUDE_RESULTS INTO lv_url.

CALL METHOD cl_http_client=>create_by_destination
EXPORTING
destination = 'DOCFLO_API'
IMPORTING
client = lo_http_client
EXCEPTIONS
argument_not_found = 1
destination_not_found = 2
destination_no_authority = 3
plugin_not_active = 4
internal_error = 5
OTHERS = 6.

IF sy-subrc <> 0.
MESSAGE 'Error creating HTTP client' TYPE 'E'.
RETURN.
ENDIF.

IF lo_http_client IS BOUND.
cl_http_utility=>set_request_uri( request = lo_http_client->request uri = lv_url ).
lo_http_client->request->set_method( if_http_request=>co_request_method_get ).

" Set headers
lo_http_client->request->set_header_field(
name = 'x-tenant-id'
value = lv_tenant_id ).

lo_http_client->request->set_header_field(
name = 'apiKey'
value = lv_api_key ).

" Send the request
CALL METHOD lo_http_client->send
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3
http_invalid_timeout = 4
OTHERS = 5.

IF sy-subrc <> 0.
MESSAGE 'Error sending HTTP request' TYPE 'E'.
lo_http_client->close( ).
RETURN.
ENDIF.

" Receive the response
CALL METHOD lo_http_client->receive
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3
OTHERS = 4.

IF sy-subrc <> 0.
MESSAGE 'Error receiving HTTP response' TYPE 'E'.
lo_http_client->close( ).
RETURN.
ENDIF.

" Get response status
lo_http_client->response->get_status(
IMPORTING
code = lv_status_code
reason = lv_reason ).

MOVE lv_status_code TO lv_status_string.

* Check if request was successful
IF lv_status_code <> 200.
CONCATENATE 'HTTP Error:' lv_status_string lv_reason INTO lv_reason SEPARATED BY space.
MESSAGE lv_reason TYPE 'E'.
lo_http_client->close( ).
RETURN.
ENDIF.

" Get response data
lv_response = lo_http_client->response->get_cdata( ).

" Close HTTP client
lo_http_client->close( ).

/ui2/cl_json=>deserialize(
EXPORTING
json = lv_response
pretty_name = /ui2/cl_json=>pretty_mode-camel_case
CHANGING
data = ls_response
).

" Process each document in the data array
LOOP AT ls_response-data INTO ls_json_document.
DATA: lv_result_row TYPE STRING,
lv_descriptions TYPE STRING,
ls_item TYPE ty_item.

" Initialize result row with document ID and status
CONCATENATE ls_json_document-_id ls_json_document-status INTO lv_result_row SEPARATED BY '/'.

" Clear descriptions string for each document
CLEAR lv_descriptions.

" Loop through Items array to concatenate descriptions
LOOP AT ls_json_document-docflo_results-modelFields-Items-value INTO ls_item.
" Check if Description exists and has valueString
IF ls_item-valueObject-Description-valueString IS NOT INITIAL.
" Add separator if not the first description
IF lv_descriptions IS NOT INITIAL.
CONCATENATE lv_descriptions ', ' ls_item-valueObject-Description-valueString INTO lv_descriptions.
ELSE.
lv_descriptions = ls_item-valueObject-Description-valueString.
ENDIF.
ENDIF.
ENDLOOP.

" Append descriptions to result row if any were found
IF lv_descriptions IS NOT INITIAL.
CONCATENATE lv_result_row ' - Items: ' lv_descriptions INTO lv_result_row.
ENDIF.

APPEND lv_result_row TO document_ids.

ENDLOOP.

ENDIF.

ENDFUNCTION.

πŸ”§ Configuration Parameters​

The RFC function requires the following parameters to be configured in STVARV (table TVARVC):

  • DOCFLO_API: The base URL for the Docflo.ai API
  • DOCFLO_TENANT_ID: Your tenant ID from step 3
  • DOCFLO_API_KEY: Your API key from step 3

πŸŽ‰ You're all set! Once you've completed these steps, your SAP system will be able to communicate with Docflo.ai and retrieve document information seamlessly.

πŸ“ž Support​

If you encounter any issues during the integration process, please contact the Docflo.ai support team or consult your SAP system administrator.