The soaring demand for SAP has led companies to become completely autonomous. ABAP is one programming language that pertains only to the SAP space and is therefore responsible for the development of SAP applications. To help individuals gain maximum insights about SAP ABAP, we have prepared this SAP ABAP tutorial.
- Updated on: 04th Jul, 19
- 1980 Views
Introducing SAP ABAP – SAP ABAP Tutorial
SAP ABAP or Advanced Business Applications Programming Language is a fourth-generation programming language that is used predominantly in the SAP world for developing and customizing SAP server applications and the SAP software itself. Positioned along side JAVA, SAP ABAP has carved a niche for itself owing to the diverse set of functionalities it brings to the table for SAP application server programming and development of enterprise applications for major players on the SAP platform.
Check this Intellipaat video of SAP HANA:
To understand the concepts of SAP ABAP in a detailed and constructive manner, let us first drill down deep into SAP’s 3-tier architecture.
All ABAP Programs run at the application layer. However, all these 3 layers can be installed on top of each other on a single computer or installed individually on different computers. ABAP programs are contained within the SAP database and executed by runtime systems which are a part of the SAP Kernel. For this reason, ABAP is different from other programming languages like C++ and Java. While C++ and Java programs are stored as external files, ABAP programs exist within the database in two forms specifically.
- Source Code: SAP ABAP Workbench tools can be used to view and edit these codes.
- Generated Code: These codes are the binary representations of a program and resemble with JAVA byte code.
The most striking feature about SAP ABAP programs is the fact that a single program can be written and leveraged to operate around a diverse range of databases. This is made possible by SAP ABAP’s inherent database interface. This interface converts database independent systems into such statements that are understood by the underlying database.
Before moving on to coding in ABAP, let us first understand how the ABAP editor works in the first place.
SAP ABAP Editor
It is important to note that all ABAP programs start with an ABAP keyword and terminate with a full stop (.) Codes should be entered through the ABAP editor. This editor is offered to users as a part of ABAP Tools which is delivered with the SAP Net Weaver Application Server ABAP (AS ABAP).
AS ABAP is an application server with its own database, run-time environment, and development tools including the ABAP Editor. ABAP Editor is a development platform which is independent of hardware, operating systems, and databases. If your will to preparing for SAP job please go through this Top SAP ABAP Interview Questions and Answers
To further enhance your understanding, we will create a Report containing an executable program using ABAP Editor. There are certain steps you need to follow while writing a program:
- Use Transaction Code ‘SE38’ to open the SAP ABAP Editor.
- In the input field ‘Program’, specify the name of the report and make sure that your report name is not similar with Customer or Partner Objects.
- Click on ‘Create’ button and choose ‘Executable Program’ from the pop-up window that appears after specifying the name of the report.
- Enter the title of your program and click on ‘Save’ to continue.
- And then you can type in your program.
E.g: REPORT ZABAP1233. WRITE ‘HELLO ABAP USER’.
SAP ABAP Datatypes – SAP ABAP Tutorial
We will also cover datatypes in this SAP ABAP Tutorial. There are several Datatypes that are used in SAP ABAP. Here’s a table that specifies the different types that are used around SAP ABAP.
|Data Type||Initial Field Length||Valid Field|
|P||8||1 to 16||0||Packed Number|
But how can we assign values to variables?
Well, to do that, you can follow the following syntaxes.
A=16. move 16 to A. write A to B.
For conducting arithmetic operation, use the keyword ‘Compute’
E.g, compute A=A*100.
Control Statements-SAP ABAP Tutorial
While coding, several situations arise where a line of code, or a group of coding lines is supposed to be executed multiple times to fetch the required output. In these instances, Control Statements are used to make sure that the program stays modular while producing the desired outputs.
There are several control statements in ABAP Programming:
- While Loop: This is entry condition-controlled loop which checks for the condition at the beginning of the loop block and executes statements within the block only when the condition is validated.
- Do Loop: This is an exit condition-controlled which functions the same as a While Loop but checks for the validity of condition at the end of the loop block. This means that no matter what, the block of the loop will be executed at least once.
- Nested Loop: These types of loops are used when users are supposed to execute statements repeatedly within another loop. Both While and Do Loops can be nested if required. Program execution doesn’t change however.
- .end..if Statement: This statement is used in places where different statements are supposed to be executed when their respective conditions are validated.
- Case Statement: This statement is leveraged when there is only condition, but execution statements are determined at specific instances of the case variable.
In addition to these, there are 3 Loop Control Statements:
- Continue: This statement makes the control flow skip the following loop statements and check for the next looping condition.
- Check: When a condition is deemed false, ‘Check’ statement makes sure that control flow skips loop statements that follow it inside the looping block.
- Exit: This statement terminates the loop then and there.
Interested in learning SAP FICO? Enroll in our SAP FICO training course now!
SAP ABAP Operators – SAP ABAP Tutorial
There are 4 types of Operators that pertain to SAP ABAP. This SAP ABAP Tutorial helps you understand them.
- Arithmetic Operators: These operators are used in mathematical expressions and function the same as algebraic operators.
- + (Addition): This operator adds values or variables on either side of it.
- – (Subtraction): This operator subtracts values or variables on either side of it.
- * (Multiplication): This operator multiplies values or variables on either side of it.
- / (Division): This operator divides values or variables on either side of it.
- MOD (Modulus): This operator divides right hand operand by left hand operand and return their reminder.
- Comparison Operators: These operators compare between multiple operands and return the results in either true or false.
- EQ or =: Checks if values of two operands are equal or not.
- NE or <>: Checks if values of two operands are not equal or not.
- GT or >: Checks which operand is greater.
- LT or <: Checks which operand is lower.
- GE or >=: Returns true if the left-hand operand is greater than or equal to the right-hand operand.
- LE or <=: Returns true if the left-hand operand is lower than or equal to the right-hand operand.
- A1 between A2 and A3: Returns true only when A1 lies in between A2 and A3.
- IS INITIAL: Returns true if value of a variable is not changed after execution.
- IS NOT INITIAL: Returns true if value of a variable is changed after execution.
If the data type or length of an operand does not match with the other operand, then type conversion is performed implicitly. The operand with lower length automatically gets converted to the type of the operand with larger length.
- Bitwise Operators: These operators convert operands to bits and compare between bits to build Boolean Algebraic Expressions.
- BIT-NOT: This is a Unary Operator that converts all bits in a hexadecimal number to the opposite value.
- BIT-AND: This binary operator performs AND operation on each bit of both operands individually and returns a value.
- BIT-OR: This binary operator performs OR operation on each bit of both operands individually and returns a value.
- BIT-XOR: This binary operator performs Exclusive OR operation on each bit of both operands individually and returns a value.
- Character String Operators: These operators are directly applied to strings. There are several types of Character String Operators:
- CO (Contains Only): Checks if string ‘A’ is composed only from the characters of string ‘B’.
- CN (Not Contains Only): Checks if string ‘A’ is composed from at least one character of string ‘B’.
- CA (Contains Any): Checks if string ‘A’ contains any character that also string ‘B’ contains.
- NA (Not Contains Any): Checks if string ‘A’ contains no character that also string ‘B’ contains.
- NS (Not Contains a String): Checks if string ‘A’ contain no character that also string ‘B’ contains.
- CS (Contains a String): Checks if string ‘A’ contains any character that also string ‘B’ contains.
- CP (Contains a Pattern): Checks if ‘A’ holds the pattern in ‘B’.
- NP (Not Contains a Pattern): Checks if ‘A’ does not hold the pattern in ‘B’.
If you have any technical doubts & queries related to SAP ABAP, post the same on SAP Community.
Exception Handling in SAP ABAP – SAP ABAP Tutorial
Before talking about Exception Handling, I believe we must first understand what Exceptions are in the first place.
Basically, Exceptions are problems that arise during the execution of a program. When exceptions arise, the normal or presumed flow of a program is disrupted and at times the program application terminates. To avoid exceptions, it is necessary to handle them beforehand. Go through the SAP ABAP Training to get clear understanding of Exception Handling.
When it comes to Exception Handling, SAP ABAP pertains to:
- RAISE: This is used to raise exceptions. By raising exceptions, users can indicate the system about exceptional situations that might hamper program execution at run-time.
- TRY: The statements which may raise exceptions are put in this block. It is followed by one or more Catch statements.
- CATCH: This statement is used to catch and handle the exception.
- CLEANUP: Statements in a cleanup block are executed when exception arises in the try block but is not handled by a try-endtry construct.
Data Dictionary in SAP ABAP – SAP ABAP Tutorial
In this SAP ABAP Tutorial, let us learn about Data Dictionary in SAP ABAP in SAP ABAP. As we all know, SQL can be categorized as:
- DML (Data Manipulation Language): Contains query and update commands including SELECT, INSERT, UPDATE, and DELETE among others.
- DDL (Data Definition Language): Contains commands such as CREATE TABLE, CREATE INDEX, DROP TABLE, ALTER TABLE, etc.
While ABAP programs handle the DML part of SQL, the DDL part of SQL is handled by the ABAP Data Dictionary in SAP ABAP.
Mostly viewed as metadata, the SAP ABAP Data Dictionary in SAP ABAP is contained in the SAP database. It is mostly used to create and manage data definitions, and create Tables, Data Elements, Domains, etc.
There are 3 basic types in the SAP ABAP Data Dictionary in SAP ABAP:
- Data Elements: Define elementary type by defining data type, length, and decimal places.
- Structures: Contain variables with any type.
- Table Types: Describe the structure of internal tables.
Howsoever, the first and foremost purpose of Data Dictionary in SAP ABAP is to:
- Enforce Data Integrity
- Manage Data Definitions while removing redundancy
- Integrate with ABAB development workbench.
Tables in SAP ABAP – SAP ABAP Tutorial
Tables in SAP ABAP are different from other tables. If a table is activated in the ABAP Data Dictionary in SAP ABAP, the same table is consequently activated in the SAP database as well. And tables that are activated in the ABAP Data Dictionary in SAP ABAP are automatically converted to a format which is compatible with the SAP database. Tables can contain several fields. Various table fields that are available in SAP ABAP are:
- Field Name: This is the name given to a table field. Max length can be 16 characters.
- Key Flag: This field determines if a field belongs to a key field or not.
- Field Type: Assigns a data type to a field in a table.
- Field Length: Gives information about the number of characters that can be entered in a field.
- Decimal Places: This field defines the number of digits after a decimal point that are supposed to be considered in ABAP programming.
- Short Text: It is kind of a meta-description that describes the meaning of its corresponding field.
For Creating Tables in SAP ABAP, use the T-Code ‘SE11’
SAP ABAP Views
Views in SAP ABAP function as a regular database table. However, Views do not occupy any storage space.
Views can be created by combining the data of one or more tables that contain information about application objects. Users can represent a subset of data which is contained in a table or join two or more tables into a single virtual table using Views. Application object data is distributed among several tables using views using Inner Joins.
Maintenance View is leveraged to display and modify data which is stored in an application object.
Projection View is leveraged to hide unnecessary fields and display only those fields that are relevant.
SAP ABAP Search Help – SAP ABAP Tutorial
Yet another repository In the SAP ABAP Dictionary, Search Help displays all possible values for a field in the form of a list. This list is mostly termed as ‘hit list’.
For creating Search Help,
- Use Transaction Code SE11 and select the radio button for Search Help.
- Enter the name of Search Help and click on ‘Create’.
- Select the Source of Data which may be a table or a view. The selection can be done from the Selection List.
- After that control the appearance of the Search Help by defining type in the Dialog Box.
- After that chose Parameter Area as per your requirements. The parameter field can be selected from Search Help Parameters, Import, Export, LPos, SPos, and Data elements.
- Activate Search Help and press F8 to execute.
SAP ABAP Lock Objects
Offered by the SAP Data Dictionary in SAP ABAP to synchronize data access in cases where the same data is to be used by one or more programs, SAP Lock Objects in SAP ABAP help alleviate inconsistency when data is inserted into or changed in the database. It is necessary to define tables in a Lock Object along with their key fields if their data records are to be locked.
With lock mechanism programs can communicate with each other about data records, and programs can be prevented from reading data which are changed by other programs. Lock requests are generated by the initial program, and the request is transferred to the Enqueue Server. Then the lock is created in the lock table. Lock is set by the Enqueue server and access is provided to the first program.
To Create Lock Objects in SAP ABAP,
- Use the transaction code ‘SE11’. Select the ‘Lock Object’ radio button.
- Enter the name of the lock object and click Create.
- Add a description of the lock object in the description field and then Click on ‘Tables’.
- Enter the Table Name and Set Lock mode as ‘Write Lock’.
- Open the Lock parameter tab and Save them and Activate.
This is how you can create a Lock Object in SAP ABAP.
Note: Lock arguments or key fields of the table are used as input parameters in function modules. These arguments are basically used to add or remove locks.
Modularization – SAP ABAP Tutorial
When it pertains to programming, the process of Modularization is defined as splitting large or complicated blocks of code into simpler or smaller ones that are easy to understand and work with. The same is the case with Modularization in SAP ABAP.
Over here, broken down modules are termed as logical blocks. With modularization, the program becomes easy to read and interpret. Also, redundancy is reduced, resulting in an increase in execution-time, and substantial surge in reusability of the same code. Most SAP ABAP programs are a result of the integration of modularized processing blocks. These blocks are, in most cases, invoked from ABAP Run-time environments including event blocks and dialog modules.
Moreover, source codes in SAP ABAP programs are also modularized. This is done through Local Macros and Global Include Programs.
There are instances where a certain block of code is ought to be used several times in an ABAP program. But there is no need to write them multiple times. This is where Macros take the center stage.
Macros can be useful for long calculation and writing sophisticated WRITE programs. However, it is necessary to define macros first before putting them to use.
Syntax to define Macros in SAP ABAP is:
DEFINE <macro_name>. <statements> END-OF-DEFINITION. …. <macro_name> [<parameter1> <parameter2> ….].
Global Include Programs
Basically, these are global repository objects which are leveraged to modularize the source code of an ABAP program. These programs also allow users to manage complex programs in an ordered and systematic manner. Include programs need to be invoked to other programs using the syntax INCLUDE <program_name>.
These programs cannot run independently and are therefore built into other programs which need to be modularized.
Steps to create and invoke an Include Program:
- Create the necessary Include Program in the SAP ABAP editor and set the Type of the Program under attributes to ‘INCLUDE program’.
- Click on Save and the program will be saved in an Include package.
- Create the program where the Include program you created in the first place is to be used. Set the type of the latter program as ‘EXECUTABLE program’.
- Use the Include program in the executable program using the aforementioned syntax.
- Save, activate and execute the program.
Also, programs can be modularized through processing blocks using subroutines and function modules.
Subroutines are those sections of a code that can be reused. In subroutines, functions are encapsulated as source code and in this way, modularization is achieved. Some parts of a program can be charted out from the main block to a subroutine and reused in other places where the same source code is required to be used.
In instances where a same block of code is to be used in an ABAP program, the block can be paged out to a subroutine and that subroutine can be invoked wherever required. This makes managing complex codes seamless.
Format to define a subroutine: FORM <subroutine_name>. <statements> ENDFORM.
To invoke a subroutine, the keyword PERFORM must be used.
These are nothing but sub-programs that contain a set of reusable statements that include importing and exporting parameters. Executable independently, pre-defined Function Modules can be invoked from any SAP ABAP program.
Related Function modules can be put together into a Function Group, and that group can be used in SAP ABAP programs.
SAP ABAP – Report Programming
Reports are representations of data in an organized manner. Because SAP applications support report creation, Report Programming was ought to be included as a part of SAP ABAP. Reports are generated by using output data in relative WRITE statements within loops. There are certain Standard Reports that SAP provides. These include ‘RSCLTCOP’ and ‘RSPARAM’. While RCLSTOP is used to copy tables across clienteles, RSPARAM is leveraged to display instance parameters.
Mostly, Events are used to create Reports in SAP ABAP.
|INITIALIZATION||This event is triggered before selection screen is displayed.|
|AT-SELECTION-SCREEN||This event is triggered after the selection screen is processed to verify user input prior to execution of a program.|
|START-OF-SELECTION||This event is triggered only after the selection screen is processed, and specifically when users click on Execute.|
|END-OF-SELECTION||This event is triggered after the last statement on START-OF-SELECTION event is executed.|
|TOP-OF-PAGE||This event is triggered by the first WRITE statement to display data on a new page.|
|END-OF-PAGE||This event is triggered to display any text content at the end of a page in a report.|
Dialog Programming – SAP ABAP Tutorial
Dialog programming is basically done to create multiple objects. These objects are linked in hierarchy to the main program and are therefore executed in the specific hierarchy order. Dialog programs contain several components. They are:
- Module Pools
And these components can be leveraged using specific tools.
|All Components||Object Browser||SE80|
|Module Pool||ABAP Editor||SE38|
|Dictionary Objects||ABAP Data Dictionary||SE11|
Dialog programs are created in the object browser to make sure that objects are linked to the main program implicitly.
To create a new Dialog Program,
- Use transaction code SE80 and select ‘Program’ from the dropdown menu and add a name to your program.
- Press Enter and choose ‘WITH TOP INCL’ and click on the ‘Yes’ Button.
- Within attributes, enter a title and click on save.
This is how Dialog Programs are created. Further, you can add screens to dialog programs and format the screen layout as per requirements. You can also create and execute transactions within dialog programs.
Smartforms – SAP ABAP Tutorial
SAP ABAP Smart Forms is a tool that is mostly used to send and print documents. This highly advanced tool can be used to develop forms, PDF files, emails and documents for usage in the Internet. Also, an interface is implicitly available with SAP ABAP Smart Forms to help users maintain the layout and logic of a form which is already built or to be built.
Users can modify existing forms using simple graphical tools within SAP ABAP Smart Forms. This functionality ensures that users with zero programming knowledge can configure forms for streamlining business processes. Data is basically retrieved from both static and dynamic tables.
To create forms using SAP ABAP Smart Forms,
- Use the Smart Forms Builder as the main interface to create a form. The Smart Form Builder is available on the initial screen of SAP ABAP Smart Forms. Use the transaction code ‘SMARTFORMS’ to open the initial SAP ABAP Smart Forms Screen.
- Enter the Form name in the Forms field.
- Select Smart Forms and click on the copy icon to open the copy form.
- Enter the name of the new form in the target object field.
- Click on copy icon on the Copy Form and then hit the Save icon.
- Click on Create. This will open the newly created form in the Smart Forms Builder.
Once you create a new form, you can further create text nodes in the form.
SAP ABAP SAPscript
A tool for managing and building business forms including invoices and purchase orders, SAPscript provides several templates for the same purpose. These templates simplify the designing of business forms substantially.
Standard SAP ABAP SAPscripts are delivered with the SAP Standard Client (client 000). Here’s a list of Standard SAP ABAP SAPscripts that are available with client 000.
|RVORDER01||Sales Order Confirmation Form|
Any SAP ABAP SAPscript has 2 main components:
- Content: Either business data or company logo.
- Layout: Defined by a set of windows in which the form content appears.
SAP ABAP User Exits – SAP ABAP Tutorial
Learn about User Exits in this SAP ABAP Tutorial.
When standard SAP Extractors do not function as expected or fail to come up with the required functionality, SAP ABAP User Exits are leveraged by users. Mostly used in SAP Sales & Distribution (SAP SD) module, SAP ABAP User Exits are provided by SAP in the areas of sales, transportation, shipping and billing.
Here is a list of User Exits that are available to users.
|USEREXIT_FIELD_MODIFICATION||Specifies screen attributes|
|USEREXIT_SAVE_DOCUMENT||Performs internal operations when save is hit|
|USEREXIT_SAVE_DOCUMENT_PREPARE||Checks input fields|
|USEREXIT_MOVE_FIELD_TO_VBAK||Used when user header changes move to header changes area.|
|USEREXIT_MOVE_FIELD_TO_VBAP||Used when user item changes move to SAP Item work area.|
SAP Business Add-Ins (SAP BADI) – SAP ABAP Tutorial
At times, special functions are predefined in an application to enhance their functionalities. SAP facilitates the improvement of predefined functions by providing Business Add-Ins known as BADIs.
Basically, a BADI is an enhancement technique that facilitates an SAP user to add additional codes to existing programs in the SAP system. You may use standard or customized logic to improve the SAP system. A BADI must first be pre-defined. While defining a BADI, an interface is created, and this interface is implemented by one or more adaptor classes.
Users create filter BADIs —BADIs that are defined based on filtered data. Goals of BADI:
- Enhancing the existing and standard SAP applications by adding two new elements in the ABAP language, which go by the name ‘GET BADI’ and ‘CALL BADI’.
- Improving flexibility for the enhancement of standard applications in an SAP system.
SAP Web Dynpro – SAP ABAP Tutorial
Developed by SAP AG, SAP Web Dynpro is the SAP Standard UI technology which is used to develop web-based applications in the SAP ABAP environment buy utilizing SAP development tools and concepts. Within SAP Web Dynpro a front-end web user interface is provided that can directly connect to the backend for accessing data and functions for reporting.
The SAP Web Dynpro contains a run-time environment and a graphical development environment. It also features specific development tools that are integrated with the SAP Workbench.
Few pointers on SAP Web Dynpro:
- It is the SAP NetWeaver programming model for user interfaces
- All applications are listed as per MVC programming model
- Applications can have access to system data
The advantages of Web Dynpro are many. Here’s a few of them:
- Graphical tools are used to reduce the effort which is put in for implementing application functionalities.
- Components are used to enhance reusability and maintenance paradigms.
- Layout and navigation settings can be easily changed.
- Complete integration with SAP Development environment.
So, this was pretty much the basics for working in an SAP ABAP environment. SAP is growing and to foster a career of excellence in the SAP domain, don’t you think learning the language that is used to develop SAP’s application server is handy?