As ABAP developers we should write object-oriented code instead of procedural code, because it has better memory management, better syntax checks, more reuse possibilities and more. Also SAP states it is the preferred way to program ABAP. However, most of the business logic of SAP must be accessed by BAPIs *1, which is procedural code.
To overcome this hurdle, we must write
Business Object ABAP Classes for wrapping these BAPIs. Mostly I started wrapping the Create, Read and Update BAPIs and after that adding extra business logic and business data.
However, creating
BO ABAP classes manually is error prone, takes a long time, is boring and so it was a starting point for innovation. Therefore, I started the idea to create a tool which generates Business Object ABAP class code based on the Create, Read and Update BAPIs of SAP Business Objects.
This simple idea was not so simple to develop because the BAPIs come in many ways. For example in different ways of parameters (elements, structures and table parameters), different return parameters (structures vs tables) and different return data types (BAPIRET2, BAPIRET1, BAPIRETURN) and different ways of read BAPIs (GET_DETAIL and GET_LIST BAPIs). It took me a lot of time to finish it, and now it is ready to be shared and used by you. As an open source program, this program can freely be used and modified.
The code generator generates about 95 to 100 percent of the code, depending on the data of the BOR object type *2, main DB table *3 and the BAPIs. Sometimes you must do some small changes to get it work.
Installation
Installing the tool is very easy. Download one ABAP program from this Github repository:
https://github.com/alwinvandeput/abap_bapi_bo_class_generator
… and copy the program code ZAB_ABAP_BAPI_BO_CLASS_GEN_P.txt to your SAP system.
Demo
The use case is for this demo is reading SAP material data and getting the material description by making use of the BAPI.
We will execute the following steps
- Generating the code
- Creating the test program
- Executing the test program
Generating the code
- Start transaction SA38 to execute the ABAP program: ZAB_ABAP_BAPI_BO_CLASS_GEN_PRG.
- Push button “Select business object”.
- Filter on Main DB table “MARA” and double click the “Added - BUS1001 Material”.
- Double click Module abbreviation MM.
- The selection screen is filled now.
- Make sure the ABAP naming is selected.
- Press F8 and F8 again.
- The code is generated now, and all code is automatically selected to be copied.
- Push the editor button Copy
Create the test program
- Start transaction SE80 and create a new ABAP executable program.
- Paste the generated code in this program.
- Activate the code.
- See the generated classes
The important class is that Business Object class ZMM_MATERAIL_BO.
It contains the BAPIs:
- CREATE_INSTANCE_BO = BAPI_MATERIAL_SAVE
- GET_DATA = BAPI_MATERIAL_GET_DETAIL
- UPDATE_DATA = BAPI_MATERIAL_SAVE
All other classes are for the testing framework. Class ZMM_MATERAIL_BO_TT is the Unit Test for the BO class.
Testing the read method
- Execute the program by pressing F8
- See the results
It shows all the data which is retrieved by the GET_DATA method.
Field MATERIAL_GENERAL_DATA-MATL_DESC contains the material description(The other unit tests for CREATE and UPDATE a business object will be explained in upcoming blog posts.)
- For reading and showing the data only these lines were needed:
It could even be done in one line of code
Filtering feature
- Even filtering the results is possible. If I now for example the result external value is "PC" (pieces) than I could filter on *P*.
- And the result is...
Final thoughts
If you like the tool and you want to be informed on the upcoming blog posts, then you can click the “Follow” button. In the upcoming blog posts I will describe in detail how the tool works and why it makes programming faster and more robust.
I hope you are going to use the program and share it. Please share your thoughts, questions and critics in the comments in this blog post, and register improvements and issues on GitHub.
Specific words
*1 BAPI stands for Business Application Programming Interface. These interfaces can be called within SAP and by external programs outside SAP. To call methods of a BOR object type *1 methods, you must use the BAPIs.
*2 BOR object type : stands for a Business Object Repository object type. BOR types are the Business Objects of SAP and can be viewed in UI transaction BAPI.
*3 Main DB table is the header database table of the BOR object type. SAP stores this name on BAPI level in the database table SWOTLV in field REFSTRUCT.
Follow-up blog posts