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

View this thread on: d.buzz | hive.blog | peakd.com | ecency.com
·@fintechresearch·
0.000 HBD
SAP ABAP Programming - Modularization and local classes in a Standard Report
<center> ![SAP-ABAP.jpg](https://steemitimages.com/DQmYn4mueD1f3iZeP7Mi4BygWAqXxmwC8kbCRB6KTkzZjNV/SAP-ABAP.jpg)</center>

**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](https://steemitimages.com/DQmboC3TNcZ5dP33S7fdq5MUYi8dAWv9kHFJthum63PQHbp/1.PNG)
 
After click on button create an other screen will be displayed. We must fill the fields as in the image. 

![2.PNG](https://steemitimages.com/DQmNQycXijNTK7JMvjZ1PEwu95e9fk7LZ38U1sS4wtWArfm/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](https://steemitimages.com/DQmXrmJt14RKMehZzsokdnf17mZZNiNgbM1LT1d9vqTpKcm/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](https://steemitimages.com/DQmUDvM28g2UNfNPpgKR6sLErabMRRiTE6A7kjMLUHPQdmD/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](https://steemitimages.com/DQmfXujDENyg4zT5imuzTEvzk55rYtNnWtxiETiVKAwXcQc/7.PNG)

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

![8.PNG](https://steemitimages.com/DQmQsGEG4XWp41J8iXm3VhtHws5e2tgx3jGGnC1cMm5Ks1d/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](https://steemitimages.com/DQmUM1JnX1bWGzNDRjGoYvwykQiFaKrPatmzDRaQXKEGkdv/9.PNG)

Here we can put the names that we want. 

![10.PNG](https://steemitimages.com/DQmTUX9bPEDdGTkeEM6mAZT5Tjz34gt9x7WHH4sU8PczwXh/10.PNG)

And if we test it...

![11.PNG](https://steemitimages.com/DQmWwjutxoJXz3H7nfbo1j1Uztnv6EzwLSpSv53t4d6kwHA/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](https://steemitimages.com/DQmWH18H6421tdjXA1wNjEHU2g8R6ay4APL2B6ZcL6SzZCf/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](https://steemitimages.com/DQmQ7cZgMgFcuN5gQfmTJBkppCDSZXdJeWzhoBBG9iDt9jr/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](https://steemitimages.com/DQmUY8AT9xweMcGUTrfWZizR3azfqroDSUw91JDT38ZbDb1/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](https://steemitimages.com/DQmVNg99TsvJqi5MpHHBY7unnJnx1zZQ2tQnvvmKzGJdPt1/16.PNG)

<center>_Input screen_ </center>

![18.PNG](https://steemitimages.com/DQmVnapCcZrUqp6x5bTHVEUwoMbwGpjeaH1GtsnmrHaKHig/18.PNG)

<center>_Output screen_ </center>

#### <center> Thanks for reading! don't forget to upvote and reestem! </center>
**********************************

_The screenshot were taken by me_
_[Main image]( http://programaenlinea.net/sabes-que-es-y-para-que-sirve-abap/)


![coollogo_com-149121399 (1).png](https://steemitimages.com/DQmdaMqmun6gjF8SRRQq3LA8sfRV4aHu4WhR36ttRzsxagJ/coollogo_com-149121399%20(1).png)
👍 , , , , , , ,