0% found this document useful (0 votes)
3 views

Module 4 - Coding and Debugging in ABAP

The document is a training presentation on SAP ABAP, covering topics from defining simple variables to debugging programs. It includes lessons on data types, arithmetic operations, system variables, and the ABAP Debugger. The content is structured to guide users from basic to advanced concepts in ABAP programming.

Uploaded by

thoyaja.abap
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Module 4 - Coding and Debugging in ABAP

The document is a training presentation on SAP ABAP, covering topics from defining simple variables to debugging programs. It includes lessons on data types, arithmetic operations, system variables, and the ABAP Debugger. The content is structured to guide users from basic to advanced concepts in ABAP programming.

Uploaded by

thoyaja.abap
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 59

SAP ABAP

(Basics to Advanced)

Coding and Debugging in ABAP


www.zarantech.com

© Copyright 2021, ZaranTech LLC. All rights reserved.


2 Disclaimer

• This presentation, including examples, images, and references are provided for
informational purposes only.

• Complying with all applicable copyrights laws is the responsibility of the user.

• Without limiting the rights under copyright, no part of this document may be
reproduced, stored or introduced into a retrieval system, or transmitted in any
form or by any means.

• Credits shall be given to the images taken from the open-source and cannot be
used for promotional activities

© Copyright 2021, ZaranTech LLC. All rights reserved.


3 Agenda
• Lesson 1 - Defining Simple Variables
• Lesson 2 - Defining Text Symbols Performing Arithmetic Operations Using
Simple Variables
• Lesson 3 - Using System Variables
• Lesson 4 - Debugging a Program
• Lesson 5 - Implementing Conditional Logic
• Lesson 6 - Implementing Loops
• Lesson 7 - Analyzing Runtime Errors
• Lesson 8 - Implementing Error Handling

© Copyright 2021, ZaranTech LLC. All rights reserved.


2 Lesson 1 - Defining Simple Variables
• Use of Data Types
Objectives
• Choosing Data Types for Selection Screen
Parameters
• ABAP Standard Data Types
• Character-like Types
• Numeric Types
• Character-like Types
• Global Data Types
• The ABAP Dictionary
• Variable Declarations
• Literals and Constants 4
© Copyright 2021, ZaranTech LLC. All rights reserved.
2 Use of Data Types

5
© Copyright 2021, ZaranTech LLC. All rights reserved.
2 Choosing Data Types for Selection Screen Parameters

6
© Copyright 2021, ZaranTech LLC. All rights reserved.
2 Choosing Data Types for Selection Screen Parameters

• The name of the parameter can be chosen freely. It has no impact on the values that can be
entered in the field.
• The type of the parameter determines the values that can be entered (numbers, characters,
strings, dates, and so on) and how large those values can be.
• You can think of a data type as being a formal description of the variable (input field).
• The figure Choosing Data Types for Selection Screen Parameters shows how different data types
are appropriate for different parameters, depending on the intended use.

7
© Copyright 2021, ZaranTech LLC. All rights reserved.
2 Choosing Data Types for Selection Screen Parameters

• The data type does not just determine the values that can be entered in the input field. It also
impacts what can be done with the data in the ABAP program.
• If a variable is type string and contains the value 'ABC', calculations cannot be performed.
• It is important to consider which data type to give to a selection screen parameter, as this will
determine which values the user can enter in the corresponding input field.
• It is also important to consider the correct data type to give to internal program variables. The
declaration and use of these variables is covered in another lesson.

8
© Copyright 2021, ZaranTech LLC. All rights reserved.
2 ABAP Standard Data Types

Type Meaning Use


Character-like Types
Variable length character string Any characters. Has an arbitrary
length.

string

9
© Copyright 2021, ZaranTech LLC. All rights reserved.
2 ABAP Standard Data Types
Type Meaning Use
c Fixed-length character string Any characters. Has a fixed length.

d Date Holds dates in the format


YYYYMMDD.
t Time Holds times in the format HHMMSS.

Numeric Types
i Integer Whole numbers (no decimal places).

p Packed number Decimal numbers.

10
© Copyright 2021, ZaranTech LLC. All rights reserved.
2 Character-like Types

• Variables of type c or type string can both hold character strings.


• Variables of type string can be any length.
• You do not need to specify a length and it is not possible to do so.
• Variables of type c have a specific length; you specify this using the LENGTH addition as follows:
DATA gv_var1 TYPE c LENGTH 2.

* Or

PARAMETERS pa_field TYPE c LENGTH 10.

11
© Copyright 2021, ZaranTech LLC. All rights reserved.
2 Numeric Types

12
© Copyright 2021, ZaranTech LLC. All rights reserved.
2 Numeric Types

