Currently Being Moderated

After you have learned how to create database tables and ABAP dictionary types ABAP Trial Version for Newbies Part 14: The Foundation of an Application - Creating the Database Tables and ABAP Trial Version for Newbies Part 15: How to Dynamically Create Test Data for Our Database Table of our New Blog Series: ABAP Trial Version for Newbies, now it is a good time to create your first ABAP class, which works with this data.

Our first goal is an application which allows to display and change the data of a selected customer. In this blog we will create the necessary ABAP object and in the next one we will create the user interface with Web Dynpro. The intention is of course not to demonstrate a well thought out, realistic example of object orientated modelling of a real business problem, which is a science in itself, and would go beyond the scope of this blog. Here we want to demonstrate how classes can be implemented in ABAP Objects in the form of a tiny example.

The customer class will have a constructor, a set of public attributes and a save method which stores changed attributes to the database.

To create an ABAP class go to the ABAP workbench SE80, select "Class/Interface” and enter the name YCL_CUSTOMER in the input field and hit enter.

Confirm the pop up and enter a description in the next window while leaving the other settings as they are.

image

Double click the YCL_CUSTOMER class on the left hand side (below "Object Name”) and select the Attribute tab on the right hand side, to create the attributes of the class. We choose a subset of the SCUSTOM database columns as attributes of our class. Select the Attributes tab and add the attributes based on the ABAP dictionary types used for the corresponding database table column:

ID         TYPE  S_CUSTOMER
NAME       TYPE  S_CUSTNAME
STREET     TYPE  S_STREET
POSTCODE   TYPE  POSTCODE
CITY       TYPE  CITY
COUNTRY    TYPE  S_COUNTRY
TELEPHONE  TYPE  S_PHONENO
EMAIL      TYPE  S_EMAIL

All attributes should be public Instance attributes, set by the Level and Visibility column. The result looks like

image

Of course we could also declare the attributes as private and offer setter and getter methods, which might be better style but let’s keep our example as simple as possible for the moment.

Let’s assume an application which wants to instantiate a customer object knows in most cases already the ID of the customer (by a query method of a customer manager class for example). Therefore we create a constructor for the class with the customer ID as an optional parameter. The constructor then gets the data of the customer and fills the object’s attributes with the values from the database.

To create a constructor click on the Constructor button on the upper right hand corner and the editor for the already existing but empty constructor method opens.

image

Click on "Parameters” to add the customer ID as optional (check checkbox in third column!) parameter of the dictionary type S_CUSTOMER.

image

Clicking on "Methods” will bring you back to the former view and a double click on the CONSTRUCTOR method opens the ABAP editor.

In the constructor we will first check if the customer ID has been provided or not, since it was declared as an optional parameter.

If the ID is available we select the data of the customer with this ID from the database into a structure. The structure type is available in the ABAP dictionary because the database table SCUSTOM is based on it. Sometimes people are confused that a table in the database (table=many lines) is represented as structure (=single line) in the ABAP dictionary, but for the moment lets simply accept this as a fact.

The structure is declared at the top of the method with the data: statement and filled with the Open SQL statement SELECT…FROM…INTO. The important point is that SQL is directly integrated into ABAP. You just type in the SQL statements into your ABAP code. The application server automatically gives you the connection to the database and hides completely the differences between the SQL dialects of different database which can drive programmers crazy. This is done via the "Open SQL” called layer in the application server, offering a unified SQL which is translated into the specific SQL dialect of the currently used database, making ABAP programming independent of the database vendors.

Then the attributes of the object instance are filled with the result we got from the database. A column, the so called component of a.structure can be directly accessed with the "-" separator, for example the name of the customer is available as S_CUSTOMER-NAME.

The whole listing of the constructor method looks like

METHOD constructor.

* declaration of a structure matching the database table
  DATA: s_customer TYPE scustom.

* check if ID parameter has been supplied
  IF id IS SUPPLIED.

* get the data of the customer from the database
    SELECT SINGLE * FROM scustom INTO s_customer WHERE id = id.

* fill the object's attributes with the data
    me->id = id.
    me->name = s_customer-name.
    me->street = s_customer-street.
    me->postcode = s_customer-postcode.
    me->city = s_customer-city.
    me->country = s_customer-country.
    me->telephone = s_customer-telephone.
    me->email = s_customer-email.

  ENDIF.

ENDMETHOD.

Type in the coding (and I recommend to type it rather than cut and paste), save and check the syntax of the method with the check button. If no errors occur you can activate the class with the button next to the check button.

image

A special feature of the ABAP workbench is that it contains a test environment which you can use out of the box to test and execute your ABAP coding directly without the need of writing a test program. After you have successfully activated your class click on the test button and the workbench will display the following:

image

This screen is a built in test feature of the ABAP workbench. It generates automatically a test UI for an ABAP class based on its methods and attributes. The first screen offers all static public methods of a class, in our case only the constructor with its single parameter. Choose for example 00000001 as customer ID (1.) and click the instantiate button (2.). An object instance is created and its attributes are displayed:

 

image

 

We see the first customer in our dataset is a company named SAP AG, located in Walldorf, Germany. Unfortunately the data is already outdated. Last year the city of Walldorf renamed the Neurottstrasse to Dietmar-Hopp-Allee in honour of the founder and former CEO of SAP. Although we can change the street name due to the public attributes we still need a method which allows us to save the changed data to the database.

To create this new method go back to the class builder with the green back button click the Methods tab and go into change mode. Create a new method SAVE as public instance method. A double click on SAVE leads you to the editor. The code to save the current attribute values to the database is pretty simple:

METHOD save.

* declaration of structure
  DATA: s_customer TYPE scustom.

* fill structure with current attribute values
  s_customer-id = me->id.
  s_customer-name = me->name.
  s_customer-street = me->street.
  s_customer-postcode = me->postcode.
  s_customer-city = me->city.
  s_customer-country = me->country.
  s_customer-telephone = me->telephone.
  s_customer-email = me->email.

* update the data in the DB
  MODIFY scustom FROM s_customer.

ENDMETHOD.

It is necessary to put the current attribute values into a structure first, because the SQL statements for updating or modifying database tables need the values in a structure matching the database table structure. The MODIFY keyword is the Open SQL statement for either updating an existing dataset or creating a new one. (We could have choosen here also the UPDATE statement, but we want to enhance the class later also with the capability to create and save new entries into the database, therefore we chose the MODIFY.) Enter the code into the method and activate the class.

If you now click the Test button and instantiate the customer 00000001, the test environment will display also the new SAVE method.

image

Now we can correct the address to Dietmar Hopp Allee 16 (2.). Check the Upper/lower case active checkbox (1.), otherwise the data will be converted to uppercase and click on the clock/run icon (3.) right to the SAVE method.

Now the data is updated and if you call the test again or look into the database table with the transaction SE16 it will display the new address.

Although the integrated test capabilities of the ABAP workbench are quite useful for test purposes, we want to have a "real” application example in the end, of course. With our first ABAP class, we can start with creating a user interface in Web Dynpro, which allows to display, change and save customer addresses in the next blog.

Comments