Invoking Business Agent Foundation Agents from ABAP: A Step-by-Step Guide

Estimated read time 12 min read

With the rise of intelligent automation and AI-powered solutions in enterprise landscapes, integrating SAP Business Technology Platform (BTP) services with on-premise ABAP systems has become increasingly important. In this blog, I’ll walk you through the process of invoking agents from the Business Agent Foundation service directly from an ABAP report—an integration that bridges the gap between traditional SAP systems and modern AI capabilities.

Whether you’re looking to enhance your business processes with intelligent agents or simply exploring the possibilities of AI integration in your SAP landscape, this guide will provide you with a comprehensive, hands-on approach to achieve this integration.

 

Pre-requisites: Subaccount in BTP, subscription of Business Agent Framework service in the subaccount, a dedicated client profile with required configurations.

 

Step 1: Create Your Agent in Business Agent Framework

Setting up Business Agent Framework service in your subaccount: Setup – Project Agent Builder Documentation

After setting the service, go to Instances → Project Agent Builder → Service Keys → view. Copy the endpoint url (uaa → url) which is required in the OAuth 2.0 Client Profile Configuration.

Start by opening the Business Agent Framework application in your BTP subaccount. Create a test agent and initialize a new chat session. This agent will be the endpoint we’ll communicate with from our ABAP system.

 

Note: If you already have a dedicated client profile with required configurations, directly go to “Step 2: Configure the RFC Destination”.

 

OAuth 2.0 Client Profile Configuration

Proper authentication is crucial for secure integration. Here’s how to set up your OAuth 2.0 client profile:

Part A: Creating the OAuth Client Profile

Navigate to transaction code SE80 (Object Navigator)

Select Development Object and choose your package

Right-click on the package and choose: Create → Others → OAuth 2.0 Client Profile and enter a meaningful name for your profile

 

Choose ‘DEFAULT’ as the service provider type

Save your profile

Assign the relevant scopes required for accessing the Business Agent Foundation service

Part B: Configuring the OAuth Client Profile

Start transaction code SOAUTH2_CLIENT (or OA2C_CONFIG for older versions)

Alternatively, access it via browser:

   https://<host_name>:<https_port>/sap/bc/webdynpro/sap/oa2c_config?sap-language=EN&sap-client=<client>Click Create and select your previously created client profile

Enter Client Credentials:Navigate to your BTP subaccountGo to: Instances → <your_service_instance> → Service KeysCopy the Client ID from the service key and enter it in the configuration

Configure Token Endpoint:

Under General Settings, enter the token endpoint URL without the `https://` prefix. 
Use the endpoint url which you copied in the Step 1.

Configure Grant Type:Select ‘Client Credentials’ as the grant typeThis is the standard grant type for server-to-server authentication

Verify Scopes:The associated scopes should automatically appear if they were properly configured in Part A

Test the configuration:Save your configurationNavigate to the Token tabClick Request New TokenIf successful, you’ll receive an access token, confirming that your OAuth configuration is working correctly

 

Note: For comprehensive details on OAuth 2.0 configuration in ABAP, refer to the official SAP documentation on [OAuth 2.0 Client for ABAP].

 

Step 2: Configure the RFC Destination

The destination acts as a bridge between your ABAP system and the BTP service. Here’s how to set it up:

2.1 Create the HTTP Destination

Navigate to transaction code SM59 in your ABAP systemCreate a new RFC destination with the following settings:Connection Type: Select ‘G’ (HTTP connection to external server)

2.2 Technical Settings

Under the Technical Settings tab enter the host URL of your BTP service without the “https://” prefix

2.3 Authentication Configuration

Navigate to the Logon & Security tab:Under Logon Procedure, select ‘Logon with User’Choose ‘Basic Authentication’ as the authentication method

Click on OAuth Settings:Select your previously configured OAuth client profile (see configuration details in the OAuth Client Configuration step above)

2.4 Test Your Connection

Click on Test Connection to verify connectivity to the host URLNavigate to the Test OAuth Configuration tab to validate the OAuth setupIf you encounter SSL certificate issues, use transaction code STRUST to upload the required SSL certificate

Note: For more comprehensive information on calling RESTful APIs from ABAP, refer to the [Calling RESTful APIs from SAP ABAP].

 

Step 3: Implement the ABAP Report

Writing the ABAP code to invoke the agent.

3.1 Create Your ABAP Report

Go to transaction code SE38 and create a new report.

3.2 Understanding the CL_HTTP_CLIENT Class

ABAP provides a powerful class called CL_HTTP_CLIENT for making HTTP calls to REST APIs. This class offers two primary methods for creating HTTP client instances:

`CREATE_BY_DESTINATION` – Uses a predefined RFC destination`CREATE_BY_URL` – Uses a direct URL

Since we’ve already created our destination, we’ll use `CREATE_BY_DESTINATION`.

3.3 The Complete Code Implementation
A. Create the HTTP Client Object

