Beginner's Guide to ABAP

  ·   21 min read

Introduction to ABAP #

What is ABAP? #

ABAP (Advanced Business Application Programming) is a high-level programming language developed by SAP. It’s specifically designed for building and extending SAP’s enterprise resource planning (ERP) software. ABAP programs interact with SAP’s database and its various modules (like Finance, Sales, and HR) to provide customized functionality and automate business processes. While object-oriented programming (OOP) concepts are now central to modern ABAP, it retains aspects of its procedural roots, offering a relatively straightforward syntax for those familiar with other imperative programming languages.

Why learn ABAP? #

Learning ABAP opens up numerous career opportunities in the SAP ecosystem, a large and globally influential sector. Many organizations rely heavily on SAP systems, leading to a consistent demand for skilled ABAP developers to customize, maintain, and enhance these crucial business applications. ABAP developers are responsible for creating solutions that directly impact business operations, making it a rewarding career path for those interested in both programming and business processes. Furthermore, understanding ABAP facilitates efficient interaction with SAP systems, allowing for better problem-solving and system administration.

ABAP’s role in SAP systems #

ABAP is the primary programming language used to develop and customize SAP applications. It provides the tools to:

  • Create custom reports: Extract and present data from SAP systems according to specific business needs.
  • Develop new functionalities: Extend the standard SAP functionality to address unique organizational requirements.
  • Integrate systems: Connect different SAP modules or external systems to facilitate data flow and process automation.
  • Enhance user interfaces: Improve the user experience by creating custom screens and interactive elements within SAP applications.
  • Automate tasks: Streamline repetitive processes using ABAP programs to improve efficiency.

Essentially, ABAP is the engine that drives much of the customization and extension within the SAP landscape.

Setting up your Development Environment #

Setting up your ABAP development environment typically involves accessing an SAP system, either through a local installation (for learning and development) or via a remote connection to a server. The specific steps depend on your access rights and your organization’s infrastructure. Generally, you’ll need:

  • Access to an SAP system: This requires an account with appropriate authorization to create and execute ABAP programs. Your employer or educational institution will provide this access.
  • SAP GUI: The SAP GUI (Graphical User Interface) is the primary tool for interacting with the SAP system, including developing and executing ABAP programs. This software is provided by SAP and needs to be installed on your computer.
  • ABAP Development Tools (ADT): While the traditional SAP GUI can be used for ABAP development, the modern approach is to use the ABAP Development Tools (ADT) within an Eclipse-based IDE (Integrated Development Environment). ADT provides improved features such as code completion, debugging, and version control integration. Installation instructions for ADT can be found on the SAP website.
  • Understanding of your organization’s security policies: Access to SAP systems is usually governed by strict security policies. Familiarize yourself with these policies and follow them diligently.

Detailed instructions for setting up a development environment may be specific to your organization or learning platform. Consult the relevant documentation or support resources provided by your instructor or employer.

Basic ABAP Syntax and Data Types #

Writing your first ABAP program #

The simplest ABAP program consists of a REPORT statement followed by a WRITE statement. This program will display a message on the screen. Here’s an example:

REPORT zfirst_program.

WRITE: / 'Hello, ABAP!'.

To execute this program:

  1. Access your ABAP development environment (see the previous section on setting up your development environment).
  2. Create a new ABAP program (the exact method depends on your development tool – ADT or SAP GUI). Give it a name that starts with Z (this is a common convention for custom programs).
  3. Paste the code above into the program editor.
  4. Activate the program (usually by clicking a button or using a menu option).
  5. Execute the program (again, the method depends on your development tool).

You should see “Hello, ABAP!” displayed in the output window. The REPORT statement declares the program, and the WRITE statement outputs text to the screen. The / after WRITE indicates a new line.

Understanding data types (integers, strings, etc.) #

ABAP supports various data types to represent different kinds of data. Here are some fundamental types:

  • i (Integer): Represents whole numbers (e.g., 10, -5, 0).
  • p (Packed Number): Represents numbers with decimal places, efficient for storing numerical data.
  • f (Floating Point Number): Represents numbers with decimal places, suitable for scientific calculations.
  • c (Character): Represents a single character (e.g., ‘A’, ‘b’, ‘5’).
  • string (String): Represents a sequence of characters (e.g., ‘Hello World’).
  • d (Date): Represents a date.
  • t (Time): Represents a time.

