Introduction to Programming SAP Applications Using 1726349907
Introduction to Programming SAP Applications Using 1726349907
iv
Viewing the Data in a Table 51
Chapter 3 55
Creating a Program 55
Code Editor 57
Write Statements 62
Output Individual Fields 71
Chaining Statements Together 72
Copy Your Program 73
Declaring Variables 75
Constants 78
Chapter 4 79
Arithmetic – Addition 79
Arithmetic – Subtraction 80
Arithmetic – Division 81
Arithmetic – Multiplication 81
Conversion Rules 82
Division Variations 83
The standard form of division. 83
The integer form of division. 83
The remainder form of division. 84
Chapter 5 – Character Strings 85
Declaring C and N Fields 85
Data type C. 85
Data type N. 86
String Manipulation 87
Concatenate 87
v
Condense 88
NO-GAPS 89
Find the Length of a String 89
Replace 90
Search 90
SEARCH Example 1 91
SEARCH Example 2 91
SEARCH Example 3 92
SEARCH Example 4 92
Shift 93
Split 94
SubFields 96
Chapter 6 – Debugging Programs 98
Fields mode 102
System Variables 103
Table Mode 103
Breakpoints 105
Static Breakpoints 107
Watchpoints 108
Ending a Debug Session 111
Chapter 7: Working with Database Tables 113
Making a Copy of a Table 113
Add New Fields 116
Foreign Keys 117
Append Structures 122
Include Structures 124
vi
Key Fields 127
Deleting Fields 130
Deleting Tables 133
Chapter 8 – Working with Other Data Types 136
Date and Time Fields 136
Date Fields in Calculations 138
Time Fields in Calculations 141
Quantity and Currency Fields in Calculations 142
Chapter 9 – Modifying Data in a Database Table 146
Authorisations 146
Fundamentals 146
Database Lock Objects 148
Using Open SQL Statements 149
Using Open SQL Statements – 5 Statements 150
Insert Statement 151
Clear Statement 155
Update Statement 157
Modify Statement 158
Delete Statement 160
Chapter 10 – Program Flow Control and Logical Expressions 164
Control Structures 164
If Statement 164
Linking Logical Expressions Together 169
Nested If Statements 169
Case Statement 170
Select Loops 171
vii
Do Loops 172
Nested Do Loops 175
While Loops 178
Nested While Loops 179
Loop Termination – CONTINUE 180
Loop Termination – CHECK 181
Loop Termination – EXIT 182
Chapter 11 – Selection Screens 184
Events 184
Intro to Selection Screens 185
Creating Selection Screens 186
At Selection Screen 187
Parameters 188
DEFAULT 189
OBLIGATORY 190
Automatic Generation of Drop-Down fields 190
LOWER CASE 191
Check Boxes and Radio Button Parameters 192
Select-Options 193
Select-Option Example 196
Select-Option Additions 200
Text Elements 200
Variants 203
Text Symbols 209
Text Messages 211
Skip Lines and Underline 216
viii
Comments 218
Format a Line and Position 219
Element Blocks 221
Chapter 12 – Internal Tables 223
Introduction 223
Types of Internal Tables 224
Standard Tables 224
Sorted Tables 225
Hashed Table 225
Internal Tables - Best Practice Guidelines 225
Creating Standard and Sorted Tables 226
Create an Internal Table with Separate Work Area 227
Filling an Internal Table with Header Line 228
Move-Corresponding 232
Filling Internal Tables with a Work Area 234
Using Internal Tables One Line at a Time 235
Modify 236
Describe and Insert 236
Read 238
Delete Records 239
Sort Records 240
Work Area Differences 241
Loops 241
Modify 242
Insert 242
Read 242
ix
Delete 242
Delete a Table with a Header Line 243
CLEAR 243
REFRESH 243
FREE 243
Delete a Table with a Work Area 244
Chapter 13 – Modularizing Programs 245
Introduction 245
Includes 246
Procedures 249
Sub-Routines 250
Passing Tables 254
Passing Tables and Fields Together 255
Sub-Routines - External Programs 256
Function Modules 257
Function Modules – Components 258
Attributes Tab 262
Import Tab 262
Export Tab 263
Changing Tab 263
Tables Tab 263
Exceptions Tab 263
Source Code Tab 264
Function Module Testing 264
Function Modules - Coding 267
x
SAP SYSTEM OVERVIEW
This diagram shows the 3-tier Client/Server architecture of a typical SAP system:
15
SAP SYSTEM OVERVIEW
At the top is the Presentation server, which is any input device that can be used to control
an SAP system (the diagram shows the SAP GUI, but this could equally be a web browser,
a mobile device, and so on). The Presentation layer communicates with the Application
server, and the Application server is the 'brains' of an SAP system, where all the central
processing takes place. The Application server is not just one system in itself, but can be
made up of multiple instances of the processing system. The Application server, in turn,
communicates with the Database layer.
The Database is kept on a separate server, mainly for performance reasons, but also for
security, providing a separation between the different layers of the system.
Communication happens between each layer of the system, from the Presentation layer,
to the Application server, to the Database, and then back up the chain, through the
Application server again, for further processing, until finally reaching the Presentation
layer.
A typical Landscape Architecture - Typical here is subjective, in practical terms there is not
really any such thing as a standard, 'typical' landscape architecture which most companies
16
SAP SYSTEM OVERVIEW
The reason for this is fairly simple. All the initial development and testing is done on a
Development system, which ensures other systems are not affected. Once developments
are at a stage where they may be ready to be tested by an external source, or someone
within the company whose role is to carry out testing, the developments are moved, using
what is called a Transport System, to the next system (here, the Testing system).
Normally, no development at all is done on the testing system; it is just used for testing
the developments from the development system. If everything passes through the Testing
system, a Transport system is used again to move the developments into the Production
environment. When code enters the Production environment, this is the stage at which it
is turned on, and used within the business itself.
The landscape architecture is not separated just for development purposes; the company
may have other reasons. This could be the quantity of data in the Production system,
which may be too great to be used in the development environment (normally the
Development and Testing systems are not as large as the Production system, only needing
a subset of data to test on). Also, it could be for security reasons. More often than not,
companies do not want developers to see live production data, for data security reasons
(for example, the system could include employee data or sales data, which a company
would not want people not employed in those areas to see). Normally, then, the
Development and Testing systems would have their own set of data to work with.
The three systems described here, normally, are a minimum. It can increase to four
systems, perhaps with the addition of a Training system, or perhaps multiple projects are
running simultaneously, meaning there may be two separate Development systems, or
Testing systems, even perhaps a Consolidation system before anything is passed to the
Production environment. This is all, of course, dependent on the company, but commonly
each system within the Landscape architecture will have its own Application server and its
own Database server, ensuring platform independence.
17
SAP SYSTEM OVERVIEW
Next, we have the environment which programs run in, the Work Processes, and the
structure of an ABAP program.
Within an SAP system, or at least the example used here, there are two types of programs,
Reports and D p o s.
Reports, as the name would suggest, are programs which generate lists of data. They may
involve a small amount of interactivity, but mainly they supply data to the front-end
interfaces, the SAP GUI and so on. When a user runs a report, they typically get a selection
screen. Once they enter their selection parameters and execute the report, they normally
cannot intervene in the execution of the program. The program runs, and then displays
the output.
D p o s are slightly different. They are dynamic programs, and allow the user to
intervene in the execution of the program, by processing a series of screens, called
18
SAP SYSTEM OVERVIEW
Dialogue screens. The user determines the flow of the program itself by choosing which
buttons or fields to interact with on the screen. Their action then triggers different
functions which have been coded within the flow logic of the program. While reports are
being created, interfaces are also to be generated which are classed as Dynpro s, for all
the selection criteria.
Most of the work done by people involved with ABAP is done within Report programs, and
even though these programs are labelled 'Reports', they do not always generate output.
The Report programs are there to process the logic, reading and writing to the Database,
in order to make the system work.
Work Processes
Every program that runs in an SAP system runs on what are called Work Processes, which
run on the Application server. Work Processes themselves work independently of the
computer's operating system and the Database that it interacts with, giving the
independence discussed earlier with regard to the Technical architecture. When an SAP
system is initially set up, the basis consultants (who install the system, keep it running,
manage all the memory and so on) configure SAP in such a way that it automatically sets
the number of Work Processes programs use when they start, the equivalent of setting up
a pre-defined number of channels or connections to the Database system itself, each of
which tend to have their own set of properties and functions.
The Dispatcher
You might come across something referred to as the Dispatcher. The SAP system has no
technical limits as to the number of users who can log on and use it, generally the number
of users who can access an SAP system is much larger than the number of available Work
Processes the system is configured for. This is because not everybody is sending
instructions to the Application server at exactly the same time. Because of this, users
cannot be assigned a certain number of processes while they are logged on.
The Dispatcher controls the distribution of the Work Processes to the system users. The
Dispatcher keeps an eye on how many Work Processes are available, and when a user
triggers a transaction, the Dispatcher's job is to provide that user with a Work Process to
use. The Dispatcher tries to optimise things as far as possible, so that the same Work
Process receives the sequential Dialogue steps of an application. If this is not possible, for
example because the user takes a long time between clicking different aspects of the
19
SAP SYSTEM OVERVIEW
screen, it will then select a different Work Process to continue the processing of the
Dialogue program. It is the Work Process which executes an application, and it is the Work
Process which has access to the memory areas that contain all of the data and objects an
application uses. It also makes three very important elements available.
The first is the Dynpro processor. All Dynpro programs have flow and processing logic, and
it is the Dynpro processor's job to handle the flow logic. It responds to the user's
interactions, and controls the further flow of the program depending on these
interactions. It is responsible for Dialogue control and the screen itself, but it is important
to remember that it cannot perform calculations; it is purely there to manage the flow
logic of a program.
The next important element is the ABAP processor, which is responsible for the processing
logic of the programs. It receives screen entries from the Dynpro processor, and transmits
the screen output to the program. It is the ABAP processor which can perform the logical
operations and arithmetical calculations in the programs. It can check authorisations, and
read and write to the Database, over the Database Interface.
There are times when you may want to use a specific SQL statement native to the
database which is installed. ABAP is designed in such a way that if this type of coding is
necessary, this facility is available. It is possible to directly access the Database through
the programs using native SQL statements, but this is not encouraged. Normally, when
systems are set up, the system administrator will forbid these practices, due to the
security and stability risks to the system which may be introduced. If you are going to be
programming ABAP, make sure Open SQL is used, because then anyone subsequently
looking at the programs will understand what is trying to be achieved.
20
SAP SYSTEM OVERVIEW
Like many other programming languages, ABAP programs are normally structured into
two parts.
The first is what is considered to be the Declaration section. This is where you define the
data types, structures, tables, work area variables and the individual fields to be used
inside the programs. This is also where you would declare global variables that will be
available throughout the individual subsections of the program. When creating an ABAP
program, you do not only declare global variables, but you also have the option to declare
22
SAP SYSTEM OVERVIEW
variables that are only valid within specific sections inside the programs. These sections
are commonly referred to as internal Processing Blocks.
The Declaration part of the program is where you define the parameters used for the
selection screens for the reports. Once you have declared tables, global variables and data
types in the Declaration section of the program, then comes the second part of the ABAP
program, where all of the logic for the program will be written. This part of an ABAP
program is often split up into what are called Processing Blocks.
The Processing Blocks defined within programs can be called from the Dynpro processor,
which were discussed previously, depending on the specific rules created within the
program. These Processing Blocks are almost always just small sections of programming
logic which allow the code to be encapsulated.
First Look
When logged into an SAP system it will look something similar to the image below.
The way the SAP GUI looks may vary, the menu to the side may be different, but here the
display show a minimal menu tree which will be used throughout this book.
The first thing to do here is look at the ABAP Workbench. To access this, you use the menu
on the left hand side. Open the SAP menu, choose Tools and open the ABAP Workbench,
where there will be four different options.
23
SAP SYSTEM OVERVIEW
The first thing to look at is a quick overview of how to run a transaction in SAP. There are
two ways to do this. Firstly, if the overview folder is opened, any item which does not look
like a folder itself, is a transaction which can be run. In this instance, we can see the Object
Navigator:
24
SAP SYSTEM OVERVIEW
The second way of running a transaction is to enter the transaction code into the
transaction code input area:
A useful tip to become familiar with the names of transactions is to look at the Extras
menu --> open Settings and in the dialogue box which appears, select the option 'Display
technical names' and click the 'Continue' icon:
25
SAP SYSTEM OVERVIEW
The menu tree will be refreshed, and when the 'Overview' folder is opened, the
transaction codes will be made visible. It is now possible to become familiar with them,
and enter them directly into the transaction code input area:
Now, a step-by-step look will be taken through the major transactions of the ABAP
Workbench to become familiar with, and use, as an ABAP developer.
26
SAP SYSTEM OVERVIEW
ABAP Dictionary
One thing most programs will have in common is that they will read and write data to and
from the Database tables within the SAP system. The ABAP Workbench has a transaction
to allow the creation of Database tables, view the fields which make up these tables and
browse the data inside. This is called the ABAP Dictionary. The ABAP Dictionary can be
found by expanding the ABAP Workbench menu tree --> 'Development'. The transaction
code to run the ABAP dictionary directly is SE11:
ABAP Editor
The next and probably most commonly used part of the ABAP Workbench is the ABAP
Editor, which much of this course will focus upon. The ABAP Editor is where all of the code
is created, the logic built and, by using forward navigation (a function within an SAP
system which will be discussed later), function modules defined, screens created and so
on. The ABAP Editor can be found under the 'Development' menu, as shown above and
with transaction code SE38.
Function Builder
The next important part of the Workbench is the Function Builder, which is similar to the
ABAP Editor. Its main function is to define specific tasks that can be called from any other
program. Interfaces are created in the Function Builder, where the different data
elements and different types of tables are defined, that can be passed to and from the
Function which is built. The Function Builder will be discussed a little later on, when the
programs created are encapsulated into function modules. The Function Builder can be
27
SAP SYSTEM OVERVIEW
Menu Painter
The next item to look at here is called the Menu Painter, which can be found in the 'User
Interface' folder inside the 'Development' menu, or with transaction code SE41. This is a
tool which can be used to generate menu options, buttons, icons, menu bars, transaction
input fields, all of which can trigger events within the program. You can define whether
events are triggered using a mouse click, or with a keyboard-based shortcut. For example,
in the top menu bar here, the 'Log off' button can be seen, which can be triggered by using
(Shift + F3):
Screen Painter
While the Menu Painter is used for building menu items, menu bars and so on, the next
item on the list is the Screen Painter, transaction code SE51, which allows you to define
the user input screen, meaning that you can define text boxes, drop-down menus, list
boxes, input fields, tabbed areas of the screen and so on. It allows you to define the whole
interface which the user will eventually use, and behind the initial elements that are put
on the screen, you can also define the individual functions which are called when the user
interacts with them.
Object Navigator
The last item to look at here is the Object Navigator, a tool which brings together all the
previous tools, providing a highly efficient environment in which to develop programs.
When building large programs, with many function modules, many screens, the Object
Navigator is the ideal tool to use to navigate around the development. It can be found in
the 'Overview' menu of the ABAP Workbench, with transaction code SE80.
These are the main features of the ABAP Workbench interacted with during this course. In
the SAP menu tree, there are evidently many more transactions which can be used to help
develop programs, but these cover the vast majority of development tools which will be
used.
28
DATA DICTIONARY
Introduction
This chapter will focus specifically on the Data Dictionary. This is the main tool used to
look at, understand and enhance the Database and Database tables which are used by the
SAP system. You can view standard tables delivered by SAP using this tool, create new
tables and enhance the existing tables delivered by SAP with new fields. There are many
other features involved in the Data Dictionary, but the focus here will be on the basic ones
so as to build on this later on when creating ABAP programs.
First, a database table will be created, involving the creation of fields, data elements and
domains. An explanation of what each of these is, and why they are necessary to the
tables built will be given. During the building of the tables, the tools used to check for
errors will be shown. Once these errors are eradicated, the tables can be activated so that
they can be used within the system.
After this, a look will be taken at maintaining the technical settings of the table created,
which will allow the entry of data, before finally looking at the data which has been
entered using standard SAP transactions available in the SAP system.
Creating a Table
With the SAP GUI open, you will be able find the Data Dictionary in the SAP menu tree.
This is done via the Tools menu. Open the ABAP Workbench and click the 'Development'
folder, where the ABAP Dictionary can be found and double-clicked. Alternatively, use the
transaction code SE11:
29
DATA DICTIONARY
To create a table, select the 'Database table' option. In this exercise a transparent table
will be created. Other types of table do exist (such a cluster tables and pool tables), but at
this early stage the transparent table variety is the important one to focus upon.
The table name must adhere to the customer-defined name space, meaning that the
name must begin with the letter Z or Y, most commonly this will be Z. In this example, the
table will show a list of employees within a company, so, in the 'Database table' area, type
'ZEMPLOYEES' and click the 'Create' button.
30
DATA DICTIONARY
In the 'Short text' field, a description for the table must be included, enter 'Employees':
In the 'Delivery and Maintenance' tab (which opens by default), look at the 'Delivery class'
section, select the field and then click the drop-down button, where a list of Delivery
classes will be shown and selected:
31
DATA DICTIONARY
For the table being created here, choose 'Application table', as the data held in the table
fits the description 'master and transaction data'.
In the field below this, labelled 'Data Browser/Table View Maint.', choose the
'Display/Maintenance allowed' option, which will allow for data entry directly into the
table later on. It should look like this:
Nearly all development work done with SAP is usually done within a development
environment, before being moved on to, for example, a quality assurance environment
and on further to production. This window allows you to choose the appropriate
Development class which is supported by other systems where the work may be moved
on to. In this example scenario, though, developments will not be moved on to another
system, so click 'Local object', so as to indicate to the system (via the phrase '$TMP' which
appears) that the object is only to exist within the development system and not to be
transported elsewhere. Once this is done, the status bar at the bottom will show that the
object has been saved:
32
DATA DICTIONARY
To check everything has worked as we want, select the 'Go to' menu and selects the
'Object directory entry' option, a similar pop-up box to the previous one will appear,
where the 'Development class' field will show '$TMP', confirming this has been done
correctly.
Creating Fields
The next step is to begin creating Field names for the table, in the 'Fields' tab:
Fields, unlike the name of the table, can begin with any letter of the alphabet, not just Z
and Y and can contain up to 16 characters.
33
DATA DICTIONARY
Tables must include at least one Key field, which is used later for the searching and sorting
of data, and to identify each record as being unique.
An Initial value can be assigned to each field, for example, in the case of a field called
Employee Class you could say the majority of employees are Regular Staff ('S'), but some
are Directors, with a code of 'D'. The standard initial value would be 'S', but the user could
change some of these to a 'D' later on.
Data Elements
Every Field in the table is made up of what is called a Data Element, which defines specific
attributes of each field.
The first Field to be created here is an important one within an SAP system, and identifies
the client which the records are associated with. In the Field name, enter 'Client', and in
the Data Element, type 'MANDT'. This Data Element already exists in the system, and after
entering it, the system automatically fills in the Data Type, the Length, Number of
Decimals and Short text for the Data Element itself. Ensure that the 'Client' field is made a
Key field in the table by checking the 'Key' box.
The next field will be called 'Employee'. Again, check the box to make this a Key field, and
enter the new Data Element 'ZEENUM' (Data Elements broadly must adhere to the
customer name space by beginning with Z or Y). Once this is done, click the save button.
Next, because the Data Element 'ZEENUM' does not yet exist, it must be created. If you try
to activate or even check the table (via the 'Check' button), an error message is
displayed:
Until the Data Element 'ZEENUM' is created, it cannot be used within the system. To do
this, forward navigation is used. Double-click the new Data Element, and a window
labelled 'Create Data element' appears. Answer 'Yes' to this, and the 'Maintain Data
Element' window comes up.
34
DATA DICTIONARY
35
DATA DICTIONARY
In the 'Short text' area, enter 'Employee Data Element'. Next, the Elementary data type,
called the 'Domain', must be defined for the new Data element. Domains must adhere to
the customer name space, so in this instance the same name as the Data element will be
given: 'ZEENUM', (though giving both the same name is not imperative). Again, forward
navigation will be used to create the Domain.
Data Domains
Double-click the entry ('ZEENUM') in the Domain area, and agree to save the changes
made. Now, the 'Create Object Directory Entry' window will re-appear and again it is
important to save this development to the '$TMP' development class, via the 'save' or
'local object' button visible in this window.
After doing this, a window will appear stating that the new Domain 'ZEENUM' does not
exist. Choose 'Yes' to create the Domain, and in the window which appears, type into the
'Short text' box a description of the Domain. In this example, 'Employee Domain':
36
DATA DICTIONARY
The 'Definition' tab, which, as shown above, opens automatically. The first available field
here is 'Data type', click inside the box and select the drop-down menu, and a number of
generic data types already existing within the ABAP dictionary will appear.
Next, in the 'No. characters' field, enter the number 8, indicating that the field will contain
a maximum of 8 characters, and in the 'Decimal places' area, enter 0. An Output length of
8 should be selected, and then press Enter.
The 'NUMC' field's description should re-appear, confirming that this is a valid entry.
Next, select the 'Value range' tab, which is visible next to the 'Definition' tab just used:
This is where you set valid value ranges for the Domain created. Once this is set, any
subsequent user entering values outside the valid value range will be shown an error
37
DATA DICTIONARY
message and be requested to enter a valid entry. Here, there are three options.
First, where you can see 'Single values', it is possible to enter a list of individual
valid values which can be entered by the user.
Second, 'Intervals', where you can enter a lower and upper limit for valid values,
for example 1 and 9, which saves the effort of entering 9 individual single values in
the 'Single values' section.
Last, the 'Value table' box visible at the bottom. When there are a large number of
possible entries, this is a common method (to do this you must specify a complete
valid value table entry list, in which case it is also necessary to introduce foreign
keys to the table, to ensure the user's entries are tested against the value stored
in the value table created).
This example Domain, however, does not require any Value range entry, so just click the
save button and, again, assign it as a 'Local object'.
The next step is to Activate the object, allowing other Data elements to use this domain
going forward. In the toolbar click the small matchstick icon (also accessible by pressing
CTRL +F3).
It may be possible to activate all of the objects together, but this is not advised. In a typical
development environment, a number of people will be creating developments
simultaneously, and quite often, others' objects will appear in this list.
At this point, it is only the Domain which is to be activated, the top entry labelled 'DOMA',
with the name 'ZEENUM'. When this is highlighted, click the green tick continue button.
The window should disappear, and the status bar will display the message 'Object(s)
38
DATA DICTIONARY
activated'
Now it is possible to proceed with the creation of the table. Forward navigation was used
for generating the Domain, so click the 'Back' button, or press F3 to return to the
'Maintain Data Element' screen. As the domain is active, the description entered
previously should appear by the area where 'ZEENUM' was typed, along with other
Domain properties which have been created:
Next, the Field labels must be created, so click that tab. The Field labels entered here will
appear as field labels in the final table. In this example they should read 'Employee', or
better, 'Employee Number'. If this does not fit within the area given, just tailor it so that it
still makes sense, for example typing 'Employee N' into the 'Short' Field label box. Once
the text has been put into the Field label spaces, press enter, and the 'Length' section will
automatically be filled in:
39
DATA DICTIONARY
Once this is complete, Save and Activate the element via the toolbar at the top. The
inactive objects window will reappear, where two inactive objects will remain. Highlight
the Data element (labelled 'DTEL') and click the green tick
Continue button at the bottom.
Press the back button to return to the Table maintenance screen. Here you will now see
that the 'EMPLOYEE' column has the correct Data Type, Length, Decimals and Short text,
thus indicating the successful creation of a Data element and Domain being used for this
Field.
Next, the same practices will be used to create four additional fields.
The next field to create should be titled 'SURNAME'. This time it should not be selected as
a Key field, so do not check the box. The Data element, in this instance, is labelled
'ZSURNAME':
Now, forward navigation will again be used. Double-click )“U‘NAME ; choose 'Yes' to
save the table and 'Yes' again to create the new Data element. The 'Maintain Data
Element' window will appear which will be familiar from the previous steps.
In the 'Short text' box this time type 'Surname Data Element' and title the new domain
40
DATA DICTIONARY
'ZSURNAME':
Double-click the new domain and save the Data element, assigning it a 'Local object' and
then choose 'Yes' to create the new Domain.
The Domain maintenance screen will reappear. Enter the short text 'Surname' and, this
time; the Data type to select is 'CHAR', a Character string.
The number of characters and output length should both be set to 40, then press enter to
be sure everything has worked, and click the Activate button.
41
DATA DICTIONARY
Note that the Save button has not been pressed this time, as the Activate button will also
save the work automatically. Ensure you assign the object to the $TMP development class
as usual.
In the Activate menu, select the object (the domain (labelled 'DOMA') named
'ZSURNAME') to be activated, and click the green tick continue button. The status bar
should read 'Object saved and activated'.
Following this, click Back or F3 to return to the Maintain Data element screen. Ensure the
domain attributes have appeared (Short text, Data type, Length and so on). In the Field
Label tab, enter 'Surname' in each box and press Enter to automatically fill the 'Length'
boxes and then activate the Data element (in the Activate menu, the 'DTEL' object named
'ZSURNAMES'), checking the status bar to ensure this has occurred with any errors:
Again, press Back to return to the Maintain Table screen, where the new Data element will
be visible:
42
DATA DICTIONARY
The next field to be created is titled 'FORENAME', and the data element 'ZFORENAME'.
Click to create the Data element and follow the steps above again.
In the Maintain Data Element screen, the Short text should read 'Forename Data Element'
and the domain 'ZFORENAME'. Save this and choose 'Yes' to create the domain.
The domain's short text should read 'Forename'. Use the CHAR data type again and a
Length and Output length of 40. Next, Activate the Domain as before.
Return to the Maintain Data Element screen. Type 'Forename' into the four Field label
boxes. Press enter to fill the length boxes and then Activate the Data Element named
'ZFORENAME' as before. Go back again to see the table:
43
DATA DICTIONARY
The next field will be called 'Title' and the Data Element 'ZTITLE', follow the steps above
again to create this field with the following information:
The Data element short text should read 'Title Data Element' and the domain should be
named 'ZTITLE'.
The Domain Short text should be 'Title' and the Data type is again 'CHAR'. This time the
Length and Output length will be 15.
Activate all of these and go back to view the new, fifth field in the Table.
The final field which will be created for this table is for Date of Birth. In the Field box type
'DOB' and create the Data element 'ZDOB' using the steps from the previous section and
this information:
The Data element short text should read 'Date of Birth Data Element' and the domain
should be named 'ZDOB'.
The Domain Short text should be 'Date of Birth' and the Data type is, this time, 'DATS',
after which an information box will appear to confirm this. Click the green tick to continue:
44
DATA DICTIONARY
For the DATS data type, the Length and Output lengths are set automatically at 8 and 10
(the Output length is longer as it will automatically output dividers between the day,
month and year parts of the date).
The Field labels should all read 'Date of Birth', except the 'Short' label where this will not
fit, so just type 'DOB' here. Activate the Domain and Data element, and return to the
table.
Technical Settings
Once this has been saved, the next step is to move on to maintaining the technical settings
of the Table. Before creating the final Database table, SAP will need some more
information about the table being created.
Select 'Technical settings' via the toolbar above the table, through the 'Go to' menu, or
45
DATA DICTIONARY
Here, it is important to tell the system what Data class is to be used, so select the drop
down button. There are five different options, with accompanying descriptions. For this
table, select the first, labelled 'APPL0', and double-click it:
For the 'Size category' field, again click the drop-down button. Here, you have to make an
estimate as to the amount of data records which will be held within the table so that the
system has some idea of how to create the tables in the underlying database. In this
instance, it will be a relatively small amount of information, so select the first size
category, labelled 0:
46
DATA DICTIONARY
Below this are the Buffering options. Here, 'Buffering not allowed' should be selected:
This prevents the table contents from being loaded into memory for reading, stopping the
table from being read in advance of the selection of the records in the program. You may,
correctly, point out that it may be advantageous to hold the table in the memory for
speed efficiency, but in this example, this is not necessary. If speed was an issue in a
development, buffering would then be switched on, ensuring the data is read into
memory. In the case of large tables which are accessed regularly but updated
infrequently, this is the option to choose.
Nothing else on the 'Maintain Technical Settings' screen needs to be filled at this point, so
click Save and then go back to the table itself. If all of this is successful, then the table
should now be in a position to be activated and the entry of records can begin. Click the
Activate icon to activate the table and check the status bar, which should again read
'Object Activated'.
47
DATA DICTIONARY
A Data-entry screen will appear which has automatically been generated from the table
created. The field names correspond here to the technical names given when we created
them. To change these to the Field labels which we set up, enter the 'Settings' menu and
select 'User Parameters'. This facility allows you to tailor how tables look for your own
specific user ID. Select the 'Field label' radio button and click 'Continue':
48
DATA DICTIONARY
The Field labels created will now appear as they were defined when creating the table:
The Employee Number field is limited to 8 characters, and the data type was set to NUMC,
so only numerical characters can be entered. Create a record with the following data:
Press Enter and the system will automatically put the names in upper case, and validate
each field to ensure the correct values were entered:
49
DATA DICTIONARY
Click Save and the status bar should state 'Database record successfully created'. Next,
click the 'Reset' button above the data entry fields to clear the fields for the next entry.
Note that this time the Date of Birth has been filled in without the appropriate dividers.
When Enter is pressed, the system automatically validates all fields, correcting the Date of
Birth field to the correct formatting itself:
50
DATA DICTIONARY
Save, Reset, and then further records can be entered following the same steps:
Note that if dates are entered in the wrong format, an error message will appear in the
status bar:
Having entered several data records in the manner discussed previously, click the Back key
to return to the 'Dictionary: Display Table' screen. To view the table created with the data
entered, from the 'Utilities' menu, select 'Table contents' and then 'Display':
51
DATA DICTIONARY
A selection screen will then appear, allowing you to enter or choose filter values for the
fields you created. The selection screen is very useful when you have lots of data in your
table. In this case, though, only five records have been entered, so this is unnecessary.
However, for example if you were to only want to focus on a single employee number, or
a small range, these figures can be selected from this screen:
To view all of the records, do not enter any data here. Just click the 'Execute' button,
which is displayed in the top left corner of the image above, or use the shortcut F8. You
will now see a screen showing the data records you entered in the previous section:
52
DATA DICTIONARY
If further fields were to exist, the screen would scroll further to the right, meaning not all
fields could be displayed simultaneously due to field size properties.
If you want to see all of the data for one record, double-click on the record and this will be
shown. Alternatively, several records can be scrolled through by selecting the desired
records via the check-boxes to the left of the 'Client' column and then clicking the 'Choose'
icon on the toolbar:
These can then be individually viewed and scrolled through with the 'Next entry' button:
To return to the full table then, simply click the Back button, or press F3.
Experiment with the table created, using the toolbar's range of options to filter and sort
the information in a number of ways:
53
DATA DICTIONARY
For example, to organise alphabetically by forename, click to select the 'Forename' field,
There are a number of things which can be achieved in this table view, and it can be a
useful tool for checking the data within an SAP system without going through the
transaction screens themselves.
54
YOUR FIRST ABAP PROGRAM
Chapter 3
Creating a Program
To begin creating a program, access the ABAP Editor either via transaction code SE38, or
by navigating the SAP menu tree to Tools ABAP Workbench Development, in which
the ABAP Editor is found. Double-click to execute.
A note to begin: it is advisable to keep the programs created as simple as possible. Do not
make them any more complicated than is necessary. This way, when a program is passed
on to another developer to work with, fix bugs and so on, it will be far easier for them to
understand. Add as many comments as possible to the code, to make it simpler for
anyone who comes to it later to understand what a program is doing, and the flow of the
logic as it is executed.
The program name must adhere to the customer naming conventions, meaning that here
it must begin with the letter Z. In continuation of the example from the previous chapter,
i this i sta e the p og a ill e titled )_E plo ee_List_ , hi h should e t ped
i to the P og a field o the i itial s ee of the ABAP Edito . E su e that the “ource
ode utto is he ked, a d the li k C eate :
55
YOUR FIRST ABAP PROGRAM
I the Att i utes se tio of the i do , fo the T pe , li k the d op-down menu and
sele t E e uta le p og a , ea i g that the p og a a e e e uted ithout the use
of a transaction code, and also that it can be run as a background job. The “tatus sele ted
should e Test p og a , a d the Appli atio should e Basis . These t o optio s help
to manage the program within the SAP system itself, describing what the program will be
used for, and also the program development status.
For now, the other fields below these should be left empty. Particularly ensure that the
Edito Lock o is left lea sele tio of this ill p e e t the p og a f o ei g edited .
U i ode he ks a ti e should e sele ted, as should Fi ed poi t a ith eti without
this, any packed-decimal fields in the program will be rounded to whole numbers). Leave
the “ta t usi g a ia t o la k. The , li k the “a e utto .
56
YOUR FIRST ABAP PROGRAM
Code Editor
Here, focus will be put on the coding area. The first set of lines visible here are comment
lines. These seven lines can be used to begin commenting the program. In ABAP,
comments can appear in two ways. Firstly, if a * is placed at the beginning of a line, it
turns everything to its right into a comment.
57
YOUR FIRST ABAP PROGRAM
Note that the * must be in the first column on the left. If it appears in the second column
or beyond, the text will cease to be a comment.
The next line of code, visible above, begins with the word REPORT. This is called a
STATEMENT, and the REPORT statement will always be the first line of any executable
program created. The statement is followed by the program name which was created
previously. The line is then terminated with a full stop (visible to the left of the comment).
Every statement in ABAP must be followed by a full stop, or period. This allows the
statement to take up as many lines in the editor as it needs, so for example, the REPORT
statement here could look like this:
58
YOUR FIRST ABAP PROGRAM
As long as the period appears at the end of the statement, no problems will arise. It is this
period which marks where the statement finishes.
If you require help with a statement, place the cursor within the statement and choose
the Help o ... utto i the top tool a :
A window will appear with the ABAP keyword automatically filled in. Click the continue
button and the system will display help on that particular statement, giving an explanation
of what it is used for and the syntax. This can be used for every ABAP statement within an
SAP system. Alternatively, this can be achieved by clicking the cursor within the
statement, and pressing the F1 key:
59
YOUR FIRST ABAP PROGRAM
60
YOUR FIRST ABAP PROGRAM
Returning to the ABAP editor now, the first line of code will be written. On the line below
the REPORT statement, type the statement: write HELLO SAP WO‘LD .
The write statement will, as you might expect, write whatever is in quotes after it to the
output window (there are a number of additions which can be made to the write
statement to help format the text, which we will return in a later chapter).
Save the program, and check the syntax with the Che k utto i the tool a o ia CT‘L
+ F . The status a should displa a essage eadi g P og a )_EMPLOYEE_LI“T_ is
s ta ti all o e t . The , li k the A ti ate utto , hi h should add the o d A ti e
next to the program name. On e this is do e, li k the Di e t p o essi g utto to test
the code:
61
YOUR FIRST ABAP PROGRAM
The report title and the text output should appear like this, completing the program:
Write Statements
Now that the first program has been created, it can be expanded with the addition of
further ABAP statements. Use the Back button to return from the test screen to the ABAP
editor.
Here, the tables which were created in the ABAP Dictionary during the first stage will be
accessed. The first step toward doing this is to include a ta le s statement in the program,
which will be placed below the REPORT statement. Following this, the table name which
62
YOUR FIRST ABAP PROGRAM
was created is typed in, z_employee_list_01, and, as always, a period to end the
statement:
While not essential, to keep the format of the code uniform, the Pretty Printer facility can
e used. Cli k the P ett P i te utto i the tool a to auto ati all alte the te t i
line with the Pretty Printer settings (which can be accessed through the Utilities menu,
Settings, and the Pretty Printer tab in the ABAP Editor section):
Once these settings have been applied, the code will look slightly tidier, like this:
63
YOUR FIRST ABAP PROGRAM
Let us now return to the TABLES statement. When the program is executed, the TABLES
statement will create a table structure in memory based on the structure previously
defined in the ABAP Dictionary. This table structure will include all of the fields previously
created, allowing the records from the table to be read and stored in a temporary
structure for the program to use.
To retrieve from our data dictionary table and place them into the table structure, the
SELECT statement will be used.
Type SELECT * from z_employee_list_01. This is telling the system to select everything
(the * refers to all-fields) from the table. Because the SELECT statement is a loop, the
system must be told where the loop ends. This is done by typing the statement
ENDSELECT. Now we ha e eated a sele t loop let s do so ethi g ith the data e ha e
are looping through. Here, the WRITE command will be used again. Replace the write
HELLO SAP WORLD . li e ith write z_employee_list_01.” to write every row of the
table to the output window:
Che k the ode ith the Che k utto , a d it ill state that the e is a s ta error:
64
YOUR FIRST ABAP PROGRAM
The cursor will have moved to the TABLES statement which was identified, along with the
a o e a i g. The a e )_EMPLOYEE_LI“T_ appea s to e i o e t. To he k this,
open a new session via the New Session button in the toolbar . Execute the ABAP
Di tio a ith t a sa tio ode “E , sea h fo )* i the Data ase ta le o a d it ill
bring back the table ZEMPLOYEES, meaning that the initial table name
Z_EMPLOYEE_LIST_01 was wrong. Close the new session and the syntax error window and
type i the o e t ta le a e )EMPLOYEE“ afte the TABLES state. Your screen should
look like this:
Save the program and check the code, ensuring the syntax error has been removed, and
then click the Test button (F8) and the output window should display every row of the
table:
65
YOUR FIRST ABAP PROGRAM
Look at the data in the output window. The system has automatically put each line from
the table on a new row. The WRITE statement in the program did not know that each row
was to be output on a new line; this was forced by some of the default settings within the
system regarding screen settings, making the line length correspond to the width of the
screen. If you try to print the report, it could be that there are too many columns or
characters to fit on a standard sheet of A4. With this in mind, it is advisable to use an
addition to the REPORT statement regarding the width of each line.
Return to the program, click the REPORT statement and press the F1 key and observe the
LINE SIZE addition which can be included:
In this example, add the LINE-SIZE addition to the REPORT statement. Here, the line will be
limited to 40 characters. Having done this, see what difference it has made to the output
window. The lines have now been broken at the 40 character limit, truncating the output
66
YOUR FIRST ABAP PROGRAM
of each line:
Bear these limits in mind so as to avoid automatic truncation when printing reports. For a
standard sheet of A4 this limit will usually be 132 characters. When the limit is set to this
for the example table here, the full ta le etu s, ut the li e e eath the title My
Employee List Report displa s the poi t at hi h the output is limited:
Next, the program will be enhanced somewhat, by adding specific formatting additions to
the WRITE statement. First, a line break will be inserted at the beginning of every row that
is output.
67
YOUR FIRST ABAP PROGRAM
Duplicate the previous SELECT – ENDSELECT statement block of code and place a / after
the WRITE statement. This will trigger a line break:
Save and execute the code. The output window should now look like this:
The first SELECT loop has created the first five rows, and the second has output the next
five.
Both look identical. This is due to the LINE-SIZE limit in the REPORT statement, causing the
first five rows to create a new line once they reached 132 characters. If the LINE-SIZE is
increased to, for example 532, the effects of the different WRITE statements will be
visible:
68
YOUR FIRST ABAP PROGRAM
The first five rows, because they do not have a line break in the WRITE statement, have
appeared on the first line up until the point at which the 532 character limit was reached
and a new line was forced. The first four records were output on the first line. The 5th
record appears on a line of its own followed by the second set of five records, having had
a line break forced before each record was output.
Return the LINE-SIZE to 132, before some more formatting is done to show the separation
between the two different SELECT loops.
Above the second SELECT loop, type ULINE. This means underline.
Click the ULINE statement and press F1 for further explanation from the Documentation
i do , hi h ill state W ites a o ti uous u de li e i a e li e. Doi g this ill
help separate the two different SELECT outputs in the code created. Execute this, and it
should look like so:
Duplicate the previous SELECT – ENDSELECT statement block of code again, including the
69
YOUR FIRST ABAP PROGRAM
ULINE, to create a third SELECT output. In this third section, remove the line break from
the W‘ITE state e t a d, o the li e elo , t pe W‘ITE /. This ill ean that a new
line will be output at the end of the previous line. Execute this to see the difference in the
third section:
Now, create another SELECT loop by duplicating the second SELECT loop. This time the
WRITE statement will be left intact, but a new statement will be added before the SELECT
loop: SKIP, which means to skip a line. This can have a number added to it to specify how
many lines to skip, in this case 2. If you press F1 to access the documentation window it
will explain further, including the ability to skip to a specific line. The code for this section
should look like the first image, and when executed, the second:
70