Book Image

SAP ABAP Advanced Cookbook

By : Rehan Zaidi
Book Image

SAP ABAP Advanced Cookbook

By: Rehan Zaidi

Overview of this book

ABAP (Advanced Business Application Programming) is SAP's proprietary 4th Generation Language (4GL). SAP core is written almost entirely in ABAP.ABAP is a high level programming language used in SAP for development and other customization processes."SAP ABAP Advanced Cookbook"ù covers advanced SAP programming applications with ABAP. It teaches you to enhance SAP applications by developing custom reports and interfaces with ABAP programming. This cookbook has quick and advanced real world recipes for programming ABAP.It begins with the applications of ABAP Objects and ALV tips and tricks. It then covers Design Patterns and Dynamic Programming in detail.You will also learn the usage of quality improvement tools such as transaction SAT, SQL Trace, and the Code Inspector.Simple transformations and its application in Excel Downloading will also be discussed, as well as the newest topics of Adobe Interactive Forms and the consumption and creation of Web services. The book comes to an end by covering advanced usage of Web Dynpro for ABAP and the latest advancement in Floorplan Manager.
Table of Contents (22 chapters)
SAP ABAP Advanced Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Creating a persistent object


ABAP objects provide a persistent object service that allows the developer to store objects in the database. The values of the attributes of the object are stored in appropriate fields of the database table specified. This recipe shows how to define persistent classes and then how to call them in your application programs.

Getting ready

Prior to storing objects in the database, a suitable database table with the name ZEMP_TABLE is created to store the values of the objects' attributes. Two fields are defined, NUMBER1 and NAME (the field name NUMBER was not allowed, so NUMBER1 has been used as the field name). Refer to the following screenshot:

How to do it...

Once the database table is defined, a persistence class must be defined. In order to define persistent classes, follow these steps:

  1. Call transaction SE24. Enter a suitable name of the persistent class to be created. We will create a class by entering the name ZCL_MY_PERSIST. Enter the name in the Class field and click on the Create button.

  2. Enter a suitable description in the field provided. Make sure that the Persistent Class indicator is selected, and click on Save.

  3. The programmer may only modify the methods HANDLE_EXCEPTION and INIT.

  4. Click on the Persistence button. Then, enter the name of the table that was created for storage of data( in our case, we will enter the name ZEMP_TABLE). Refer to the following screenshot:

  5. This will take you to the mapping editor. The lower part of the screen will show Table/Fields. Double-click each of the field that is to be included and stored as attributes of the persistent class. The selected field appears in the area earlier (for example, the NUMBER1 field as shown in the following screenshot). Click on the Set attribute values button to include the field.

  6. This will transfer the selected field in the topmost area of the editor.

  7. Similarly, the NAME field must be included.

  8. All the mapped fields will appear at the top area of the mapper. The Number1 field will appear as a business key, as show in the following screenshot:

  9. Upon activation of the persistence class, the system asks for activation of the actor class as well. Click on Yes, as shown in the following screenshot:

  10. The class ZCL_MY_PERSIST is created and necessary methods needed for the persistence service are included. An actor class is also created with the class. The agent class has been generated by the name ZCA_MY_PERSIST. There is one base agent class generated as a result. In total, three classes are generated, the persistent class, the agent class, and the base class of the agent.

  11. The class ZCL_MY_PERSIST contains methods for setting and getting the values of the attributes NAME and NUMBER1. Note that no SET method is generated for the key field, in our case NUMBER1.

  12. The agent class provides number of useful methods related to the persistent property. Important methods, such as create_persistent, delete_persistant, and get_persistent are provided. The methods are implemented in the superclass zcb_my_persist of the agent class zca_my_persist.

How it works...

During the generation of the persistent class zcl_my_persist, two additional classes are generated. These are the actor (agent) and the base agent classes having the names zca_my_persist and zcb_my_persist respectively. The base agent class is generated as abstract (that is, no instance can be constructed from it), and cannot be modified. It is created in a separate pool class from zcl_my_persist. The agent class zca_my_persist may be extended, as well as the loading and saving methods may be modified.

The instantiation mode of the persistence class may be set as abstract or protected. In our recipe, we have chosen the instantiation mode as protected (which means that only instances may be created from within the class or its subclasses). However, making the instantiation mode of a persistent class as protected makes the generated base agent class a friend of the persistent class (in the world of ABAP objects, a friend or its subclasses may create instances of the class in question).

The coding for this recipe declares two references, emp and agent, to the persistent class zcl_my_persist and the agent class zca_my_persist, respectively. Next, the static factory method agent is called for the class zca_my_persist (agent class). The reference returned is stored in the variable agent.

The agent class contains the method create_persistent required for storing the data into the database (this is analogous to the concept of insertion in database table).

The most important part is the calling of the create_persistent method that is passed the number and name that is to be stored. The employee with the number 00000017 and name John Reed is created and reference is returned in emp. Finally, the COMMIT WORK method stores the data of the emp object into the table created earlier in this recipe. One row with the number and a name is added to the table ZEMP_TABLE.

For reading the stored value related to the employee number 00000017, a number variable is declared and assigned the value 00000017. The static method agent of the zca_my_persist class is called in order to get a reference to the agent. The get_persistent method is then called and the number (in our case, 00000017) is passed. This method returns the entire object emp pertaining to the employee number. You may then call the get_name method of the zcl_my_persist class for the emp object in order to retrieve the employee name.