The choice of data type depends on the nature of the data being stored and processed. Using the appropriate data type improves efficiency and reduces errors.

Declaring variables #

Variables in ABAP are declared using the DATA statement. The syntax is:

DATA <variable_name> TYPE <data_type>.

For example:

DATA: lv_integer TYPE i,
      lv_string TYPE string,
      lv_packed TYPE p DECIMALS 2. "Packed number with 2 decimal places

This declares three variables: lv_integer (integer), lv_string (string), and lv_packed (packed number with two decimal places). The prefix lv_ is a common convention for local variables. You can assign values to variables using the =, assignment operator.

Basic Operators #

ABAP supports standard arithmetic, comparison, and logical operators.

  • Arithmetic Operators: + (addition), - (subtraction), * (multiplication), / (division), MOD (modulo).
  • Comparison Operators: = (equals), <> (not equals), > (greater than), < (less than), >= (greater than or equals), <= (less than or equals).
  • Logical Operators: AND, OR, NOT.

Example:

DATA: lv_a TYPE i VALUE 10,
      lv_b TYPE i VALUE 5,
      lv_result TYPE i.

lv_result = lv_a + lv_b.  "Addition
WRITE: / lv_result.

IF lv_a > lv_b.      "Comparison
  WRITE: / 'lv_a is greater than lv_b'.
ENDIF.

This code performs addition and a comparison, demonstrating the use of arithmetic and comparison operators. Remember to always choose appropriate data types for variables to avoid runtime errors.

Control Structures #

IF-ELSE statements #

IF-ELSE statements control the flow of execution based on conditions. The basic syntax is:

IF <condition>.
  <statements>
ELSEIF <condition>.
  <statements>
ELSE.
  <statements>
ENDIF.

<condition> is a Boolean expression that evaluates to true or false. <statements> are the ABAP statements to be executed if the corresponding condition is true. The ELSEIF part is optional and can be repeated multiple times. The ELSE part is also optional and executes if none of the preceding conditions are true.

Example:

DATA: lv_age TYPE i VALUE 25.

IF lv_age < 18.
  WRITE: / 'Minor'.
ELSEIF lv_age >= 18 AND lv_age < 65.
  WRITE: / 'Adult'.
ELSE.
  WRITE: / 'Senior'.
ENDIF.

Loops (DO, WHILE, FOR) #

ABAP offers several loop constructs:

  • DO loop: Executes a block of code a fixed number of times.
DATA: lv_count TYPE i.

DO 10 TIMES.  "Executes 10 times
  lv_count = lv_count + 1.
  WRITE: / lv_count.
ENDDO.
  • WHILE loop: Executes a block of code as long as a condition is true.
DATA: lv_count TYPE i VALUE 0.

WHILE lv_count < 5.
  lv_count = lv_count + 1.
  WRITE: / lv_count.
ENDWHILE.
  • LOOP AT (for internal tables): Iterates over the rows of an internal table. This is crucial for processing data stored in internal tables. (Internal tables are covered in later sections)
"Example requires an internal table 'itab' to be defined and populated beforehand.
LOOP AT itab INTO DATA(ls_itab).
  WRITE: / ls_itab-field1, ls_itab-field2. " Access fields of the table line
ENDLOOP.
  • FOR loop: Iterates over a range of values. Less commonly used in ABAP compared to LOOP AT for table processing.

CASE statements #

CASE statements provide a concise way to handle multiple conditions. The syntax is:

CASE <expression>.
  WHEN <value1>.
    <statements>
  WHEN <value2>.
    <statements>
  WHEN OTHERS.
    <statements>
ENDCASE.

<expression> is an expression that evaluates to a specific value. Each WHEN clause specifies a value to be compared against the expression. The WHEN OTHERS clause is optional and executes if none of the other conditions are met.

Example:

DATA: lv_day TYPE c VALUE 'MON'.

CASE lv_day.
  WHEN 'MON'.
    WRITE: / 'Monday'.
  WHEN 'TUE'.
    WRITE: / 'Tuesday'.
  WHEN OTHERS.
    WRITE: / 'Other weekday'.
ENDCASE.

