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.
![](/legacyfs/online/storage/blog_attachments/2019/08/210_sa38_generator.png)
- Push button “Select business object”.
![](/legacyfs/online/storage/blog_attachments/2019/08/220_select_business_object.png)
- Filter on Main DB table “MARA” and double click the “Added - BUS1001 Material”.
![](/legacyfs/online/storage/blog_attachments/2019/08/230_filter_main_db_table_mara.png)
- Double click Module abbreviation MM.
![](/legacyfs/online/storage/blog_attachments/2019/08/240_double_click_abbreviation_mm.png)
- The selection screen is filled now.
![](/legacyfs/online/storage/blog_attachments/2019/08/250_selection_screen.png)
- 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.
![](/legacyfs/online/storage/blog_attachments/2019/08/260_generated_code.png)
- 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
![](/legacyfs/online/storage/blog_attachments/2019/08/270_se80_generated_classes.png)
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
![](/legacyfs/online/storage/blog_attachments/2019/08/280_read_result_tree.png)
- 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
![](/legacyfs/online/storage/blog_attachments/2019/08/294_read_abap_code_one_line.png)
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*.
![](/legacyfs/online/storage/blog_attachments/2019/08/300_filtering.png)
- And the result is...
![](/legacyfs/online/storage/blog_attachments/2019/08/310_filter_results.png)
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