SAP ABAP Programming - Modularization and local classes in a Standard Report

SAP-ABAP.jpg

SAP is the most popular ERP system in the world.

As a coder we have a very big chance to find a very good paid job if we know how to code in this plattform.

In this post I will show you how to modularize a Report and how to define and use a local class. This is the best practice to create a Z solution within SAP.

Create the main program


To define a class we must first create a program in which we can create an instance of it and where we can also modularize our source code. To do so we are going to enter into Transaction SE38 and create a report program.

1.PNG

After click on button create an other screen will be displayed. We must fill the fields as in the image.

2.PNG

Then, we are going to save this program in package $TMP because we don't want to transport it to an other system like Quality or Productive, first we are going to do this program as a test.

3.PNG

Since we created our main report Z_CLASS_TEST it is time to modularize the main parts, which are

  1. Global Variables (Top)
  2. Selection parameters
  3. Local class

This is good programming practice. An include is simply the encapsulation of one program into another.

Within our first include we will declare our global variables. As a good practice it is recombinable to name it the same as our program, but in the end put an underscore and then the word top.

Z_CLASS_TEST_TOP

We are going to apply the same name space rule for the other two includes, but for the one which includes the selection parámeters we are going to put at then of the name underscore and the word sel

Z_CLASS_TEST_SEL

For the last one we are going to put at the end the word lcl as an abbreviation of local class

Z_CLASS_TEST_LCL

Z_CLASS_TEST_TOP


Inside this include we can declare global variables, but there is one statement we must declare there.

The statement REPORT introduces an executable program and must be at the first include, i.e , the top include.

6.PNG

Z_CLASS_TEST_SEL


Inside this include we are going to declare our selection parameters. For example, imagine we want to know which Company Code, which year and which month as input parameters to search for a journal entry. Thus, we should write the following code:

PARAMETERS: pa_ccode TYPE bukrs,
            pa_year  TYPE gjahr,
            pa_month TYPE month.

7.PNG

If we execute our program at this point we should view an screen like this.

8.PNG

As you can notice the input fields have the same name that we put as variables, to change this into a descriptive name ABAP has a type of data called selection text. These objects are asociated by the reserved word parameters. Thus, we can put the label that we want instead of the name that we put in the source code. In fact we can even make translations depending of the log language the user decide. To edit this selection text we have to go into the Menu bar at the top of the screen and click on the option Go to -> Text elements -> Selection Text

9.PNG

Here we can put the names that we want.

10.PNG

And if we test it...

11.PNG

But since we want descriptive names and we are using predefined data types when we declared our selection parameters we can check the option Dictionary in the selection text menu and we are going to retrieve the reserved name for those data types which are already translated into more than 4 different languages. That is the advantage of using predefined data types.

13.PNG

Z_CLASS_TEST_LCL


In this include we are going to define our class. First we are going to define it's methods and attributes.

In this example we are going to define a Constructor and a method to display the data from the input at the selection screen.

CLASS lcl_class_test DEFINITION
  FINAL CREATE PRIVATE.

  PUBLIC SECTION.

    METHODS:
      constructor
      IMPORTING
         iv_ccode TYPE bukrs
         iv_year  TYPE gjahr
         iv_month type month,
      display.


    class-data:
         gv_ccode type bukrs,
         gv_year  type gjahr,
         gv_month type month.
ENDCLASS.            

14.PNG

Now that we have the definition part we are going to code the implementation part.

CLASS lcl_class_test IMPLEMENTATION.
  METHOD constructor.
    gv_ccode = iv_ccode.
    gv_year = iv_year.
    gv_month = iv_month.
  ENDMETHOD.                    "constructor
  METHOD display.
    WRITE: 'Company:' ,gv_ccode.
    WRITE: 'Year:' ,gv_year.
    WRITE: 'Month:' ,gv_month.
  ENDMETHOD.                    "display_color
ENDCLASS.                    "lcl_class_test IMPLEMENTATION

We are using the same paradigm of Object Oriented Programming, so we just need to take a look to the syntax and understand what is going on.

The main Report


Since we have all the includes with the source code needed at the main report we are going to create an instance of the class and a call to the method display.

Let's code...


START-OF-SELECTION.

data: lx_object type ref to lcl_class_test.

create OBJECT lx_object
  exporting
   iv_ccode =  pa_ccode
   iv_year =  pa_year
   iv_month =  pa_month.

 lx_object->display( ).

15.PNG

Executing the program


At the end if we execute the program we are going to have the following screens as input and output...

16.PNG

Input screen

18.PNG

Output screen

Thanks for reading! don't forget to upvote and reestem!


The screenshot were taken by me
_Main image

coollogo_com-149121399 (1).png