• Type p is one of the most important and common ABAP data types.
• It can be used for business calculations where the result must be accurate.
• The abbreviation p comes from the term ‘packed’, because this data type packs two digits into
each byte.
• In principle, a variable of type p can contain twice as many digits as the specified length, but half
a byte is required for the sign (since negative values can be stored in a variable of type p).

13
© Copyright 2021, ZaranTech LLC. All rights reserved.
2 Global Data Types

14
© Copyright 2021, ZaranTech LLC. All rights reserved.
2 The ABAP Dictionary

• Many business entities that you may need to work with in your ABAP programs are described in
the SAP system.
• These descriptions can be found in the ABAP Dictionary by looking at the definitions of the
database tables that hold your SAP data.

PARAMETERS pa_mat TYPE MARA-MATNR.

15
© Copyright 2021, ZaranTech LLC. All rights reserved.
2 The ABAP Dictionary

16
© Copyright 2021, ZaranTech LLC. All rights reserved.
2 Variables

17
© Copyright 2021, ZaranTech LLC. All rights reserved.
2 Variable Declarations

18
© Copyright 2021, ZaranTech LLC. All rights reserved.
2 Variable Declarations

• You can use the VALUE addition to preassign a value to a variable.


• In the DATA statement, there are two ways that you can provide the length for any variable of a
type that requires it (for example, type c and type p).
• The two ways are as follows:

19
© Copyright 2021, ZaranTech LLC. All rights reserved.
2 Literals and Constants

20
© Copyright 2021, ZaranTech LLC. All rights reserved.
2 Literals

• Literals are strings of characters without a name. Their values cannot be changed, since they are
essentially hard-coded values.
• Numeric literals consist of continuous sequences of numbers, and text literals are character
strings.
• Numeric literals can be specified without quotes, whereas text literals must be enclosed in
single-quotes, as shown in the figure.
• Since text literals cannot be changed, you cannot translate them into other languages. This can
cause problems. A solution is to use text symbols instead of text literals.
• The lesson Defining Text Symbols provides more information about text symbols.

21
© Copyright 2021, ZaranTech LLC. All rights reserved.
2 Constants

22
© Copyright 2021, ZaranTech LLC. All rights reserved.
2 Constants

• We recommend that you avoid using literals to specify values in your source code. Instead,
define constants with those values and use the constants in place of literals.
• One reason for this is that certain values will be required in more than one place in the source
code.
• It is inconvenient to specify these values directly, since you would need to change several
statements if the value needs to be changed.
• Instead, you can use an appropriate constant so that you only need to modify the value once.
This can make a program easier to maintain and enhance in the future.

23
© Copyright 2021, ZaranTech LLC. All rights reserved.
Lesson 2 - Defining Text Symbols Performing Arithmetic
2
Operations Using Simple Variables
Objectives

• Text Symbols
• Value Assignment to Simple Variables
• Value Assignments
• Calculations Using Simple Variables
• Result of Calculations in ABAP
• Arithmetic Operators for ABAP Calculations
• Keyword Alternatives for Arithmetic Operators

24
© Copyright 2021, ZaranTech LLC. All rights reserved.
2 Text Symbols

25
© Copyright 2021, ZaranTech LLC. All rights reserved.
2 Text Symbols

• Multilingual capability is an important principle in ABAP development. It means that the logon
language of the current user is accounted for when texts display on the user interface.
• As a consequence, we recommend that you avoid the use of text literals (hard-coded texts)
when developing programs, since a literal exists in only one language in the source code.
• ]If you are developing productive programs that will be executed by different users in different
logon languages, use text symbols to ensure that all texts are translatable.

26
© Copyright 2021, ZaranTech LLC. All rights reserved.
2 Text Symbols

27
© Copyright 2021, ZaranTech LLC. All rights reserved.
2 Text Symbols

• Text symbols belong to a specific program and can be translated into different languages. When
a statement using a text symbol is executed, the system automatically takes into account the
logon language of the user and supplies the text in this language. This feature is illustrated in the
figure Text Symbols.
• A text symbol is identified by means of a three-character alphanumeric ID, such as xxx.

28
© Copyright 2021, ZaranTech LLC. All rights reserved.
2 Text Symbols

There are two ways to address text symbols in your code:


• TEXT-xxx, where xxx stands for the three-character text symbol ID. For example, WRITE TEXT-abc.
• '...'(xxx), where the ellipsis (...) is the original language text. For example, WRITE ‘Hello’(abc).
There are two ways to access the screen where you can define a text symbol:
• Choose Goto→ Text Elements → Text Symbols .
• Define the text symbol ID in the source code, then double-click it.
• The Editor asks you if you want to create a text symbol, then opens the Text Symbols screen