Remember to always use appropriate indentation to make your code readable and maintainable. Choosing the correct control structure depends on the specific logic you need to implement in your ABAP program.

Working with Internal Tables #

Internal tables are fundamental data structures in ABAP, used to store collections of data. They are similar to arrays or lists in other programming languages. Understanding internal tables is crucial for processing large datasets within ABAP programs.

Declaring and initializing internal tables #

Internal tables are declared using the DATA statement with the TYPE TABLE OF keyword. You specify the line type (structure or elementary data type) that each entry in the table will hold. There are different table types, but for beginners, STANDARD TABLE is a good starting point.

TYPES: BEGIN OF ty_flight,
         carrid TYPE string,
         connid TYPE string,
       END OF ty_flight.

DATA: gt_flights TYPE STANDARD TABLE OF ty_flight. " Declares an internal table

This code first defines a structure ty_flight with fields carrid and connid (presumably for carrier ID and connection ID). Then, it declares an internal table gt_flights that will hold entries of this structure. STANDARD TABLE means that the table is not sorted automatically and entries maintain their insertion order. gt_ is a common prefix for global tables.

Adding and deleting table entries #

  • Adding entries: The APPEND statement adds a single line to the end of a table. You can use the INTO addition to populate the table line directly from a structure or work area.
DATA: ls_flight TYPE ty_flight.

ls_flight-carrid = 'LH'.
ls_flight-connid = '4711'.
APPEND ls_flight TO gt_flights.

"Alternatively, using a more concise notation
APPEND VALUE #( carrid = 'UA' connid = '1234' ) TO gt_flights.
  • Deleting entries: The DELETE statement removes entries from an internal table. You can delete individual lines using a WHERE clause or all lines at once.
DELETE gt_flights WHERE carrid = 'LH'. "Deletes all lines with carrid = 'LH'
DELETE gt_flights.                 "Deletes all lines

Reading data from internal tables #

You can access individual table entries using the READ TABLE statement.

READ TABLE gt_flights INTO ls_flight WITH KEY carrid = 'UA'.
IF sy-subrc = 0.  " sy-subrc indicates success (0) or failure (4)
  WRITE: / ls_flight-connid.
ELSE.
  WRITE: / 'Flight not found'.
ENDIF.

This code attempts to read a table line where carrid is ‘UA’. The system variable sy-subrc tells you if the read was successful (0) or unsuccessful (4).

Looping through internal tables #

The most common way to process all lines of an internal table is using the LOOP AT statement. We already saw an example in the Control Structures section. Let’s expand on it:

LOOP AT gt_flights INTO ls_flight.
  WRITE: / ls_flight-carrid, ls_flight-connid.
ENDLOOP.

This code iterates through each line of gt_flights, placing each line into the work area ls_flight. You can then access the fields of each line within the loop. Remember to always define a work area with the same structure as the table line type. Using DATA(ls_flight) instead of DATA ls_flight is a modern, concise method of declaring and initializing the work area within the loop statement.

These are the basic operations for handling internal tables. Further topics, like sorted tables, hashed tables and other table operations, are covered in advanced ABAP tutorials. Remember to consult SAP’s official documentation for a complete understanding of internal table functionalities and their variations.

Database Interactions #

ABAP provides several ways to interact with the SAP database. This section focuses on the basics of interacting with the database using Open SQL.

SELECT statements #

The SELECT statement retrieves data from database tables. It’s the most common way to access data in ABAP.

SELECT carrid connid
  FROM scarr
  INTO CORRESPONDING FIELDS OF TABLE gt_flights
  WHERE carrid = 'LH'.

This example selects the carrid and connid fields from the scarr table (which likely contains airline carrier data) and places them into the internal table gt_flights. The WHERE clause filters the results to only include entries where carrid is ‘LH’. INTO CORRESPONDING FIELDS OF TABLE efficiently maps the selected fields to the corresponding fields in the internal table structure.

If you only need a single row, you can use INTO instead of INTO CORRESPONDING FIELDS OF TABLE:

SELECT SINGLE carrid connid
  FROM scarr
  INTO @DATA(ls_flight)
  WHERE carrid = 'LH'.

IF sy-subrc = 0.
  WRITE: / ls_flight-carrid, ls_flight-connid.
ENDIF.

