How to Create an ABAP Program?
Process of creating and running a simple ABAP program. In short this involves
(i) specifying a set of program attributes,
(ii) entering the source code,
(iii) syntax checking the code and then
(iv) running the program.
Creating an ABAP Program
To create an ABAP program, you need to access the ABAP Editor.
To access the ABAP Editor through Object Navigator, go to Object Navigator initial screen by double-clicking on Object Navigator under the Overview folder. From the Object Navigator initial screen, you will be able to access all the permitted objects such as functions, ABAP programs, development classes, etc). To choose the type of object to access, in this case an ABAP program, select “Program” from the top field in the Object Navigator frame. Another input field will be shown below the first field. Enter the name of the ABAP program you want to access or create in this field (for example Z######) and click on “Display”. If the program does not exist, the system will prompt you with a dialog box to ask if you would like to create the program.
To access the ABAP Editor directly under the Development folder, double-click on ABAP Editor. You will be directed to the ABAP Editor initial screen. In the input field, specifies the name of the program (for example, Z######), and then click on “Display” if it is an existing program, or “Create” if it is a new program. (If you click on Display and the program does not exist, a status message will appear indicating that the program does not exist).
NOTE: Program names must start either with Z or Y.
In either case, you will enter an editing mode to create an ABAP program. An ABAP program consists of two basic parts: the attributes and the source code. The first step in creating an ABAP program is to set the attributes:
Setting the Program Attributes
1. Enter the attribute values on the Program Attributes screen as follows:
In the Title box enter My First ABAP Program as the title.
- In the Type box select “Executable Program”.
- In the Status box select “Test Program”.
- In the Application box select “Basis”.
2. Click on the Save button in the window to save the program attributes.
3. The Create Object Directory Entry dialogue box appears. Click on the Local Object button to indicate that the program can only be used in the current system, and then you will be presented with the source code editor.
Creating the Program Source Code
1. The system has generated the report declaration statement containing the name you specified for your program. Leave a blank line after the report statement and enter the following code:
write ‘Customer list’.
2. Save the source code by clicking on the Save button in the standard tool bar. It is important to save your program before trying to execute it. Executing without saving will cause a runtime error and possibly result in the loss of your program code!
Working in the Editor
The ABAP Editor works much like the basic word processors:
You can select a block of text by positioning the cursor at the beginning of the block, click and hold on the left mouse button, and drag the cursor over the block of code to be selected.
You can cut and paste the selected block of code by using the cut, copy, and paste buttons in the Editor toolbar or by clicking on the right mouse button and then selecting the appropriate action.
To cut or copy a single line, position the cursor on the beginning of the line and click on the left mouse button.
- Ctrl-C to copy text to the clipboard
- Ctrl-X to cut text to the clipboard
- Ctrl-Y to paste text from the clipboard
Note that the contents of the internal buffer are lost when you exit the Editor. If you want to copy a marked block of source lines from one program to another, copy them first into the X, Y, and Z buffers, and or Clipboard, which are accessible via the menu choice Utilities->Block/Buffer.
Help Information
The Editor provides several features to help you perfect your code. Place the cursor on the language keyword and click on the ABAP Help button or press F1, or click on the ABAP Help button and type in the keyword in the ABAP Key Word text box in dialogue box that appears. In both cases, you will get a detailed explanation for that command and all its variations.
Syntax Check
When you click on the Check button in the Editor screen, syntax of the current program is checked and syntax errors will be sequentially indicated and displayed in a box that pops up on the screen, and if possible, the Syntax Check will propose a correct version. You can automatically substitute the correct version by clicking on Correct at the bottom of the screen. If the program is correct, or after you fixed all the errors, you will get a confirmation message.
Testing a Program
1. Perform the Syntax Check on your program as described above. If the program has no errors, a message appears at the bottom of the Editor screen, which says “Program Z###### is syntactically correct”.
2. Execute the program from the Editor screen by selecting Program->Test from the menu bar.
3. The literal ‘Customer list’ in the write command appears on the screen. Note that the system automatically displays the program title as the list header.
Activating a Program
Once you are satisfied that a program is working as intended and you would like to make the latest version available for authorized users, you have to activate the program by clicking on the Activate button on the application tool bar. Note that making changes to an active program and saving the changes will automatically render the latest version inactive. When this occurs, there will be two programs of the same name in the system: an inactive version that has the latest changes incorporated into it, and an active version, which has not yet included the latest changes. You can toggle between an active and inactive version of a program by clicking on the Active<->Inactive button in the application tool bar to see what was the latest changes you have incorporated since you last activate the program. To make the latest version an active version (and therefore discarding the previous active version), simply activate the latest program.
Creating a Basic List (of output)
Go back to the editor screen and continue editing the Z###### program. We are going to add a few lines of code to create a basic customer list from the SCUSTOM table. Make the following additions to the code of your program:
1. After the report statement add a new line:
tables scustom.
2. After the write statement add the following lines:
select * from scustom.
write: / scustom-id, scustom-name, scustom-city, scustom-postcode.
endselect.
3. Save, Syntax check and Execute your program.
Note that the title of the program that you entered in the attributes screen appears as the title of the report. The literal ‘Customer List’ appears as the first line of the report. The body of the report is made up of the rows from the SCUSTOM table.
Adding Column Headings to the List
Go back to the editor screen and continue editing the Z###### program. Make the following additions to the code of your program:
1. Alter the write ‘Customer List’. statement so that it reads:
write:/ text-001, text-002, text-003, text-004.
2. Check, Save and Execute your program. Note that the ‘Customer List’ heading does not appear but neither does anything else.
3. Go back to the editor and double click on the text-001. The system responds with the message ‘Text element 001 does not exist it. Do you want to create it? Answer Yes and go to the Text Elements screen.
4. Enter the text Id in the Text column next to the 001 identifier.
5. Execute the program to see the effect of defining the text element.
6. Repeat the process to add appropriate text for the other text elements.
Note that after doing this the column headings are still less than satisfactory. They are not necessarily lined up over the relevant columns and do not appear different to the detail lines. There are 3 ways to define column headings for a list. The first way is to position the header and the output at specific columns using appropriate ABAP statements in the program (something which we will learn in 2 weeks’ time). The second way is to define it through the editor through the Text Elements Screen by selecting Goto->Text Elements->List Headings from the menu bar. The third way is to define it interactively from the List Header screen at runtime by:
1. Executing the report.
2. Choose System->List->List Header from the menu bar. The system displays 4 lines where column header text can be entered. Enter the column heading text and click on Save. The report is displayed but without the just defined column headers.
3. Return to the editor and execute the report again. This time the column headers appear in the appropriate place. Note that they are a different colour to the report body.
NOTE: In order to define the list header interactively, you will first need to make the program active by activating it.