DATA: lo_http_client TYPE REF TO if_http_client. cl_http_client=>create_by_destination(
EXPORTING
destination = ‘<Name of the RFC destination>’
IMPORTING
client = lo_http_client ” HTTP Client object
EXCEPTIONS
argument_not_found = 1
destination_not_found = 2
destination_no_authority = 3
plugin_not_active = 4
internal_error = 5
OTHERS = 6
).

B. Configure HTTP Method and Version

lo_http_client->request->set_method( if_http_request=>co_request_method_post ).
lo_http_client->request->set_version( if_http_request=>co_protocol_version_1_1 ).

We’re using POST method since we’re sending data to the agent, and HTTP version 1.1 for compatibility.

C. Set the API Endpoint

lv_url = ‘/api/v1/Agents(agent_id)/chats(chat_id)/UnifiedAiAgentService.sendMessage’.
CALL METHOD cl_http_utility=>set_request_uri
EXPORTING
request = lo_http_client->request
uri = lv_url.

Note: Replace `agent_id` and `chat_id` with your actual agent and chat identifiers from Business Agent Framework.

D. Set Content Type

CALL METHOD lo_http_client->request->set_content_type( ‘application/json’ ).

Since we’re sending JSON data, we specify `application/json`. If you were working with XML, you’d use `application/xml` or `text/xml` instead.

E. Prepare and Send the Request Body

lv_string = ‘{“msg”: “give message here”, “outputFormat”: “Markdown”, “async”: false, “returnTrace”: true}’.
lo_http_client->request->set_cdata( lv_string ).

F. Execute the Request

CALL METHOD lo_http_client->send
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2.

CALL METHOD lo_http_client->receive
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3.

These two calls handle the request-response cycle.

G. Process the Response

CALL METHOD lo_http_client->response->get_status
IMPORTING
code = http_status
reason = reason.

response = lo_http_client->response->get_cdata( ).

H. Extract the Agent’s Response

SPLIT response AT ‘”response”:”‘ INTO rest response.
SPLIT response AT ‘”,”‘ INTO explanation rest.

This parsing logic extracts the actual response text from the JSON structure. The variable `explanation` now contains the agent’s reply.

 

 

In conclusion, integrating SAP Business Technology Platform (BTP) services with on-premise ABAP systems is a powerful way to leverage modern AI capabilities within your traditional SAP landscape. By following the step-by-step guide provided in this blog, you can successfully invoke agents from the Business Agent Foundation service directly from an ABAP report. This integration not only enhances your business processes with intelligent automation but also demonstrates the flexibility and extensibility of SAP systems.

 