Here, a single line is read into a structure ls_flight. The @DATA syntax allows concisely creating the structure ls_flight at runtime.

INSERT, UPDATE, DELETE statements #

These statements modify data in database tables. They require appropriate authorization.

  • INSERT: Adds a new row to a table.
INSERT INTO scarr VALUES ('AZ', 'Air Zaïre', 'KIN', ...). " Requires all fields to be populated
  • UPDATE: Modifies existing rows.
UPDATE scarr SET carrname = 'New Airline Name' WHERE carrid = 'AZ'.
  • DELETE: Removes rows from a table.
DELETE FROM scarr WHERE carrid = 'AZ'.

Important: Always use appropriate error handling (checking sy-subrc) after INSERT, UPDATE, and DELETE statements to ensure the operations were successful. In production code, you would wrap these commands within a suitable exception handling mechanism using TRY...CATCH blocks.

Open SQL vs Native SQL #

ABAP offers two ways to interact with the database:

  • Open SQL: A higher-level interface that provides a database-independent way to access data. It’s easier to use and maintain, making it the preferred choice for most development tasks. The examples above all used Open SQL.

  • Native SQL: A lower-level interface that allows direct execution of database-specific SQL statements. It offers more flexibility but is database-dependent and generally more complex. Use Native SQL only when Open SQL is insufficient for your needs. It is typically used when performance optimization is paramount or when interacting with non-SAP databases.

For beginners, mastering Open SQL is sufficient for most database interaction needs within the SAP environment. Native SQL should be approached with caution and a deep understanding of the underlying database system. Always prioritize Open SQL for its portability and easier maintenance.

Subroutines and Modularization #

Modularizing your code into reusable subroutines is crucial for creating well-structured, maintainable, and efficient ABAP programs. This section introduces subroutines, also known as FORM routines.

Creating and using subroutines (FORM routines) #

Subroutines are blocks of code that perform a specific task. They improve code organization and reusability. A subroutine is defined using the FORM and ENDFORM statements.

FORM calculate_sum.
  DATA: lv_sum TYPE i.
  lv_sum = lv_a + lv_b.
  WRITE: / 'Sum:', lv_sum.
ENDFORM.

This defines a subroutine named calculate_sum that calculates and displays the sum of two variables (lv_a and lv_b). These variables must be declared and assigned values in the main program before calling the subroutine. To use this subroutine:

DATA: lv_a TYPE i VALUE 10,
      lv_b TYPE i VALUE 20.

PERFORM calculate_sum.

The PERFORM statement calls the subroutine.

Passing parameters to subroutines #

To make subroutines more flexible, you can pass parameters. This allows the subroutine to work with different data without modification. Parameters are declared within the FORM statement using the USING addition.

FORM calculate_sum USING p_a TYPE i p_b TYPE i CHANGING p_sum TYPE i.
  p_sum = p_a + p_b.
ENDFORM.

DATA: lv_a TYPE i VALUE 10,
      lv_b TYPE i VALUE 20,
      lv_sum TYPE i.

PERFORM calculate_sum USING lv_a lv_b CHANGING lv_sum.
WRITE: / 'Sum:', lv_sum.

Here, lv_a and lv_b are passed as input parameters (USING), and lv_sum is a changing parameter (CHANGING), meaning its value can be modified within the subroutine and this change will be reflected in the calling program. USING parameters are input only.

Importance of modular code #

Modular code offers several advantages:

  • Reusability: Subroutines can be used multiple times in the same program or in different programs, reducing code duplication and maintenance effort.
  • Readability and maintainability: Breaking down a large program into smaller, well-defined modules makes the code easier to understand, debug, and modify.
  • Testability: Individual modules can be tested independently, simplifying the overall testing process.
  • Improved organization: Modular code promotes a clear and logical structure, improving code organization and reducing complexity. It improves collaboration and understanding between developers.
  • Efficiency: By separating functions into reusable modules, you avoid recalculating results repeatedly, which increases efficiency.

Well-structured, modular code is essential for building robust and maintainable ABAP applications. Using subroutines effectively is a key step in achieving this goal. As you progress in ABAP development, you will explore more advanced modularization techniques, including classes and methods (object-oriented programming).

Object-Oriented ABAP (OOABAP) - Introduction #

