Category: ABAP Beginner

  • SAP Operator: Arithmetic and Comparison Operation

    Preface – This post is part of the ABAP Beginner series.

    SAP Operator: Arithmetic Operators

    Following are the basic SAP Operator using which Arithmetic operations can be performed upon ABAP variables:

    1. Assign Values to variables

    To assign value to a variable, we can use equal to “=” sign or Keyword MOVE.

    DATA:   LV_NUMBER1 TYPE      n,

    LV_NUMBER2   TYPE     n.

    LV_NUMBER1 = 10.

    MOVE LV_NUMBER1 TO LV_NUMBER2.

    WRITE: / LV_NUMBER1, LV_NUMBER2.

    OUTPUT:

    1. Apply Sum, Subtraction, Multiplication and division on two or more variables

    DATA:           LV_NUMBER1 TYPE      n             VALUE  10,

    LV_NUMBER2   TYPE     n             VALUE  20,

    LV_ADD                TYPE     n,

    LV_SUB                 TYPE     n,

    LV_MUL               TYPE     n,

    LV_DIV                 TYPE     n.

    LV_ADD = LV_NUMBER1 + LV_NUMBER2.

    (OR)

    A = ADD LV_NUMBER2 TO LV_NUMBER1.

    LV_SUB = LV_NUMBER1 – LV_NUMBER2.

    (OR)

    B = SUBTRACT LV_NUMBER2 FROM LV_NUMBER1

    LV_MUL = LV_NUMBER1 * LV_NUMBER2.

    (OR)

    C = MULTIPLY LV_NUMBER1 BY LV_NUMBER2

    LV_DIV                 = LV_NUMBER2 / LV_NUMBER1.

    (OR)

    D = DIVIDE LV_NUMBER2 BY LV_NUMBER1

    WRITE: / LV_ADD, LV_SUB, LV_MUL, LV_DIV.

    (OR)

    WRITE: / A, B, C, D.

     

    1. Clear Variables

    Keyword CLEAR is used to clear the values in variable to default values i.e. 0 for numeric and “ ” for character field.

    WRITE: / “BEFORE CLEAR”.

    WRITE: / A, B, C, D.

    CLEAR: A, B, C, D.

    WRITE: / “AFTER CLEAR”.

    WRITE: / A, B, C, D.

     

     

    ABAP MATHS FUNCTIONS

    ABAP provides predefined built-in maths functions that you can use according to your requirement.

    Following are some of the Maths Functions:

    FUNCTIONS SUPPORTED NUMERIC DATA TYPES DESCRIPTION
    ABS ALL TO CALCULATE ABSOLUTE VALUE
    SIGN ALL TO DETERMINE SIGN, RETURNS 1 FOR (+) 0 FOR (-)
    CEIL ALL It gives next highest integer i.e.  ceil (2.1) = 3
    FLOOR ALL It gives next lowest integer  i.e. floor (2.1) = 2
    TRUNC ALL It gives truncated part of the input
    FRAC ALL It gives fractional part of input i.e. FRAC(1.2) = .2
    COS,SIN,TAN F It will implement the given trigonometry function
    EXP F It will implement Exponential function
    LOG F It will implement Natural Log on that number
    SQRT F It will return square root of the input
    MOD N It will return remainder mod(12,5) = 2

     

    SAP Operator: Comparison Operators

    Following are the SAP Operator provided for ABAP Comparison Operation:

    Operator Alternate Form Meaning
    = EQ Equality Test returns 1 if equal
    <> NE Inequality Test return 1 if True
    > GT Greater Than Test
    < LT Less Than Test
    >= GE Greater than or Equal
    <= LE Less than or Equal
    BETWEEN Interval Test returns value between two value
    IS INITIAL Initial Test returns True if variable have no value
    IS NOT INITIAL Not Initial Test returns True if variable is not empty

     

    Apart from above operators ABAP also provide Bitwise Operators and Character String Operators

  • What is ABAP

    Preface – This post is part of the ABAP Beginner series.

    What is ABAP?

    A general interview question: “What is ABAP?”. ABAP stands for Advanced Business Application Programming. It is a 4th Generation (because it is domain specific language) programming language developed by SAP. The ABAP kernel is implemented in C++.ABAP is a hybrid programming language that supports both a procedural and an object-oriented programming model.
    All ABAP programs also called Reports reside inside SAP database. In the database all ABAP code exists in two forms: source code, which can be viewed and edited with the ABAP Workbench tools; and generated code, a binary representation somewhat comparable with Java byte code.

    When you write a code in ABAP editor, it is saved in two files. One is the exact what you have written and other is the code converted in Binary format. The Binary code is the one actually executed by the computer.

    ABAP has both compiler and Interpreter. Firstly, the ABAP compiler translates an ABAP program it into an intermediate language called an ABAP load. Then the ABAP virtual machine, an interpreter for ABAP loads (i.e. it can execute ABAP loads) comes into the picture. SAP has two tables to store these files, ABAP reports in D010S table and ABAP Loads in D010L table.

    ABAP on Application Server ABAP

    ABAP is the programming interface of Application Server ABAP (AS ABAP) in SAP NetWeaver. A prerequisite to use the ABAP programming language is to install an Application Server ABAP. The components of an Application Server ABAP can be organized in the layers (presentation, application, and database) of three-tier client-server architecture in accordance with their tasks.

    • The presentation layer represents the user interface of an AS ABAP (SAP GUI or Web browser)and it is distributed to the workstations of individual users.
    • The application layeris implemented by utilizing one or more application servers. The application layer contains the ABAP runtime environment in which ABAP programs are executed.
    • The database layerconsists of a database system in which the central dataset of an Application Server ABAP is saved.

    Programming Models

    ABAP supports the following:

    • An object-oriented programming model based on classesand interfaces
    • A procedural programming model based on function modulesand subroutines

    Both modules offer interoperability.

    Types of ABAP programs

    ABAP distinguishes two types of executable programs:

    • Reports : Simple Programs which provides Input/output option to user
    • Module pools: Complex Programs which provides different screens for better user experience.

     

    The non-executable program types are:

    • INCLUDE modules : It gets included automatically at generation time; it is often used to subdivide large programs
    • Subroutine pools: It contains ABAP subroutines (enclosed by FORM/ENDFORM statements and invoked via PERFORM).
    • Function groups: These are libraries of self-contained FMs (enclosed by FUNCTION/ENDFUNCTION and invoked via CALL FUNCTION)
    • Object classes: similar to Java Classes &define a set of methods and attributes
    • Interfaces: It contain method definitions without implementations for which any class implementing the interface must provide explicit code.
    • Type pools: Type pools define collections of data types and constants.

     

    Development Environment

    There are two possible ways to develop in ABAP.

    ABAP Workbench

    The ABAP Workbench is part of the SAP ABAP system and is accessed via SAPgui. It contains different tools for editing programs. The most important of these are (transaction codes are shown in parentheses):

    • ABAP Editor(SE38): It is used to write and edit reports, module pools, includes and subroutine pools
    • ABAP Dictionary(SE11): It is used to process database table
    • Menu Painter(SE41): It is used to design the user interface (e.g. menu bar & standard toolbar)
    • Screen Painter(SE51): It is used to design screens and flow logic
    • Function Builder(SE37): for function modules
    • Class Builder(SE24): It is used to construct ABAP Objects classes and interfaces

    The Object Navigator (SE80) provides a single integrated interface to access various tools (including all tools mentioned above).

    ABAP Development Tools

    • Using certain set of plug-in in the Eclipse platform we can develop ABAP programs in eclipse, these are formally known as “ABAP in Eclipse” or ABAP Development Tools (ADT).
    • In this scenario, the ABAP developer installs the required tools on his computer and works locally, whereas a continuous synchronization with the backend is performed.
  • SAP Memory and ABAP Memory: Different types of Memory in SAP

    Preface – This post is part of the ABAP Beginner series.

    SAP Memory and ABAP Memory: Different types of Memory in SAP

    In this post we will discuss SAP Memory and ABAP Memory i.e. ABAP memory organization with reference to the ABAP program; that is, which memory is accessed by an ABAP program. There are three types of memory in SAP system:

    • SAP Memory / Shared Memory
    • User Memory
    • ABAP Memory

    SAP Memory / Shared Memory

    • In our previous post, we talked about sessions. SAP memory will have access of all the main sessions within the SAP GUI.
    • You can use SAP memory either to pass data from one program to another within a session, or to pass data from one session to another.
    • An ABAP program uses SET/GET parameters to access SAP memory. These parameters are set for a particular user or program using the SET PARAMETER statement. Other ABAP programs retrieve the already set parameters using the GET PARAMETER statement.
    • The most frequent use of SPA/GPA parameters is to fill input fields on screens

     

    User Memory

    User Memory is the memory that is allocated for each user who is logged in.  A user session is assigned its own memory area of the user memory, in which SPA/GPA parameters can be stored.SPA/GPA parameters are set using SET PARAMETER and read using GET PARAMETER.

     

    ABAP Memory

    • Each SAP session contains an area called ABAP memory. ABAP memory is available to all internal sessions.
    • ABAP programs uses the EXPORT and IMPORT statements to access it.
    • All ABAP programs can also access the SAP memory.

     

     

     

  • Official ABAP Programming Guidelines

    Preface – This post is part of the ABAP Beginner series.

    Official ABAP Programming Guidelines [As per SAP Documentation]:

    ABAP Programming Guidelines as per the official SAP documentation consists of the following major points:

    Follow the KISS principle:

    KISS is an acronym and can have any of the following meanings (the list is not exhaustive):

    • Keep it simple, stupid.
    • Keep it small and simple.
    • Keep it sweet and simple.
    • Keep it simple and straightforward.
    • Keep it short and simple.
    • Keep it simple and smart.
    • Keep it strictly simple.
    [Add on: I will say follow KISS ASS principle, i.e. Keep It Short, Smart and Strictly Simple]

    Have less nesting depth:

    It means try not to write loops under loops or Select Query / SQL commands under loop statements. It affects the execution badly.

    Follow the SoC principle:

    • Follow the separation of concerns principle. As a result, the software:
    •  is more stable
    • easier to understand
    • can be reused more easily
    • easier to transport
    • has better maintenance
    • can be tested more easily

    It can be as simple as keeping data declarations and the implementation of the functions separate to keeping different segments of codes under different methods of a class for better understanding.

    Comply with or check compliance with existing product standards:

    Adhere to the product standards that exist in your organization, and ensure the correctness and quality of your programs by testing them during development and after completion with all the test tools at your disposal.

    As part of this basic rule, we recommend that you use all available tools that help to ensure the correctness and quality of ABAP programs and follow the ABAP programming guidelines.

    ABAP Programming Guidelines

    • Include the extended program check(transaction code is SLIN) in your programming routine and correct all messages (It will display the errors in the form of messages).
    • Always use the Code Inspectortool (transaction SCI) using the standard check variant, and correct all messages.
    • You must check the usability & accessibility of your interface (GUI) elements by using all available appropriate tools (integrated into the workbench tools and ABAP Test Cockpit).
    • You must cover all the functions of your procedural units by using unit tests with ABAP Unit (integrated into ABAP Workbench, Code Inspector, and ABAP Test Cockpit).
    • All the functions of your application must be covered by using scenario tests (e.g. for calculator, the scenario will be two inputs and pressing Add ‘+’ button) with eCATT (transaction SECATT).
    • For better performance of your code, check the memory consumption of your programs, and for that, you can use ABAP Memory Inspector (transaction S_MEMORY_INSPECTOR), and by using the memory analysis function integrated into the ABAP Debugger.
    • Check runtime behaviour and performance by using the ABAP runtime analysis tool (transaction SAT).
    • Check the test coverage by using a coverage analyser (transaction SCOV and integrated into ABAP Unit Browser of ABAP Workbench).
    • You must follow the ABAP – Security Notes(for more, visit: https://help.sap.com/doc/abapdocu_751_index_htm/7.51/en-US/abenabap_security.htm) to protect your programs and data from attacks from outside.
    • A programmer must make his program readable. For that, you need to Document your programs (codes) and services using all available means: Starting with comments, this ranges from simple data element documentation for context-sensitive input help, to class and method documentation for documenting APIs, to explanations of concepts and tutorials in other repositories such as SAP Knowledge Warehouse, or on the Internet, for example in the SAP Developer Community (SCN, http://scn.sap.com).

    What I actually wanted to tell you is that whenever you start coding, use the tools underlined above. You might not need them, and you might be a perfect coder, but nobody in the industry will tell you to use ABAP programming guidelines, and your code will fail in the testing phase. So, make it a habit to follow the ABAP programming guidelines.

    More ABAP Programming Guidelines

    • Use ABAP objects: Use ABAP objects wherever possible for new and further developments. Here are the reasons why:
      • Data encapsulation: Simple Reports, FM doesn’t give a clear differentiation between externally and internally usable data. But ABAP object does use PUBLIC, PRIVATE and PROTECTED sections.
      • Explicit instantiation: Multiple instantiations and automatic garbage collection (it means auto-deletion of unused memory) have exclusive advantages.
      • Inheritance: Reuse of classes via inheritance reduces code.
      • Interfaces: This means that developers do not need to concern themselves with the implementation details of the class behind the interface.
      • Events: Events define a program flow strictly, and event trigger does not need to know about any other handlers.
      • Cleansed Syntax: Classical ABAP reports have evolved over time and contain several obsolete and overlapping concepts.These obsolete concepts were kept aside during the construction of ABAP object concepts
      • Access to new technology: ABAP objects are often only the way to deal with new technology. Web Dynpro ABAP and ICF (Internet Connection Framework) provides only class-based interfaces. UI5 supports classes and FMs only.

     

    • Original Language: Once specified, there is currently no technical support for the replacement of an original language by another language across an entire project. We specify it every time we create a new object in ABAP repository.
    • Program Type: Select the appropriate program type according to your requirement. Following are the possible program types in ABAP(We will read about them in detail later):
      • Executable program: ABAP Editor creates executable programs. These are simple programs and supports selection screens.
      • Class pool: The Class Buildertool in ABAP Workbench (SE80) can define these. They are used as a repository for global classes and interfaces.
      • Interface pool: These are the ABAP programs that contains the definition of exactly one global interface and it is loaded using the interface.
      • Function group (function pool): All function modules we create belong to a function group. A function group is a collection of function modules sharing global data with each other.
      • Module pool: The ABAP editor (SE38) helps create a module pool. They are a collection of screens and are called via transaction codes.
      • Subroutine pool: ABAP editor (SE38) creates these. They are a collection of subroutines.
      • Type group (Type Pool): ABAP Dictionary (SE11) creates these. They are a group of TYPES and CONSTANTS.
  • What are SAP Session

    Preface – This post is part of the ABAP Beginner series.

    What are SAP Session?

    “SAP Session” are a way to control the number of logins or number of active users on a single landscape. To make it easier, let us assume that we have three landscapes, i.e. ISD, IST, ISP. These three landscapes will be used for different purposes, i.e. for development, testing and production. There are three types of sessions in SAP:

    • User Session
    • ABAP Session
    • Internal Session

    Now, SAP has given you three different environments. These are nothing but user sessions. As soon, you have opened SAP GUI, you have logged into a user session. And now you have logged in to ISD, i.e. for development.  In this scenario, you have also logged in to the ABAP session.  Now, you have opened the SE38 transaction to make a report, so now you have logged in to the internal session.

    If you have understood the sessions, then we can proceed with the definitions.

    User Session

    Logging on to an application server opens a user session. Logons take place using SAP GUI, the RFC interface, or ICF. A user session is assigned its own memory area of the user memory. From a user session, it is possible to open further user sessions on the same application server or another server in a program-driven way.

    ABAP Session

    An ABAP session is opened for each user session. Each ABAP session is assigned its own memory area of ABAP memory, in which data clusters can be stored.

    Further ABAP sessions for a user session can be opened as followed:

    • Enter a transaction code after “/O” in the command field in the toolbar.
    • Call the function module TH_CREATE_MODE.
    • Call a Dynpro when processing an asynchronous RFC.

    A maximum of 16 ABAP sessions are possible per user session, and the default is 6.

    Internal Session

    Each call of an ABAP program creates a new internal SAP session in which the called program is loaded. This is where the modifiable objects of the program are held. In one ABAP session, the maximum number of internal sessions can be nine.

    Close a hung SAP Session

    Let’s say you want to end session three. You can go to either session one or two and enter this command in the transaction box: /i3.
    This will immediately close the hung up session (session four in this case).

    Or you can do this the long way by going to SM04 and double-click your user ID, select a session and hit ‘end session’.

     

  • SAP Tcode or Transaction CODE?

    Preface – This post is part of the ABAP Beginner series.

    SAP Tcode or Transaction CODE

    What is SAP Transaction Code?

    SAP provides an easy access menu to navigate through its various screens like ABAP reports, ABAP classes, ABAP dictionary. But instead of navigating via these menus which will take a long time to reach, they have also provided shortcut codes/ key codes also known as transaction code.

    Following are the basic SAP transaction codes that every programmer must remember:

    SAP Tcode DESCRIPTION Uses
    SE11 ABAP DICTIONARY To create SQL Objects
    SE24 CLASS BUILDER To create Global Classes
    SE37 FUNCTION BUILDER To create A Function Module
    SE38 ABAP EDITOR To create a Program
    SE80 ABAP WORKBENCH To manage all ABAP Objects

     

     

  • SAP Program: How to code in SAP ABAP Language

    Preface – This post is part of the ABAP Beginner series.

    SAP Program: Hello World in ABAP

    I believe a programmer is born with the Hello World program. So, we will start with this SAP program only.

    Follow the following steps to make your first program (Happy Coding)

    1. Open Transaction SE38
    2. Write your program name starting with Z or Y (Reason is in our ABAP syntax post)

    Example: ZBARRY_ALLEN

    1. Click on Create
    2. Write Title

    Example: This is My First Program

    1. In Attributes, select Executable Program in Type and click Save
    2. Press Local Object to save it in a temporary folder (Reason will be discussed in upcoming posts)
    3. Now, this is the screen in the SAP program where you can write your codes(Excited?)
    4. Just write the code below:
     WRITE “HELLO WORLD”.
    1. Now, click Save, Check and Activate one by one, as shown below.
    2. Now run the SAP program by clicking on Run or pressing F8.

    That’s it. You have successfully written your first ABAP program.

  • ABAP Syntax

    Preface – This post is part of the ABAP Beginner series.

    Mandatory ABAP Syntax for Every Coder

    The following ABAP syntax is mandatory for every coder:

    1. ABAP is not a case sensitive language (It is case sensitive in the case of field name being passed in the function module).
    2. Every program must start with a keyword.
    3. Each program must end with a period (.)
    4. Every chained statement must have a single keyword followed by a colon (:) and the individual statement followed by a comma (,).
      Example:
      DATA :LV_PLAYER(10) TYPE c,

    LV_SCORE (100) TYPE n,

    LV_WICKET (10) TYPE n.

    1. To make the entire line comment use an asterisk (*) mark at the beginning of the statement.
    2. To make a comment after a statement in the same line, use a double inverted comma (“) before the comment.
    3. Use PRETTY PRINTER to provide indentation to your codes and make them readable for testers
    4. Nomenclature: Use the following prefixes before given types of fields
    Prefix Field Name
    LWA Local Work Area
    GWA/WA Global Work Area
    LTY Local Structure
    LT Local Internal Table
    GT Global Internal Table
    IT/ITAB Internal Table
    V Variable
    C Contant
    ZRP Custom Report Program
    ZCL Custom Class
    LV Local Variable

     

    [Note: Use underscores (_) after using prefixes]
    1. ABAP codes are whitespace sensitive.
      Example: a = b+c(d) and a = b + c ( d ) have different meanings. The first one c(d), will give ‘c’ of length ‘d’. The second one will call function ‘c’ with parameter ‘d’.
    2. In the ABAP custom field, i.e. custom reports, custom data element, custom domain always starts with ’Z’. It means everything we are going to create will be custom and not standard (SAP creates standard ones). So, they will start with ‘Z’ or ‘Y’.

    Example: ZBarry_Allen might be my report name

     

    1. ABAP provides both statements based syntax as well as the expression-based syntax
      Example:

      1. ADD Tax to Total.
      2. Total = Total + Tax.

    Both will give the same output.

  • ABAP Data Types and Constants

    Preface – This post is part of the ABAP Beginner series.

    Data Types

    ABAP Data types are the one which tell us about the technical characteristics of a Variable.

    By technical characteristics we mean the type of variable (Integer or Character) and its length.

    In SAP we can either use Global Variables created with the help of SE11 or can create local variables using these Data Types.

    Following are the Data Types (or Elementary Data Types) provided by SAP:

    DATA TYPE Description Default Length Default Value
    C Character 1 ‘ ’
    N Numeric 1 0
    I Integer 4 0
    F Float 8 0
    P Packed 8 0
    D Date 8 00000000
    T Time 6 000000
    X Hexa Decimal 1 X’0’
    STRING Variable Length String
    XSTRING Variable Length Raw Byte Array

     

    Constants

    Just like other languages, ABAP also provides benefits of storing some value under constants whose value cannot be altered later in the program. For example you want to store value of PI (π) i.e. 3.14 and which always have this value only, then constants will be a great help to you. The keyword is CONSTANTS.

    Example:

    CONSTANTS: pi TYPE p DECIMALS 3 VALUE ‘3.142’,

    No TYPE c VALUE ‘’.

     

    User Defined Data Types:

    To use the above data types user has to write the given codes with keyword TYPES. To define the lengths of variable type the length next to the variable name in bracket.

    Example:

    TYPES:ID (10) TYPE n,

    NAME (20) TYPE c.

     

    Structured Data Types:

    Just like other programing language where you create structures having several data variable, ABAP also provides same feature. Just like the example mentioned above we create Structured Data Types using Keyword BEGIN OF<Structure Name> and END OF<Structure Name>.

    Example:

    TYPES:  BEGIN OF EMPLOYEE_DETAILS,

    ID (10) TYPE n,

    NAME (20) TYPE c,

    END OF EMPLOYEE_DETAILS.

     

     

  • SAP System Variables

    Preface – This post is part of the ABAP Beginner series.

    SAP System Variables

    ABAP has given some predefined types of variables used to query system statuses. These variables are called SAP system variables. We use these variables to check whether a given command works properly. These are filled during runtime. They can store numeric, char, integer, date or time values. There is a field SY-SUBRC which is always zero ‘0’ in case of the correct program run.

    There is a table/structure in ABAP Dictionary (SE11) where you can find all the lists of these variables with descriptions.  They can be accessed using the keyword “SYST-” or “SY-”.

    Following are some of the SAP System Variables:

    NAME TYPE LENGTH DESCRIPTION
    SY-SUBRC i Returns value 0 in case of no error of above statement
    SY-DATUM d System Date
    SY-UZEIT t System Time
    SY-INDEX i Loop Index: Contains previous loop passes with the current one
    SY-TABIX i Row number in the table index of Internal Table
    SY-UCOMM c 70 Function Code that has raised the event PAI

    Programming Guidelines:

    1. It is recommended not to define a local variable starting with “SYST-” or “SY-” ( although not forbidden).
    2. Do not use obsolete system fields. Here is a list of obsolete ABAP system fields.
    3. Use System Fields at the right position. It means using an SY-SUBRC check after a mathematical expression will never change its value.

    Implementation of System Variables:

    WRITE: / “ABAP SYSTEM VARIABLES”.
    WRITE: / ‘DATE:’ SY-DATUM.
    WRITE: / ‘TIME:’ SY-UZEIT.

    The output for the above code is:

    ABAP SYSTEM VARIALBES
    27-06-2018
    11:34:00
    
    

    To view the complete list of ABAP System Variables, visit here.