After you complete this chapter, you should be able to:
Before proceeding, you should:
Many of the procedures in this book are demonstrated using ScreenCams. ScreenCams are like movies; they show a series of screens, including keystrokes and mouse movements, with a descriptive voiceover. They can be found on the CD-ROM that comes with this book. See the readme.txt file found in the root directory of the CD-ROM for more information.
A development object is anything created by a developer. Examples of development objects are programs, screens, tables, views, structures, data models, messages, and includes.
The R/3 system contains tools for creating and testing development objects. These tools are located in the R/3 Development Workbench. To access any development tool, you go to the workbench.
The workbench contains these tools to help you create development objects:
The following testing and search tools are also available:
All development objects are portable, meaning that you can copy them from one R/3 system to another. This is usually done to move your development objects from the development system to the production system. If the source and target systems are on different operating systems or use different database systems, your development objects will run as-is and without any modification. This is true for all platforms supported by R/3. (For a list of supported hardware and operating systems, refer to Table 1.1.)
There are two main types of ABAP/4 programs:
The purpose of a report is to read data from the database and
write it out. It consists of only two screens (see Figure 2.1).
Figure 2.1:The selection screen and the output screen.
The first screen is called the selection screen. It contains input fields allowing the user to enter criteria for the report. For example, the report may produce a list of sales for a given date range, so the date range input fields would appear on the report's selection screen.
The second screen is the output screen. It contains the list. The list is the output from the report, and usually does not have any input fields. In our example, it would contain a list of the sales that occurred within the specified date range.
The selection screen is optional. Not all reports have one. However, all reports generate a list.
In this book, you will learn how to create report programs.
Dialog programs are more flexible than reports, and so are more complex at the program level. They can contain any number of screens, and the screen sequence can be changed dynamically at run time. On each screen, you can have input fields, output fields, pushbuttons, and more than one scrollable area.
ABAP/4 reports consist of five components (shown in Figure 2.2):
Figure 2.2.The components of an ABAP/4 program.
Only the source code and program attribute components are required. The rest of the components are optional.
All development objects and their components are stored in the R/3 database. For example, the source code for a report is stored in database table dd010s.
ABAP/4 programs are interpreted; they are not compiled. The first time you execute a program, the system automatically generates a runtime object. The runtime object is a pre-processed form of the source code. However, it is not an executable that you can run at the operating system level. Instead, it requires the R/3 system to interpret it. The runtime object is also known as the generated form of the program.
If you change the source code, the runtime object is automatically regenerated the next time you execute the program.
The company you work for is a customer of SAP. Therefore, programs that you create at your company are called customer programs.
Customer development objects must follow naming conventions that are predefined by SAP. These conventions are called the customer name range. For programs, the customer name range is two to eight characters long and the program name must start with the letter y or z. SAP reserves the letters a through x for their own programs.
Please take a moment now to choose a unique 3-character identifier for your programs. Within this book, I'll refer to this identifier as your handle. It must begin with a y or z. For example, you might use the letter z followed by your two initials. The notation will indicate where you should use your handle. For example, if you chose zkg and you see the direction "Enter a program name of abc," you would enter zkgabc. I recommend that as you go through this book, you should use your handle as the first three characters of all development objects you create. If you do, they will be easy to recognize later, and easy to find.
The program naming conventions adopted for this book are as follows:
The setup program that creates development objects and loads them with data for the exercises is called y-setup. If necessary, it can be re-run at any time to restore the exercise data to its original condition. To remove all the development objects and data created by the setup program from the system, run y-uninst. See the readme.txt file on the CD-ROM for more information.
What follows is a description of the process you will follow to create a program.
When you sign on to R/3 to create your first ABAP/4 program, the first screen you see will be the SAP main menu. From there, you will go to the Development Workbench, and then to the editor. You will enter a program name, and create the program. The first screen you will see will be the Program Attributes screen. There, you must enter the program attributes and save them. You will then be allowed to proceed to the source code editor. In the source code editor, you'll enter source code, save it, and then execute the program.
Start the ScreenCam "How to Create Your First Program" now.
Follow this procedure to create your first program. Help with common problems is given in the Troubleshooter that follows it. If you have trouble with any one of the steps, don't forget to consult the Troubleshooter.
Listing 2.1 Your First ABAP/4 Program
1 report ztx0201. 2 write 'Hello SAP World'.
The code in Listing 2.1 produces this output:
Hello SAP World
Congratulations, you have just written your first ABAP/4 program! To return to the editor, press the green arrow button on the standard toolbar (or the F3 key).
When you press the Create button, you get a dialog box saying Do Not Create Objects in the SAP Name Range. | You have entered the wrong program name. Your program names must start with y or z. Press the Cancel button (the red X) to return and enter a new program name. |
When you press the Create button, you get a dialog box with an input field asking for a key. | You have entered the wrong program name. Your program names must start with y or z. Press the Cancel button (the red X) to return and enter a new program name. |
You are getting a Change Request Query screen asking for a Request Number. | On the Create Object Catalog Entry screen, do not enter a value in the Development class field. |
Press the Local Object button instead. |
In this section you will learn how to harness the power of the ABAP/4 editor. You will learn to use two screens in this section:
The ABAP/4 Editor: Initial Screen is shown in Figure 2.3. From there, you can display or change all program components. For example, to change the source code component, choose the Source Code radio button and then press the Change button. Or, to display the attributes component, choose the Attributes radio button and then press the Display button.
Figure 2.3.From the ABAP/4 Editor: Initial Screen you can display or change program com-ponents.
TIP |
In Figure 2.3, notice that the Object Components group box encloses radio buttons, the Display button, and the Change button. When you see a group box enclosing both radio buttons and pushbuttons, the radio buttons deter-mine the component acted upon by the enclosed pushbuttons. The effect of the radio buttons is limited by the group box; they have no effect on push-buttons outside the box. |
Pressing the Change button displays the selected component in change mode, which enables you to change the component.
From the ABAP/4 Editor: Initial Screen, choose the Source Code
radio button and press the Change button. The ABAP/4 Editor: Edit
Program screen is shown, as in Figure 2.4.
Figure 2.4.Use the ABAP/4 Editor: Edit Program screen to change com-ponent functionality.
TIP |
Many developers find the R/3 user interface complex and therefore difficult to learn. I suggest that you adopt a methodical approach that enables you to become comfortable with each new screen. Whenever you come across a new screen, slowly scan the menu items and buttons. Begin at the top left corner of the screen and work down and to the right. Hold your cursor over each item long enough to read it and its accompanying ToolTip. Taking a few minutes on each new screen will help you become familiar with the functions available. |
Start the ScreenCam "Exploring the Standard Toolbar" now.
The Standard toolbar controls are shown in Figure 2.5.
Figure 2.5.These are the controls on the Standard tool-bar within the ABAP/4 editor.
The Standard toolbar controls (refer to Figure 2.3), in order, are:
DO save your program before entering a /n command in the command field. Otherwise, you will lose your changes. | DON'T close the editor window by pressing the X button in the top right-hand corner of the window. You will lose unsaved changes. |
Start the ScreenCam "Using Find and Replace" now.
Press the Find button on the Standard toolbar and the system will display the Search/Replace screen (see Figure 2.6). Enter the string you want to find into the Find field.
There are several options on this screen to control the find process:
The Find field has some unusual qualities:
Start the ScreenCam "Setting the Scope of a Search or Replace Function" now.
To set the scope for a search or replace function, follow these steps:
Start the ScreenCam "How to Find All Occurrences of a String" now.
To find all occurrences of a string:
Start the ScreenCam "How to Search and Replace in the Source Code" now.
To search and replace in the source code:
Start the ScreenCam "How to Search and Replace Via a Summary Screen" now.
To search and replace via a summary screen:
If you replaced any strings, you must save your changes before returning to the source code editor. To do so, press the Save button in the Standard toolbar, and then press the Back button. If you want to cancel your changes, press the Cancel button instead of the Save button.
Start the ScreenCam "Exploring the Application Toolbar of the ABAP/4 Editor" now.
Before proceeding, maximize your window (if it is not already). You may not see the buttons at the far right of the Application toolbar if your window is smaller than the maximum size.
The Application toolbar controls are shown in Figure 2.7.
Figure 2.7.The Application toolbar controls.
The Application toolbar controls, in the order they appear on the toolbar, are as follows:
TIP |
To bring a line to the top of the editor window, double-click between words or at the end of the line. Don't double-click on a word though; that won't work. An alternative method is to place your cursor at the end of a line and press the F2 key. |
In most Windows applications, there is one Clipboard for cut and paste operations. In R/3, there are five clipboards. Table 2.1 describes them all.
The Buffer | Cut and paste within a program | Press the Cut or Copy buttons in the editor | Press the Insert From Buffer button |
The X,Y,Z Buffers | Cut and paste between two programs | Menu path: Block/ Buffer->Copy to X Buffer | Menu path: Block/Buffer->Insert X Buffer |
The Clip- board | Cut and paste between:
- two R/3 sessions | Menu path: Block/Buffer-> Copy to Clipboard | Menu path: Block/Buffer->Insert from Clipboard to insert the Clipboard contents as new lines into the |
- R/3 and other Windows apps (e.g. Notepad) | program at the cursor position. Or Ctrl+V to paste over top of the existing lines. With Ctrl+V, data will be truncated if it won't all fit on the current screen. | ||
- copy code from F1 help to the editor editor - copy more than one line of code | 1- Click once 2- Ctrl+Y 3- Drag and mark 4- Ctrl+C |
In the first row of Table 2.2 is program-specific buffer, simply called the buffer. It lets you copy within a program. To use it, place your cursor on a line or mark a block and press the Cut, Copy, or Insert From Buffer buttons. The buffer's contents are lost when you leave the editor.
The X, Y, and Z buffers are used to copy code from one program to another. Use the Block/Buffer menu to access them. Although they are three separate buffers, they are all used in the same way. Their contents are retained after you leave the editor, but they are lost when you log off.
The Clipboard is the same as the Windows Clipboard. Use it to cut and paste into other Windows applications such as MSWord or Notepad. It is also used to copy text from F1 help screens (see the following section titled "Getting Help").
In addition to the buttons on the Application toolbar and the menus, you can use standard Windows functions to perform cut and paste operations. To highlight a word or line, drag your cursor across it, or hold down the Shift key and tap the left or right arrow keys (you can only highlight one line at a time using this method). To highlight multiple lines, click once on the screen with the mouse and then press Ctrl+Y. The pointer will change to crosshairs. Drag the crosshairs to highlight the section you want to copy. Press Ctrl+C to copy the highlighted text to the Clipboard, or press Ctrl+X to cut. Paste from the Clipboard using Ctrl+V.
Table 2.2 contains a summary of commonly used editor functions.
All functions are accessed from the ABAP/4 Editor: Edit Program
screen unless otherwise noted.
Get help on the editor | Choose the menu path Help->Extended Help. |
Get help on any ABAP/4 keyword | Put your cursor on a keyword within the code and press F1. |
Save your program | Press F11. |
Execute your program | Press F8. |
Display function keys | Right-click anywhere in the screen area of a window. |
Move the cursor to the command field | Press Ctrl+Tab. |
Bring any line to | Double-click on whitespace (at the end of the line), or place your |
the top of the screen | cursor on whitespace and press F2. |
Insert a line | Press the Enter key with your cursor at the beginning or end of a line. |
Delete a line | Put your cursor on the line and press the Cut button. |
Mark a block of code | Place your cursor on the first line of the block and press F9 (select). Then place it on the last line of the block and press F9 again. |
Delete a block of lines | Select the block and press the Cut button. |
Repeat a line or block | Press the Duplicate Line button. |
Split a line | Position your cursor at the split point and press the Enter key. |
Join two lines together | Position your cursor at the end of the line and press the Concatenate button. |
Copy lines within the current program | Mark a block. Press the Copy To Buffer button. Then position your cursor at the point where the code should be inserted, and press the Insert From Buffer button. |
Copy lines to another program | Mark a block. Choose the menu path Block/Buffer->Copy to X buffer. Open a new program and choose the menu path Block/Buffer ->Insert X buffer. (You can also use the Y and Z buffers the same way.) |
Copy to or from the Windows Clipboard | Mark a block. Choose the menu path Block/Buffer->Copy to clip-board. Then choose the menu path Block/Buffer->Insert clipboard. |
Comment out a block of lines | Mark a block, and choose the menu path Block/Buffer->Insert comment |
Uncomment a block of lines | Mark a block, and choose the menu path Block/Buffer->Delete comment. |
Print your program | Press the Print button. |
Print your program's output | While viewing the output, choose the menu path System->List->Print. |
Find and repeat find Shift code left and right | Press the Find and Find Next buttons. Put your cursor on the line to be moved, in the column you want to move it to. Then press the Move Line button (F6). To move a block, position your cursor on the first line and then press the Move Line button. |
Undo the last change | Press the Undo button. There is only one level of undo. |
Automatically format source code | Choose the menu path Program >Pretty Printer. |
Download a program to a file on your PC | Choose the menu path Utilities->Download. |
Upload a program from a file on your PC | Choose the menu path Utilities->Upload. |
Save a temporary copy of your program | Choose the menu path Program->Save Temp. Version. The temporary copy is deleted when you save the program. |
Retrieve the temporary copy of your program | Choose the menu path Program->Get Temp. Version. You can retrieve the saved copy any number of times until you save the program. |
Jump directly to the editor | From any screen, type /nse38 in the command field and press Enter. |
Copy a program | From the ABAP/4 Editor: Initial Screen, choose the menu path Program->Copy. |
Rename a program | From the ABAP/4 Editor: Initial Screen, choose the menu path Program->Rename. |
Delete a program | From the ABAP/4 Editor: Initial Screen, choose the menu path Program->Delete. |
Save your changes under a new program name | While editing a program, choose the menu path Program->Save As. |
Display two programs at the same time | Starting on any screen, open a new window using the menu path System->Create new session. In the new window, go to the ABAP/4 Editor: Initial Screen, enter the second program name, and press the Display or Change button. |
Compare two programs for differences | From the ABAP/4 Editor: Initial Screen, choose the menu path Utilities->Splitscreen editor. Enter two program names and press the Display button. To display the first difference, press the Next Difference button. To align both programs at the next identical line, press the Align button. |
Compare two programs on different systems | From the ABAP/4 Editor: InitialScreen, choose the menu path Utilities->Splitscreen editor. Press the Compare Diff. Systems button. Enter two program names and a system id and press the Display button. |
Save a version of a program | From inside the editor, choose the menu path Program->Generate version. The current program is saved in the version database. |
Retrieve a program version | From the Editor Initial screen, choose the menu path Utilities->Version management. The Versions Of Object Of Type REPS screen is displayed. Deselect the active version and tickmark the version to restore. Press the Retrieve button and then press the Back button. Press the Yes button and the current version becomes the -1 generation, and a copy of the selected version becomes the current version. |
Compare program versions | From the Editor Initial screen, choose the menu path Utilities ->Version management. The Versions Of Object Of Type REPS screen is displayed. Tickmark the versions you want to compare and press the Compare button. The Compare Programs: All screen is displayed. Scroll down to view the differences. |
Print your program | Choose the menu path Program->Print. On the next screen, specify a printer and tickmark the Print Immed. check box. |
Start the ScreenCam "Getting Help" now.
For a complete tutorial on the editor:
There are two basic types of help in the editor, F1 help and R/3 Library help. F1 help is also known as the ABAP/4 keyword documentation.
F1 help describes the syntax of ABAP/4 keywords and gives examples of their use. It is text-based and resides in tables within the R/3 database.
R/3 Library help is far more extensive and contains overviews of and procedures for creating development objects. It is Windows-based help and resides outside of the R/3 database, usually on a CD-ROM.
F1 help is useful for looking up syntax and often contains useful code samples.
To obtain F1 help:
Within F1 help, highlighted words are hypertext links. Clicking on them takes you to more information.
TIP |
You can use the FindIt utility on the CD-ROM to find even more code samples. |
Within the help there are often code samples. To cut and paste them into your program, press Ctrl+Y, and then mark a block of code by dragging your cursor across it from the top left-hand corner to the bottom right-hand corner. Let go of the mouse and press Ctrl+C. Press the Back button to return to the ABAP/4 Editor: Edit Program screen and paste into your program using Block/Buffer->Insert Clipboard. Or you can paste over top of your code using Ctrl+V. (Ctrl+V doesn't paste into the source, it pastes onto the screen, so it won't paste past the bottom of the visible page.)
R/3 Library help is stored in Windows help files. To view it:
Additionally, help can be obtained from the ABAP/4 Editor: Initial Screen. To view it:
Help can also be obtained from within the editor while you are editing the source code. Choose the menu path Utilities->Help on. Here, you can get the following types of help:
Start the ScreenCam "Finding Your Development Objects" now.
To display all of the development objects you have created in the R/3 system:
You are now in display mode on the selected object. You can press the Display <-> Change button to switch into edit mode.
NOTE |
This method only works for objects that have been saved as local objects. They will be displayed here only if you pressed the Local Object button on the Create Object Catalog Entry screen when you created the object. |
If you are looking for a program and you know the first few characters of its name, you can find it from the ABAP/4 Editor: Initial Screen. To do this:
The R/3 Data Dictionary (or DDIC for short) is a utility for defining data objects. Using the DDIC, you can create and store objects such as tables, structures, and views. To invoke the Data Dictionary, perform these steps:
Figure 2.8.The Dictionary: Initial Screen.
The DDIC is within the R/3 system. You can think of it as sitting on top of a database such as Oracle or Informix and acting like a remote control, generating and sending SQL statements to it. For example, you might create a table definition in the DDIC. When you activate the table definition, SQL statements are generated and sent to the RDBMS, causing it to create the actual table in the database. When you want to modify the table, you must modify the table definition in the DDIC. When you activate the table again, more SQL is generated causing the RDBMS to modify the table.
you must modify the table definition in the DDIC. When you activate the table again, more SQL is generated causing the RDBMS to modify the table. | DO use the DDIC to create and modify all database objects. |
DON'T modify a table or anything else at the RDBMS level. The Data Dictionary definition cannot update itself and it will be out of sync with the database. This can cause application errors and even lead to a loss of data integrity.
In R/3 a table is a collection of rows. Each row contains fields, also called columns. Normally, within a table, each row has the same number of columns as the other rows of the table.
A table holds persistent data. In other words, if you put data into a table, it remains there after your program ends. It will remain there until it is changed or deleted by your program or another program. The name of a table is unique within the entire R/3 system.
When you look at a table in the DDIC, you are looking at the description of a table in the underlying database. You are not looking directly at the database table itself. Figure 2.9 shows how a table definition appears in the R/3 DDIC.
Figure 2.9.The DDIC definition for table lfa1.
In R/3 a structure is a description of a group of fields. It describes the field names, their sequence, and their data types and lengths. Each structure has a name that is unique within the entire R/3 system. A structure cannot have the same name as a table.
A structure name can be used in two ways:
Strictly speaking, a structure is something that only exists within the R/3 Data Dictionary. If you see the word structure, you should immediately think "DDIC structure." However, some SAP documentation uses the word structure to also refer to a group of variables within a program. The structure emara is shown in Figure 2.10 as an example of how a structure appears in the DDIC.
Figure 2.10.The DDIC definition for structure emara.
In R/3, tables and structures are both defined in the DDIC. You will notice however, when viewing them, there is very little difference between them. That is because in R/3, a table is just a description of an actual database table. It is the structure of the table in the database. So, both tables and structures within the DDIC define a layout-a series of fields. The major difference between the two is that a table has an underlying database table associated with it. A structure does not.
Some people find the R/3 documentation confusing at times, since SAP occasionally uses these two terms interchangeably.
To display a table or structure definition in the DDIC:
What you see in Figure 2.9 is the structure of table lfa1 in the R/3 Data Dictionary, not the actual database table. In R/3, only a table's structure can be defined, along with some attributes such as the primary key fields.
R/3 also provides a utility to enable you to display data that exists within a table. To use it:
Figure 2.11.The Data Browser: Table Select Entries screen.
The number of records displayed is limited by the value in the Maximum No. Of Hits field on the Data Browser: Table: Selection Screen.
CAUTION |
Don't blank out the Maximum No. Of Hits field to display all records unless you actually intend to look through the entire list. For large tables, this can consume a lot of CPU on both the database and application servers and also significantly increase network traffic, thereby slowing down the system. If such a large report is needed, it should usually be run in batch. The preferred approach is to narrow down your search by entering values into the fields on the Data Browser: Table: Selection Screen. This will be covered in detail in a later chapter. |
You can control which fields are displayed on the Data Browser: Table: Selection Screen. To do so:
Before continuing, you should now run the exercise setup utility supplied on the CD-ROM. It will create the tables and data needed for the exercises in this book. For instructions, see the readme.txt file located in the root directory of the CD-ROM.
The select statement retrieves records from the database.
The following code shows simplified syntax for the select statement:
select * from t1 [into wa] [where f1 op v1 and/or f2 op v2 ...] [order by f1]. (other abap/4 statements) endselect.
where:
* indicates that all fields in the table should be retrieved.
t1 is the name of a table previously defined on a tables statement.
wa is the name of work area that matches the structure of the table.
f1 is the name of a field in table t1.
op is one of the following logical operators: = <> > >= < <=.
v1 is a literal or variable.
and/or is either the word and or the word or.
Create a new program and name it 0202. Enter the code
as shown in Listing 2.2.
Listing 2.2 Your Second Program
1 report ztx0202. 2 tables ztxlfa1. 3 select * from ztxlfa1 into ztxlfa1 order by lifnr. 4 write / ztxlfa1-lifnr. 5 endselect.
The code in Listing 2.2 should produce this output:
1000 1010 1020 1030 1040 1050 1060 1070 1080 1090 2000 V1 V10 V11 V12 V2 V3 V4 V5 V6 V7 V8 V9
NOTE |
Lfa1 is the vendor master table in R/3. Ztxlfa1 was created by the CD-ROM setup routine and is similar to lfa1, but is used for the exercises in this book. The field lifnr is the vendor number field. |
This program reads all the records from table ztxlfa1 and writes out the contents of the field lifnr (the vendor number) in ascending order.