Example of writing data to SAP using SAP BAPI package
- Updated: 2022/09/21
Example of writing data to SAP using SAP BAPI package
Using the SAP BAPI package, build a bot that writes data to the SAP database.
Prerequisites
Download the SAP Java connector and DLL from the SAP website.
In this tutorial, you will build a bot that invokes BAPI_FLCUST_CREATEFROMDATA from the SAP demo Flight Data application. You will use the CUSTOMER_DATA import parameter to pass the customer data to the BAPI. When executed, the bot creates a new customer record in SAP database and displays the customer number returned by the BAPI.
- Use the Get structure command to set values for the import parameters.
- Create a new record in an SAP table.
- Receive a response from a BAPI.
Procedure
-
Create a bot named
SAP-BAPI-CreateNewFlightCustomer.
- Log in to the Control Room.
- On the left pane, click Automation.
- Click the Create a bot icon.
- In the Name field, enter SAP-BAPI-CreateNewFlightCustomer, and click Create & Edit.
-
To connect the bot to your SAP system, from the
Actions panel, find SAP BAPI > Connect action and add it to the Bot editor.
- In JCo: SAP Java Connector package, specify the path to the Java connector .jar file.
- In JCo DLL dependency, specify the path to the DLL file.
- Choose Custom Application Server as Connection Type.
-
In Application server host name, specify the
host name of the SAP application server.
Note: As a best practice, use the Credential Vault to specify sensitive information such as host name, user name, and password.
- In System number, enter the instance number.
- In SAP instance system ID, enter the system ID.
- In Client number, enter the client number.
- In Logon language code, enter EN or the language specific to your system.
- In Router string, enter the SAP router string.
- In Username, enter your SAP user name.
- In Password, enter your password.
-
Run the bot to test the connection.
If the specified connection properties are correct, the bot is executed successfully.
-
To create a function, add the SAP BAPI > Create function action.
- In Function alias name, enter createNewFlightCustomer.
- In BAPI name, enter BAPI_FLCUST_CREATEFROMDATA.
-
Because the CUSTOMER_DATA parameter is of the type
structure, get the structure of the import parameter and assign it to an alias.
To do this, add the SAP BAPI > Get structure action.
- In Function alias name, enter createNewFlightCustomer.
- In Structure name, enter CUSTOMER_DATA.
- To store the retrieved structure as an alias, click the Alias tab in Destination.
-
In Create structure alias, enter
CustomerData.
You will use the alias to set values for the import field within the retrieved structure.
-
To set the value for the CUSTNAME field, add the SAP BAPI > Set field value action.
- In Function alias name, enter createNewFlightCustomer.
- In Source, click the Structure tab.
- In Structure alias, enter CustomerData.
- In Field name, enter CUSTNAME.
- In Field value, enter John Doe.
-
Repeat Step 5 to add and configure the Set field value
action for each of the following fields:
Field name Field value STREET testStreet POSTCODE 101001 CITY testCity COUNTR US COUNTR 00000000 EMAIL test@example.com CUSTTYPE P -
To execute BAPI_FLCUST_CREATEFROMDATA, add the SAP BAPI > Run function action.
- In Function alias name, enter createNewFlightCustomer.
Note: If you want BAPI_FLCUST_CREATEFROMDATA to write the data to SAP database, you must select the End sequence and the Commit transaction options. If you select the End sequence option, ensure that you select the Begin sequence option in the Create function action.The sample bot created with the help of this tutorial is intended to demonstrate how the Create() BAPIs work when the SAP BAPI package is used. It is not intended to modify the database.
The BAPI assigns and returns a customer number after a new customer record is created. -
To get the number into a variable, add the SAP BAPI > Get field value action.
- In Function alias name, enter createNewFlightCustomer.
- In Source, click the Function tab.
- In Field name, enter CUSTOMERNUMBER
- In Save the outcome to a variable, create a variable named strCustomerNumber by clicking (x) and select the variable.
-
To display the customer number, add the Message Box
action.
- In Enter the message to display, select the strCustomerNumber variable.
- Save and run the bot.