29
© Copyright 2021, ZaranTech LLC. All rights reserved.
2 Value Assignment to Simple Variables

30
© Copyright 2021, ZaranTech LLC. All rights reserved.
2 Value Assignment to Simple Variables

• When a program starts, the system makes memory available for the variables defined in the
program.
• Every variable is preassigned an initial value based on its type unless you set a different value
using the VALUE addition.
• For example, the initial value of a numeric variable is 0, and the initial value of a character-type
variable is a sequence of spaces.
• The figure Initial Values of Variables demonstrates the initial values of different variable types.

31
© Copyright 2021, ZaranTech LLC. All rights reserved.
2 Value Assignments

32
© Copyright 2021, ZaranTech LLC. All rights reserved.
2 Value Assignments

• The figure Value Assignments demonstrates how the values of a set of variables change as the
program processes several statements.
• For example, you can use the following syntax to assign a value to a variable: gv_num = 30.

• The target variable is to the left of the equal sign. The value to the right of the equal sign will be
assigned to the target variable and will overwrite the current value.
• You can also use the MOVE statement to transfer the contents of one variable to another, as
follows: MOVE gv_num TO gv_num2.
• This has the same result as using the short-form assignment. In this example, the MOVE
statement is equivalent to the following assignment: gv_num2 = gv_num. 33
© Copyright 2021, ZaranTech LLC. All rights reserved.
2 Calculations Using Simple Variables

34
© Copyright 2021, ZaranTech LLC. All rights reserved.
2 Result of Calculations in ABAP

35
© Copyright 2021, ZaranTech LLC. All rights reserved.
2 Arithmetic Operators for ABAP Calculations

• ABAP allows you to program arithmetic operations easily. The list of valid operators for an
arithmetic operation includes the following:

+ (Addition) - (Subtraction) * (Multiplication) / (Division)

• You can use parentheses in the calculation if one arithmetic expression must be used as the
operand for another arithmetic expression, as follows:
• gv_result = ( pa_num1 / pa_num2 ) * 100.

36
© Copyright 2021, ZaranTech LLC. All rights reserved.
2 Arithmetic Operators for ABAP Calculations

Operator Meaning

+ Addition

- Subtraction

* Multiplication

/ Division

37
© Copyright 2021, ZaranTech LLC. All rights reserved.
2 Keyword Alternatives for Arithmetic Operators

Keyword Example

ADD ADD 1 TO gv_result.

SUBTRACT SUBTRACT 1 FROM gv_result.

MULTIPLY MULTIPLY gv_result BY 2.

DIVIDE DIVIDE gv_result BY 4.

38
© Copyright 2021, ZaranTech LLC. All rights reserved.
2 Lesson 3 - Using System Variables

Objectives

• System Variables

39
© Copyright 2021, ZaranTech LLC. All rights reserved.
2 System Variables

System Variable Description

SY-DATUM Current date

SY-UZEIT Current time

SY-UNAME User ID of current user

40
© Copyright 2021, ZaranTech LLC. All rights reserved.
2 System Variables

• System variables provide information about the actual system status. The ABAP runtime system
populates and changes the values of the system fields when necessary.
• For example, the system variable SY-DATUM is always filled with the current date, and SY-UZEIT
is filled with the current time.
• The table System Variables shows just three system variables. As you learn more about ABAP,
you will learn about other important system variables.
• For a complete list of system variables, see the ABAP keyword documentation for System Fields.

41
© Copyright 2021, ZaranTech LLC. All rights reserved.
2 Lesson 4 - Debugging a Program

Objectives

• ABAP Debugger
• Setting a Breakpoint
• Activating the Debugger From a
Screen
• Understanding the Debugger
• Moving Through Code in the
Debugger
• Different Types of Breakpoint
• Watchpoints 42
© Copyright 2021, ZaranTech LLC. All rights reserved.
2 ABAP Debugger

• The ABAP Debugger is an important diagnostic tool that you can use to analyze ABAP programs.
• With the Debugger, you can determine why a program is not working correctly by ‘stepping
inside’ the program at runtime.
• This allows you to see the statements being executed and the changing values of variables as the
program proceeds.
• To use the Debugger, you first decide at which point you want to activate it.
• Depending on your needs, there are different ways to start the Debugger.

43
© Copyright 2021, ZaranTech LLC. All rights reserved.
2 ABAP Debugger

44
© Copyright 2021, ZaranTech LLC. All rights reserved.
2 Setting a Breakpoint

45
© Copyright 2021, ZaranTech LLC. All rights reserved.
2 Activating the Debugger From a Screen

46
© Copyright 2021, ZaranTech LLC. All rights reserved.
2 Activating the Debugger From a Screen

