After you have learned how to get the ABAP Object Navigator (transaction SE80) in the last tip, it is now time...
to do it. And we will do here and now in this tip, we will write a "Hello World"-program.
Again, you will recognize a principle that I have explained in my tips on the server. The ABAP development environment offers you a lot of benefits for free that take quite some extra work when developing in other languages. When developing in ABAP, your program becomes part of a large mechanism that helps to manage and organize development objects in a system landscape from the very outset. Let my elucidate this by an analogy. Creating a development object with C or Java is in a way like writing a text with a word processor: You write it, save it where you want and that's it. In contrast, developing a program in ABAP is like writing a text with a content management system where you are forced to create a document as part of a larger context with a number of default attributes, which help to pigeonhole your document within this system.
The main structure is pretty simple: Every development object in ABAP belongs to a package. In general, every package has a property that determines to which other systems the package plus its content is transported if the package plus its elements are assigned to a transport request. The only exception to this rule is the package $tmp which is made for local developments. The content of this package is not transported to any other systems. In this tip we will create a program in this package for local developments, while in a later tip we will create a package that we will use as a container for all elements we will create in other tips.
The Repository Browser The Central Place in the ABAP IDE
The Object Navigator is the development environment for the central editing of all development objects. We call these objects repository objects. This includes all ABAP programs of AS ABAP and all their components. However, there are many other repository objects, such as global data definitions in the ABAP Dictionary, global classes, or XSLT programs. Together, these development objects in the ABAP Workbench form a so-called repository. This repository is a special part of central database, which, instead of customer data, comprises the programs of AS ABAP itself.
The buttons in the upper part of the navigation area allow the selection of a browser. You can configure in Utilities - Settings which browsers are presented for selection with buttons. The browser we will generally be using is the Repository Browser (1). It is the default setting of the Object Navigator and also the most generic browser, which provides an overview of all important repository objects sorted by different criteria.
You access the repository objects through the Repository Browser by using so-called object lists. You must select such an object list in the dropdown list box, which is displayed in the navigation area under the buttons. In our screenshot you see the local objects (2) of user bcuser. As we have not created any local objects by now, the area that shows these local objects is still empty (4). By choosing another entry in the dropdown list, you can see other objects such as package, programs, function groups etc. The whole list is shown in the screenshot (3).
You can view a package plus all its elements also in the SE80. Just choose the entry Package in the object list of the Repository Browser, enter the name of the relevant package in the input field below and press "Enter". Then all the objects of that package are displayed.
A Package for Local Objects
"Hello World" as a local programz_hello_word_local glasses
Here you see a very helpful feature of the ABAP development environment. If you want to edit an object that does not exist, in general the system asks you if you want to create the respective object. We confirm and get to the next dialogue window.
By the way, you should note and always remember as from now that repository objects created for customers in customer systems have different naming conventions than those that apply to Sap's own programs: A customer must use "Y" or "Z" for the first letter, or the abbreviations reserved by SAP for the customers company. Our Trial Version is set up as a customer system in which the user BCUSER is registered as a customer. Therefore the names of the objects we create must start with an "y" or "z".
In the next dialog window we deselect the item "With TOP INCL", confirm, and get to the next dialog window.
In here you can define the properties of the new program. This is important, since the program properties determine the execution of the program in the ABAP runtime environment. One of the most important program properties of an ABAP program is its type. From the list provided, we want to select the type "Executable program" for our simple 'hello world' program, and change or add no other properties. In particular, ensure that the properties "Unicode checks active" and "Fixed point arithmetic" should always be checked.
In the next dialog window we select "Local Object" and, thus, do not need to input a package name. And there we are. Now we have done everything we need to input the code of our first program.
Note that in our tips we will be using the new frontend editor available as of SAP NetWeaver 2004s, which like all modern editors supports syntax highlighting and so on. To configure this editor, select Utilities - Settings - Frontend Editor (new). To configure the editor for your own preferences, you can select the icon in the lower right-hand corner. Up to and including Release 6.40 (SAP NetWeaver 04), you will still need to use the old frontend, which provides a lot less support for programming.
To go on, we double-click the program name in the object list of the SE80.
On the right the editor opens. In it you see the framework predefined by the ABAP Workbench. This syntax consists of a few lines of comments, and also contains an initial statement named Report.
The first seven lines are comment lines, and the eighth line is a statement.
Comment lines are introduced by an asterisk * in the first position. The rest of the line is arbitrary and is shown in a different color by the editor. To mark only the final portion of a line as a comment, you can use the character ".
As we want to input some code, we have to change the program. In order to do this we have to go to the change mode by pressing the respective button:
Below the report-statement we enter:
Admittedly, this listing needs some beautifying. The tool to do this is the Pretty printer. We configure the Pretty Printer at Utilities-Settings-ABAP Editor-Pretty Printer and select Convert Uppercase/Lowercase and below Keyword Uppercase. Next we press the Button Pretty Printer and our program now looks a bit nicer as all the keywords are now in capitals.
To run the program we press the icon:
Now let us spend some words on the syntax of this little program. Though this program is as simple as could be, I still want to add some comments on the general way ABAP programs are structured.
Every standalone ABAP program, that is, all types of programs except for so-called include programs, start with an introductory statement. In our first program, the introductory statement is REPORT. This designation is historical and expresses the fact that an executable program was once exclusively used for reporting. Here, however, it suffices to know that our first statement introduces the program z_hello_world_local.
After the introductory statement, every ABAP program follows a fixed program structure that divides it into two parts:
- a global declaration part
- a procedural part
The keywords and statements used in this program do not need many explanations. We declare a field of type string, assign a value to, and output it.
Note some important information on the write-statement. The write statement outputs a list and it is driven by the classic SAP Dynpro technology which should not be used to write to used to write applications with an state-of-the-art user interface. You should use Dynpro-based UIs only in test-programs if you want to output (or input) some values in a quick and dirty way.
You can compare the write-statement to the output to the console in Java. Just the same way you should not use the write-statement to output something in an application proper. The right way to do this is to use a Web Dynpro ABAP application as a user interface and to encapsulate all the logic of your programs in function modules or classes. Still, we will use the write-statement sometimes in our tip series in order to output some results or to input some values a program needs. And this is the way, you should use this classic, but by now obsolete UI technology yourself: Use it (only) to test your business logic separately from the presentation logic.
In order to show you, how the syntax check works in ABAP, let us change the Keyword DATA to DTA and press the Check Icon.
At the bottom of the editor we get the information:
This check gives you precise information as to in which line the error is. In case of a slip of pen like this, the system can make a reasonable guess as to what you wanted to type in, and proposes an semi-automatic correction. By pressing the Correct Errors button (marked by the red-dotted frame) you can accept the proposal and your syntax error gets corrected.
Next we save our program by pressing the Save button:
The real storage of the program in the database is done by the system. You need not worry about it. All you need to retrieve the program is the name. So there is no fumbling with files on the Web AS ABAP.
If you remember the tip about the activation of programs, you probably know that this program is still not visible to other users. Why? There is no active version of this program. A version of program that is not activated is only visible to the developer of this program.
In order to make our program visible in the whole system, we activate the it by pressing the Activate button:
Now our program is visible to every user in the system.
- You know that you need a package for every development object. If your objects does not need to be transported to another system the package $tmp for local development will do.
- You have seen how to find your way in the Repository browser of the SE80 and how easy you can create a new program. Just input the name of a new object and the system asks you if you want to create it.
- You are able to save, activate, run, or check a program for syntactical correctness
Manfred Lutz is in Netweaver Product Management with focus on Application Servers.
This content is reposted from the SAP Developer Network.
Copyright 2007, SAP Developer Network
SAP Developer Network (SDN) is an active online community where ABAP, Java, .NET, and other cutting-edge technologies converge to form a resource and collaboration channel for SAP developers, consultants, integrators, and business analysts. SDN hosts a technical library, expert blogs, exclusive downloads and code samples, an extensive eLearning catalog, and active, moderated discussion forums. SDN membership is free.