​ With the rise of intelligent automation and AI-powered solutions in enterprise landscapes, integrating SAP Business Technology Platform (BTP) services with on-premise ABAP systems has become increasingly important. In this blog, I’ll walk you through the process of invoking agents from the Business Agent Foundation service directly from an ABAP report—an integration that bridges the gap between traditional SAP systems and modern AI capabilities.Whether you’re looking to enhance your business processes with intelligent agents or simply exploring the possibilities of AI integration in your SAP landscape, this guide will provide you with a comprehensive, hands-on approach to achieve this integration. Pre-requisites: Subaccount in BTP, subscription of Business Agent Framework service in the subaccount, a dedicated client profile with required configurations. Step 1: Create Your Agent in Business Agent FrameworkSetting up Business Agent Framework service in your subaccount: Setup – Project Agent Builder DocumentationAfter setting the service, go to Instances → Project Agent Builder → Service Keys → view. Copy the endpoint url (uaa → url) which is required in the OAuth 2.0 Client Profile Configuration.Start by opening the Business Agent Framework application in your BTP subaccount. Create a test agent and initialize a new chat session. This agent will be the endpoint we’ll communicate with from our ABAP system. Note: If you already have a dedicated client profile with required configurations, directly go to “Step 2: Configure the RFC Destination”. OAuth 2.0 Client Profile ConfigurationProper authentication is crucial for secure integration. Here’s how to set up your OAuth 2.0 client profile:Part A: Creating the OAuth Client ProfileNavigate to transaction code SE80 (Object Navigator)Select Development Object and choose your packageRight-click on the package and choose: Create → Others → OAuth 2.0 Client Profile and enter a meaningful name for your profile Choose ‘DEFAULT’ as the service provider typeSave your profileAssign the relevant scopes required for accessing the Business Agent Foundation servicePart B: Configuring the OAuth Client ProfileStart transaction code SOAUTH2_CLIENT (or OA2C_CONFIG for older versions)Alternatively, access it via browser:   https://<host_name>:<https_port>/sap/bc/webdynpro/sap/oa2c_config?sap-language=EN&sap-client=<client>Click Create and select your previously created client profileEnter Client Credentials:Navigate to your BTP subaccountGo to: Instances → <your_service_instance> → Service KeysCopy the Client ID from the service key and enter it in the configurationConfigure Token Endpoint:Under General Settings, enter the token endpoint URL without the `https://` prefix. Use the endpoint url which you copied in the Step 1.Configure Grant Type:Select ‘Client Credentials’ as the grant typeThis is the standard grant type for server-to-server authenticationVerify Scopes:The associated scopes should automatically appear if they were properly configured in Part ATest the configuration:Save your configurationNavigate to the Token tabClick Request New TokenIf successful, you’ll receive an access token, confirming that your OAuth configuration is working correctly Note: For comprehensive details on OAuth 2.0 configuration in ABAP, refer to the official SAP documentation on [OAuth 2.0 Client for ABAP]. Step 2: Configure the RFC DestinationThe destination acts as a bridge between your ABAP system and the BTP service. Here’s how to set it up:2.1 Create the HTTP DestinationNavigate to transaction code SM59 in your ABAP systemCreate a new RFC destination with the following settings:Connection Type: Select ‘G’ (HTTP connection to external server)2.2 Technical SettingsUnder the Technical Settings tab enter the host URL of your BTP service without the “https://” prefix2.3 Authentication ConfigurationNavigate to the Logon & Security tab:Under Logon Procedure, select ‘Logon with User’Choose ‘Basic Authentication’ as the authentication methodClick on OAuth Settings:Select your previously configured OAuth client profile (see configuration details in the OAuth Client Configuration step above)2.4 Test Your ConnectionClick on Test Connection to verify connectivity to the host URLNavigate to the Test OAuth Configuration tab to validate the OAuth setupIf you encounter SSL certificate issues, use transaction code STRUST to upload the required SSL certificateNote: For more comprehensive information on calling RESTful APIs from ABAP, refer to the [Calling RESTful APIs from SAP ABAP]. Step 3: Implement the ABAP ReportWriting the ABAP code to invoke the agent.3.1 Create Your ABAP ReportGo to transaction code SE38 and create a new report.3.2 Understanding the CL_HTTP_CLIENT ClassABAP provides a powerful class called CL_HTTP_CLIENT for making HTTP calls to REST APIs. This class offers two primary methods for creating HTTP client instances:`CREATE_BY_DESTINATION` – Uses a predefined RFC destination`CREATE_BY_URL` – Uses a direct URLSince we’ve already created our destination, we’ll use `CREATE_BY_DESTINATION`.3.3 The Complete Code ImplementationA. Create the HTTP Client ObjectDATA: lo_http_client TYPE REF TO if_http_client. cl_http_client=>create_by_destination(
EXPORTING
destination = ‘<Name of the RFC destination>’
IMPORTING
client = lo_http_client ” HTTP Client object
EXCEPTIONS
argument_not_found = 1
destination_not_found = 2
destination_no_authority = 3
plugin_not_active = 4
internal_error = 5
OTHERS = 6
).B. Configure HTTP Method and Versionlo_http_client->request->set_method( if_http_request=>co_request_method_post ).
lo_http_client->request->set_version( if_http_request=>co_protocol_version_1_1 ).We’re using POST method since we’re sending data to the agent, and HTTP version 1.1 for compatibility.C. Set the API Endpointlv_url = ‘/api/v1/Agents(agent_id)/chats(chat_id)/UnifiedAiAgentService.sendMessage’.
CALL METHOD cl_http_utility=>set_request_uri
EXPORTING
request = lo_http_client->request
uri = lv_url.Note: Replace `agent_id` and `chat_id` with your actual agent and chat identifiers from Business Agent Framework.D. Set Content TypeCALL METHOD lo_http_client->request->set_content_type( ‘application/json’ ).Since we’re sending JSON data, we specify `application/json`. If you were working with XML, you’d use `application/xml` or `text/xml` instead.E. Prepare and Send the Request Bodylv_string = ‘{“msg”: “give message here”, “outputFormat”: “Markdown”, “async”: false, “returnTrace”: true}’.
lo_http_client->request->set_cdata( lv_string ).F. Execute the RequestCALL METHOD lo_http_client->send
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2.

CALL METHOD lo_http_client->receive
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3.These two calls handle the request-response cycle.G. Process the ResponseCALL METHOD lo_http_client->response->get_status
IMPORTING
code = http_status
reason = reason.

response = lo_http_client->response->get_cdata( ).H. Extract the Agent’s ResponseSPLIT response AT ‘”response”:”‘ INTO rest response.
SPLIT response AT ‘”,”‘ INTO explanation rest.This parsing logic extracts the actual response text from the JSON structure. The variable `explanation` now contains the agent’s reply.  In conclusion, integrating SAP Business Technology Platform (BTP) services with on-premise ABAP systems is a powerful way to leverage modern AI capabilities within your traditional SAP landscape. By following the step-by-step guide provided in this blog, you can successfully invoke agents from the Business Agent Foundation service directly from an ABAP report. This integration not only enhances your business processes with intelligent automation but also demonstrates the flexibility and extensibility of SAP systems.   Read More Technology Blog Posts by SAP articles 

#SAP

#SAPTechnologyblog

You May Also Like

More From Author