Object-Oriented ABAP (OOABAP) is a powerful paradigm shift from the procedural style of traditional ABAP. It provides a more structured and maintainable way to build complex applications. This section offers a beginner’s introduction to the core OOABAP concepts.

Classes and Objects #

In OOABAP, a class is a blueprint for creating objects. A class defines the structure and behavior of objects. It specifies the data (attributes) and actions (methods) that objects of that class will have. An object is an instance of a class—a concrete realization of the class blueprint. Think of a class as a cookie cutter and objects as the cookies it produces. All the cookies have the same shape, but they are individual entities.

CLASS zcl_flight DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS class_method.
    METHODS: constructor,
             get_flight_data.
    DATA: carrid TYPE string,
          connid TYPE string.
ENDCLASS.

CLASS zcl_flight IMPLEMENTATION.
  METHOD constructor.
    carrid = 'LH'.  "Setting default values in the constructor.
    connid = '1234'.
  ENDMETHOD.

  METHOD get_flight_data.
    WRITE: / carrid, connid.
  ENDMETHOD.

  METHOD class_method. "Example of a class method
    WRITE: / 'This is a class method.'.
  ENDMETHOD.
ENDCLASS.

This code defines a class zcl_flight. The DEFINITION section declares the class structure, including methods (constructor, get_flight_data, class_method) and attributes (carrid, connid). The IMPLEMENTATION section provides the actual code for the methods. The constructor method is a special method that is called when an object is created.

Methods and Attributes #

  • Attributes: Attributes are data elements that store information about an object. In the zcl_flight class, carrid and connid are attributes. They hold data specific to a flight.

  • Methods: Methods are functions that operate on the object’s data (attributes). get_flight_data is a method that accesses and displays the carrid and connid attributes. Methods encapsulate the object’s behavior.

Creating an object and calling its methods:

DATA lo_flight TYPE REF TO zcl_flight.
CREATE OBJECT lo_flight. "Creates an instance of zcl_flight. Constructor is called.
CALL METHOD lo_flight->get_flight_data.

Inheritance and Polymorphism (brief overview) #

  • Inheritance: Inheritance allows you to create new classes (child classes) based on existing classes (parent classes). The child class inherits the attributes and methods of the parent class and can add its own. This promotes code reuse and reduces redundancy.

  • Polymorphism: Polymorphism means that objects of different classes can respond to the same method call in their own specific way. For instance, if you had a zcl_train class and a zcl_bus class, both inheriting from a parent class zcl_vehicle, you could call a get_travel_data method on objects of both classes and get different, relevant data for each.

These are advanced concepts; a deeper dive into inheritance and polymorphism is needed in further study of OOABAP. However, understanding these basic principles of classes, objects, methods, and attributes will allow you to start writing more structured and maintainable ABAP code using the OO paradigm. As you progress, you’ll explore interfaces, events, and exceptions to fully harness the power of OOABAP.

Debugging and Troubleshooting #

Debugging is an essential skill for any ABAP developer. This section provides a basic introduction to debugging in ABAP and discusses some common errors.

Using the ABAP Debugger #

The ABAP Debugger is an interactive tool that allows you to step through your code line by line, inspect variables, and identify the source of errors. You can access the debugger in several ways:

  • Setting breakpoints: Place breakpoints in your code at strategic points where you want execution to pause. In the ABAP Development Tools (ADT), you can click in the gutter next to the line number to set a breakpoint. In the SAP GUI, you can use the BREAKPOINT statement in your code.

  • Starting the debugger: When the program reaches a breakpoint, execution pauses, and the debugger opens.

  • Stepping through the code: Use the debugger’s controls (Step Over, Step Into, Step Out) to execute your code step by step. Step Over executes the current line and moves to the next, while Step Into goes into subroutines or methods. Step Out executes the rest of the current subroutine or method and returns to the caller.

  • Inspecting variables: The debugger’s watch window allows you to examine the values of variables at any point during execution. You can add variables to the watch window or simply hover over them in the code to see their current values.

  • Modifying variables: In some cases, you might modify the value of a variable during debugging to test different scenarios. Use caution when modifying variables, as it may affect the subsequent flow of execution.

  • Debugging exceptions: If your program throws an exception, the debugger will usually stop at the point where the exception occurred.