• If your program has a screen, you can activate the Debugger from the screen (that is, you start
debugging after the input fields are filled and the appropriate button or function is chosen).
• First, start the program without the Debugger, fill any relevant input fields, and activate the
Debugger before executing the function.
• To activate the Debugger from a screen, enter /h in the command field in the standard toolbar
(see the figure Activating the Debugger from a Screen), then press ENTER.
• The system displays a message to tell you that the Debugger has been activated, and the
Debugger starts from the next user action (typically pressing a button to submit the data or
execute the program).

47
© Copyright 2021, ZaranTech LLC. All rights reserved.
2 Understanding the Debugger

48
© Copyright 2021, ZaranTech LLC. All rights reserved.
2 Understanding the Debugger

• Once the Debugger starts, you can use it to analyze the code and determine how to solve any
problems.
• By default, the Debugger displays the source code in the screen area on the left (this is
dependent on the release of the system, and the source code may display at the top of the
screen instead). In the source code, a yellow arrow indicates the line of code that is to be
executed next (see the figure Understanding the Debugger).
• This indicator allows you to track the program’s progress.

49
© Copyright 2021, ZaranTech LLC. All rights reserved.
2 Moving Through Code in the Debugger

50
© Copyright 2021, ZaranTech LLC. All rights reserved.
2 Moving Through Code in the Debugger

• The toolbar in the Debugger contains some similar-looking buttons. These buttons are important
because they allow you to ‘walk through’ the program.
• Two of the most commonly used buttons are the (Single Step) and (Continue) buttons.
• The Single Step button steps through the source code one statement at a time.
• However, you do not have to step through every statement in a program. You may have seen
enough to solve your problem or understand the program logic.
• In this case, choose the Continue button.

51
© Copyright 2021, ZaranTech LLC. All rights reserved.
2 Moving Through Code in the Debugger

• The Continue button behaves differently depending on the settings you have made.
• If you choose it, and there is a breakpoint later in the code, the Debugger continues to the next
breakpoint and stops.
• If there are no breakpoints (or watchpoints, which are discussed later in this lesson), the
Debugger continues to the end of the executable program and the output displays.

52
© Copyright 2021, ZaranTech LLC. All rights reserved.
2 Different Types of Breakpoint

53
© Copyright 2021, ZaranTech LLC. All rights reserved.
2 Different Types of Breakpoint

• Breakpoints allow you to mark lines in a program where you want the Debugger to stop.
• You know how to set a breakpoint in the ABAP Editor before executing the program, but you can
also set a breakpoint while the Debugger is active.
• This type of breakpoint is known as a Debugger breakpoint
• As with breakpoints that are set in the Editor, you can set a Debugger breakpoint by clicking in
the beige status column beside a line of code.
• By default, lines of code that are assigned a Debugger breakpoint are highlighted in green to
differentiate them from breakpoints assigned in the Editor.

54
© Copyright 2021, ZaranTech LLC. All rights reserved.
2 Different Types of Breakpoint

• If you set additional breakpoints (either Editor or Debugger breakpoints), the Continue button
causes the program to proceed until it reaches the next breakpoint.
• This means that breakpoints allow you to speed through sections of code that you do not need
to analyze and focus on sections that require examination.
• While in the Debugger, you can also set a breakpoint by choosing Breakpoints→ Breakpoint at.
This method provides several options for setting breakpoints.

55
© Copyright 2021, ZaranTech LLC. All rights reserved.
2 Watchpoints

56
© Copyright 2021, ZaranTech LLC. All rights reserved.
2 Different Types of Breakpoint

• Watchpoints are similar to breakpoints: a watchpoint can also be used to specify where the
Debugger should stop.
• However, while breakpoints are set at either a specific line of code or an ABAP keyword, a
watchpoint tells the Debugger to watch a variable and to stop the program when the contents of
that variable change.
• To set a watchpoint, you must be in the Debugger. In the toolbar of the Debugger, choose
Watchpoint. The Create Watchpoint dialog box appears.

57
© Copyright 2021, ZaranTech LLC. All rights reserved.
2 Different Types of Breakpoint

• The easiest way to create a watchpoint is to enter the name of the variable you want to watch
(see the figure Watchpoints), then choose the Check button. This ensures that you have entered
the variable name correctly and that the Debugger recognizes the variable name.
• After this, choose Create and Close Window.
• Watchpoints are useful when you are debugging a complex program, since the contents of a
variable could change frequently.
• With watchpoints, you can avoid stepping through the code one line at a time. Instead, you can
watch the variables that you are interested in and focus on statements that change those
variables.

58
© Copyright 2021, ZaranTech LLC. All rights reserved.
Thank you
Subscribe to our Channel for more Informative Videos.
https://www.youtube.com/user/ZaranTech

You might also like