The specific user interface for the debugger depends on whether you are using ADT or the older SAP GUI. Familiarize yourself with the debugger interface provided by your development environment. Efficient use of the debugger is crucial for quickly identifying and resolving issues in your code.

Common ABAP Errors and How to Fix Them #

Here are some common ABAP errors and how to approach them:

  • Short dump: A runtime error that abruptly terminates the program. Short dumps typically provide detailed information about the error, including the exact line number and the type of error. Analyze the dump message carefully to identify the root cause. Common causes include:

    • Division by zero: Check for potential divisions by variables that could be zero.
    • Accessing an invalid table index: Verify that array or table indexes are within the valid range.
    • Incorrect data type conversion: Ensure that data type conversions are valid and handle potential errors.
    • Null pointer dereference: Check for dereferencing NULL references. (Especially relevant in OOABAP)
  • Syntax errors: These occur during program activation. The ABAP compiler highlights the syntax errors, providing clues about the problem. Correct the highlighted errors, making sure to pay attention to case sensitivity and proper keyword usage.

  • Runtime errors (non-short dumps): These errors don’t necessarily halt the program but might produce incorrect results. They are often subtle and require careful investigation using the debugger.

  • Authorization errors: These errors occur if your user lacks the necessary authorization to perform certain operations (e.g., accessing a database table). Check your authorizations or contact your system administrator to request the necessary permissions.

  • Logic errors: These errors are the most challenging to debug because they produce incorrect results without producing explicit error messages. Thorough testing, the use of the debugger, and careful code review are crucial for identifying logic errors.

When encountering errors, systematically investigate the problem. Start by examining the error messages, then use the debugger to step through your code and examine variables. If you’re still stuck, search for the error message online or consult ABAP documentation and online forums. The key is to develop a systematic approach to debugging so you can efficiently identify and correct issues in your code.

Further Learning Resources #

This section lists resources to help you continue your ABAP learning journey beyond this beginner’s guide.

Official SAP Documentation #

SAP provides comprehensive documentation for its products, including ABAP. This documentation is your primary source for detailed information on ABAP language features, functionalities, and best practices. The documentation is available on the SAP Help Portal. While it can be extensive, it’s the most authoritative source and covers a broad range of topics, from basic syntax to advanced concepts. Use the search functionality effectively to find specific information. Focus your searches on keywords related to your specific questions or areas of interest. Look for tutorials and example codes within the documentation for practical guidance. The SAP Help Portal also often includes links to relevant SAP notes, which may address specific known issues or provide further explanations.

Online Courses and Tutorials #

Numerous online platforms offer ABAP courses and tutorials, catering to different skill levels and learning styles. These resources provide structured learning paths, often including practical exercises and assessments to reinforce your understanding. Some popular platforms include:

  • OpenSAP: SAP’s own open learning platform offers free courses on various topics, including ABAP. These courses frequently provide a good introduction to the language and its fundamentals.

  • Udemy, Coursera, edX: These platforms host a variety of paid and sometimes free ABAP courses, often taught by experienced instructors. You can filter courses by skill level, duration, and specific topics of interest to you. Check reviews to choose courses with high ratings and positive feedback from other learners.

  • YouTube: Many instructional videos on ABAP are available on YouTube. These videos often provide concise explanations of specific concepts or offer walk-throughs of practical examples. Remember to evaluate the source and credibility of the videos before relying on them as primary learning resources.

ABAP Community Forums #

Engaging with the ABAP community is invaluable for learning and problem-solving. Forums provide a platform to connect with other ABAP developers, ask questions, share knowledge, and learn from others’ experiences. Some popular forums include:

  • SAP Community: SAP’s official community forum is a rich source of information on ABAP and other SAP technologies. It’s a good place to ask questions, find solutions to common problems, and participate in discussions with experienced developers. Familiarize yourself with the forum’s structure and search capabilities to efficiently find information relevant to your needs.

  • Other online forums: Various other online forums and discussion groups focus specifically on ABAP development. Search for these online using search terms like “ABAP forum” or “ABAP community”. Be aware that the quality and expertise within these forums can vary; critically evaluate the answers and information you receive.

By actively utilizing these resources – SAP’s official documentation, online courses, and community forums – you can continuously expand your ABAP skills and expertise beyond this introductory guide. Remember that consistent learning and practice are key to mastering ABAP development.