Category: ABAP

ABAP

  • Final Class in SAP ABAP

    Preface – This post is part of the Object Oriented ABAP series.

    Introduction

    A class is said to be a ‘Final Class’ when it is no longer available for inheritance. Therefore, we won’t be able to inherit the properties or behavior of a final class. A final class does not have a subclass.

    However, if you don’t want to make a whole class final, and if you want to restrict some of the methods from getting redefined, then there is an option available to declare some of the methods of a class as ‘final’.

    Definition:

    If a class cannot be inherited further, then it is a Final Class. It is up to the developer if he wants to make a class ‘final’ or some of the methods ‘final’.

    Example: Let’s take an example of this ellipse. Say, ‘ELLIPSE’ is your class. A circle inside this class is your subclass.

    This is the first case, where the class ‘ELLIPSE’ is not declared as ‘final’.  Therefore, a subclass ‘CIRCLE’ inherits all the components of the superclass, where the visibility section is public & protected.

    Note: If there is an interface implemented in the superclass, it will get reflected in the subclass as well.

    Final Class in SAP ABAP
    Final Class in SAP ABAP

    Now, if class ‘ELLIPSE’ would be defined as ‘Final’, then this inheritance would not be possible.

    Program:

    Below is the sample program:

    1. CLASS_1 declared as ‘FINAL’ and hence giving error while inheriting CLASS_2 from CLASS_1.
      ABAP Final Class Example 1
      ABAP Final Class Example 1
    2. In second example, one of the methods of a class is defined as ‘Final’. The subclass is not having an implementation of final method.
      ABAP Final Class Example 2
      ABAP Final Class Example 2

    Advantages:

    Inheritance is one of the powerful pillars of OOABAP but not every time, this may lead to weakens encapsulation, can create a bug hazard. To prevent this, sometimes a class is declared as ‘Final’.

    Disclaimer:  All the images here are taken from Google, and the Admin has no possession over it.

  • Friend Class in SAP ABAP

    Preface – This post is part of the Object Oriented ABAP series.

    Introduction

    In the previous article, we have discussed the classes, its components and the visibility of components. As per the visibility section, only public methods or attributes of a class are allowed to be used by some other classes. Now, what if we want to access non-public (protected or private) components (methods or attributes) of a class, to solve this problem, OOABAP has a concept of ‘Friend Class’.

    ‘Friend’ in a real life is a person, with whom you can share your secrets. Just like that, in OOABAP, if a class is declared to be a friend of some other class, then it can access the non- public components of that class.

    Definition:

    The class to which friendship is granted can access all the components (irrespective of components visibility) of the friendship granting class.

    This Friendship is unilateral though, that means the friendship granted class cannot access the private and protected components of the class, to which friendship is granted.

    Example:

    Suppose, there is one global class ‘CL_DEMO_TEST_FRND’ class, this class is having 2 private methods. Now, I want to create one test class (ABAP Unit Test Class) for this global class say, LTC_DEMO_TEST_FRND, the task of this class is to check the code coverage of a global class, that means this test class should have access of all the components of a global class. We will make this local test class ‘LTC_DEMO_TEST_FRND’ a local friend of global class ‘CL_DEMO_TEST_FRND’ so that its private methods can be tested.

    Syntax: 

    CLASS LTC_DEMO_TEST_FRND DEFINITION DEFERRED.
    CLASS
    CL_DEMO_TEST_FRND DEFINITION LOCAL FRIENDS LTC_DEMO_TEST_FRND.

    NOTE: In the above example, keyword ‘Definition Deferred’ is used, it makes the class ‘LTC_DEMO_TEST_FRND’ known without having a definition.

    Program:

    Scenario: There are 2 classes, class ‘DEPARTMENT’  having private attribute ‘NAME’. The other class ‘EMPLOYEE’ having one method ‘SET_NAME’. Class ‘EMPLOYEE’ needs the private attribute ‘NAME’ to set the employee name.

    In the first program, class ‘EMPLOYEE’ is trying to access the private attributes of class ‘DEPARTMENT’, without declaring ‘EMPLOYEE’ as a friend of ‘DEPARTMENT’, and hence it is giving an error.

    ABAP Friend Class Example 1
    ABAP Friend Class Example 1

    After declaring ‘EMPLOYYE’ friend of ‘DEPARTMENT’. Now, ‘EMPLOYEE’ class can access the private attributes of class ‘DEPARTMENT’.

    ABAP Friend Class Example 2
    ABAP Friend Class Example 2

    Advantages of Friend Class in SAP ABAP:

    1. Allows sharing non-public information of a class by a non-member function.
    2. The sub-classes and interfaces of a class (to which friendship is granted), can also access the non-public components of a class (friendship granting class).
    3. With the help of friend class, we can use the additional functionality, kept outside the class.
    4. We can use the data which is not being used by that class.

    Disclaimer:  All the images here are taken from Google, and the Admin has no possession over it.

  • SAP ABAP Classes

    Preface – This post is part of the Object Oriented ABAP series.

    Introduction

    In the last article, we have learnt about the concepts and advantages of OOABAP through which we came to know that it mainly revolves around the classes & its object. The last article gives us the overview only, now it’s time to take a deep dive into the major concepts of OOABAP. Let’s start with the SAP ABAP Classes.

    Classes in OOABAP defines the object it is having. A class can have infinite number of objects, with a unique combination of state and behavior. Classes contain properties, methods and other things that allow developers to define what the class represents. But a class alone will be of no use, unless it is having some objects, that is why an object is known as the instance of a class. We instantiate an object that points to classes.

    Example:  

    SAP ABAP Classes
    SAP ABAP Classes

     

    The above figure depicts Car as a Class and car models as the Objects of a class ‘CAR’.

    Flavors of SAP ABAP Classes:

    ABAP Classes can be declared as either globally or locally. Following are the flavors in which ABAP classes are available:

    1. Local Class:

      Local class can only be defined within a program, and accessibility of this class is restricted to that program (within which it is defined) only. Defining a local class consist of two things:

    a. Class Declaration: This is being done inside a coding block of CLASS DEFINITION. It contains the declaration of all the components (attributes, methods, events) of the class.

    Syntax:    CLASS ABC DEFINITION.

    Coding Block

    ENDCLASS.

    Note: 

    1. ABC is a name of class.

    2. This Class definition should always be at the beginning of the program because the declaration inside this block belongs to global program data.

    b. Class Implementation: This part contains the implementation of all the methods declared in CLASS DEFINITION. The CLASS IMPLEMENTATION block of local class is a processing block (meaning consist code of functionality).

    Syntax:    CLASS ABC IMPLEMENTATION.

    Coding Block

    ENDCLASS.

    Note: If you are declaring methods in CLASS DEFINITION, then the CLASS IMPLEMENTATION of that method is must.

    1. Global Class:

      To create a global class, T-code is SE24 (Class Builder) in ABAP workbench. The name itself is suggesting the accessibility of global class, all ABAP programs can use these global classes by instantiating the object of that class.

    Structure of Class:

    A class structure comprises of 3 things:

    • Class Components.
    • Visibility section of Components.
    • Implemented methods of class.

     

    1. Components of Class:

      It defines the attribute of the objects in a class. While defining a class, components are declared in the declaration part of the class with one of the three visibility section (Public, Private, Protected). The components of classes are available in two types:

    • Instance Component – These components exist separately for each object in a class.
    • Static Component – Exist only once for whole class.

     

    Following are the components of the class (with each component type):

    a. Attribute – variables or constant declared within the class and can be access by all the methods of that class.

    • Instance Attribute – Attributes that would be instance specific for an object. It can be declared in local class by using DATA
    • Static Attribute – Defines the state of class. It can be declared in local class by using CLASS-DATA

    b. Method – Determines the behavior of an object, it provide some functionality.

    • Instance Method –  It can be declared in local class by using METHODS Accessible by all the attributes of a class.
    • Static Attribute – It can be declared in local class by using CLASS-METHODS They can access static attributes only.

    c. Events – A mechanism through which one method can raise method of another class.

    • Instance Event –  It can be declared in local class by using EVENTS This can only be triggered in an instance method.
    • Static Event – It can be declared in local class by using CLASS- EVENTS All methods (static and instance) can trigger static events.

    Note:  Each component name should be unique within a class.

     

    1. Visibility Section:

      Components of a class have their own visibility section which defines where they can be accessible. There are total three types of visibility:
      a. Private – Components will be visible to that class only, where they are defined.
      b. Public – Components with this visibility will be available to all.
      c. Protected – Components will be visible to that class, where they are defined and to the inherited class.

     

     

    Types of ABAP Class:

    When you create a class through T-Code SE-24(Class Builder), it will ask you which type of class, you want to create. So, there are total five types of classes:

    1. Usual ABAP Class:

    As the name itself suggests, usual or a normal class which we create through SE24.

     

    1. Exception Class:

    A class to handle the exceptions occurred during runtime or program execution.

    Types of Exception classes: Total 3 exceptions classes exist, that are inherited from one super class ‘CX_ROOT’.

    SAP ABAP Exception Class
    SAP ABAP Exception Class

     

    While creating an exception class, a check box ‘WITH MESSAGE CLASS’ is there. If u select this, a message class then will be generated. This message class, we can separately generate from t-code SE91.

    1. Message Class:

      Suppose, one message is being used frequently in one program. One thing we can do is hardcoding, but that would be against coding standards.

    Instead of doing that, we can collect it somewhere (say in a container), and from there we can use it anywhere we want, even in different programs as well. So, a message class is like container only, which holds a bunch of different messages.

    1. Persistent Class:

      Before going for persistent class, let’s first discuss the meaning of ‘persistent’.  A data is said to be persistent if it can be preserved beyond the runtime of program.

    For a session, ABAP program stays in local ABAP memory till the runtime of that program. To store it permanently, we use persistence service (implemented by persistence class).

    1. Test Class (ABAP Unit):

    This class is mainly created for unit testing. Now unit testing of what? When we create any class, we usually check if the required functionality is achieved or not. Before going for any testing, we usually test our code by creating a unit test class.

    This unit test class consists some test methods corresponding to the methods of original class (for which testing is being done), and this executes the comparison between the expected and actual values.

  • Object-Oriented ABAP (OOABAP )

    Preface – This post is part of the Object Oriented ABAP series.

    Introduction

    Object Oriented ABAP or OOABAP is the object-oriented extension of ABAP, which mainly focuses on the object rather than the code flow. An object-oriented approach enables programmers to think like they are working with real-life entities.

    Just take a real-life scenario, a person having knowledge can do various works, same in oops, object have fields to store data and with which it can do various works.

    The Object-Oriented concept of ABAP mainly revolves around the classes and object, which are the basic elements of OOABAP.

    Before moving further, let’s learn about the very basic and the most important elements of OABAP:

    Objects

    It’s a working entity of a class. Each object is unique at its own, meaning with unique identity and  attributes. Attributes defines the state of an object whereas behavior of an object refers to the changes that occur in its attributes over a period of time. An object can be associated with the class by declaring it with the same data type as of the class within which an object has been created.

    An object has three characteristics –

    1. Has a state.
    2. Has unique identity.
    3. May or may not display behavior.

    Class

    A class is like a blueprint of an object or a template that binds similar kind of data. It defines the characteristics of an object. Following are the components of class:

    1. Attribute – variables or constant declared within the class and can be access by all the methods of that class.
    2. Method – Determines the behavior of an object, it provide some functionality. A method can access all the attributes of their class.
    3. Events – A mechanism through which one method can raise method of another class.
    4. Interface – Independent structure which are used in a class to extend functionality of a class.
    5. Alias – A concept of providing alternate method name for interface method in an implemented class.

     

    Component types of class

    1. Instance Component (instance attributes, instance methods & instance events) – These components can only be addressed by the instance(object) of that class only.
    2. Static Component (static attributes, static method & static events) – These components can be used independently of a class instance.

     

    The three pillars of OOABAP (Advance elements of OOABAP):

    1. Encapsulation: It is a concept of preventing your data from being arbitrarily accessed by some other outside interference or protect your data from being misused. It Allows us to bind similar kind of data in one unit. It restricts the visibility of components of class.

    There are 3 levels of visibility:

    1. Private – Components will be visible to that class only, where they are defined.
    2. Protected – Components will be visible to that class, where they are defined and to the inherited class.
    3. Public – Components with this visibility will be available to all.

     

    1. Polymorphism: A concept which allows to overwrite some functionality. That is, a same functionality of a class (i.e., method) can be reused in other classes with some enhancement in it.

    ‘Interface’ is a concept in object-oriented ABAP to achieve polymorphism. An interface is a place where a functionality(method) can be defined with some parameters, which later can have different implementations in different classes.

    Advantage of polymorphism is that we don’t need to define a method for different scenarios of implementation. Same definition can be used for different implementations.

     

    1. Inheritance: A name itself explaining the concept, i.e., it allows a sub class or a child class to inherit the properties from a parent class. In OOABAP, only single and multilevel inheritance is allowed.

    Single inheritance means only one parent class and its child class.

    Multilevel Inheritance means a parent class can have a child class, it grandchild class and so on with the hierarchy.

    The main advantage of inheritance is reusability of code functionality & fast implementation time.

     

     

    Advantages of OOPS Concept in ABAP

    1. In OOABAP, programs are divided into objects leading to better and powerful data management.
    2. Provide properties like data hiding(encapsulation) & code reusability(inheritance) with more data security.
    3. Better performance with less consumption of time.
    4. Helps in future orientation.
    5. Simple and it much easier to maintain as compare to procedural ABAP programming.
    6. Relatively flexible & adaptable to changing business needs.
    7. Object Oriented programming languages include features such as “Class”, “Instance”, “Inheritance” and “Polymorphism” that increase the power and flexibility of an object.
  • ABAP Field Symbols

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

    ABAP Field Symbols

    In ABAP reports, very often you will be having a situation where you need to store single record of a table or take input from user and append that record to table, both at run-time. In this situation you will need a structure which has all the fields of that particular table with the same namespace. Every time the content of the table changes, you will have to update your structure. So, instead of using structure you can use something called workarea and field symbols that are the replica of single row of that table with same fields.

    Introduction

    In programming language C/C++, there is a concept of pointers. Let us understand pointers first, before understanding Field Symbols.

    Pointers in C/C++:

    A pointer is also a variable that hold an address which is the location of another variable in memory. Since a pointer is also a variable, therefore its value is also stored in the memory in another location. In given image, we have assigned the address of a variable “A” to a pointer variable “P”. The link between the variables “P” and the variable “A” can be visualized as shown in the figure.

    ABAP Field Symbols
    ABAP Field Symbols – Pointer Illustration

    The address of A is 07xff0a767cc4. We can see that the value of variable “P” is the address of the variable “A”. Thus, we may access the value of “A” by using the value of “P” [how to access is explained later]. Therefore, we say that the variable “P” points to the variable “A”, hence “P” got the name ‘pointer’.

    Referencing and Dereferencing Pointer:

    Referencing Pointer: As the name suggest, Referencing is used for reference. A referencing pointer is the one that uses & operator (ampersand character) to set a pointer variable i.e. Address assignment of a variable to a pointer.

    int a;
    int* p1;
    a = 11;
    p1 = &a; //p1 references c1

    Dereferencing Pointer: As the name suggests, Dereferencing is used to get values out of a reference. A dereferencing pointer is the one that uses * operator (asterisk character) to get the value stored at a pointer.

    int b;
    b = *p1; //value of b will be then 11. Since we have assigned address of a to p1 in above example

    Why to use a pointer:

    A pointer is used to reduce the length and complexity of a program and hence increase the execution speed.

    What is a Field Symbol?

    Just like a pointer discussed above, SAP also introduced something called Field-Symbols. It refers either a table, a field or even anything. When I say anything, I mean that the structure of field symbol will be determined dynamically. In simple words, a field symbol is just a pointer that is used to point a specific line of internal table.

    Definition

    A field symbol in ABAP is a dereferencing pointer that is mainly used to point specific lines of Internal table. It can be used to point either a table, a field or a dynamic structure. It is used as an alternative to work area to reduce the memory consumption and increase the performance of a program.

    Syntax

    FIELD-SYMBOLS <fs> { typing | obsolete_typing }.

    The ABAP keyword FIELD-SYMBOLS is used to declare a field symbol <fs>. Fs is the part of naming convention and the mandatory angle brackets distinguish them from the other data objects.

    Syntax for declaring a field symbol using different data types:

    FIELD-SYMBOLS: <fs_example1> TYPE field name.
    FIELD-SYMBOLS: < fs_example2> TYPE table.
    FIELD-SYMBOLS: < fs_example3> TYPE REF TO DATA. "here DATA is a reference type

    The keywords which are used to assign a value to the field symbol are ASSIGNING and ASSIGN.

    ASSIGN <itab> [ KEY primary_key (‘…’) = ‘…’] TO <fs_example>.

    It is done because, a pointer must have an address to refer, here we assign an address to the field symbol.

    It is very important to check if a field symbol is assigned or not else, we will get a run-time error.

    IF <FS_EXAMPLE> IS ASSIGNED.
    **Your code
    ENDIF.

    Dynamic Field Symbol or Field-Symbols Inline-Declaration

    With the introduction of inline declaration of field symbols, we don’t have to check whether it is assigned or not. Also, we don’t have to declare a field symbol directly.

    Syntax:

    …. FIELD-SYMBOL(<fs>) ….

    Example

    LOOP AT <itab> ASSIGNING FIELD-SYMBOL(<fs_line>). 
    *** Your Code 
    ENDLOOP.

     

  • Modularization Techniques in ABAP

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

    Modularization Techniques in ABAP is a way to write codes in different processing blocks like subroutine, Function Modules and ABAP objects. In this article, we will learn what is a Modularization, its types and advantages.

    Introduction

    The literal meaning of Modularization is “design or production of something in a separate section”. All the ABAP programs are modular in structure. This can be easily understood from ABAP Events. All these events are different blocks and are part of general execution of ABAP programs.

    Apart from this internal modularization, ABAP also provides external Modularization techniques. In this external modularization technique, ABAP codes are written in different processing blocks like subroutine, Function Modules and ABAP objects, and are called from ABAP report.

    These processing blocks that we call from ABAP programs are called procedures.

    Modularization Techniques in ABAP
    Modularization Techniques in ABAP – Image Illustration

    Definition:

    Modularization Techniques in ABAP is a way to incorporate reusability in ABAP programs. It improves the readability and maintainability of ABAP programs, prevents redundancies, incorporates reusability of functions, and the encapsulation of data.

    Type of Modularization in ABAP

    • Following are the processing blocks that can be called from ABAP programs:
      1.     Subroutines
      2.    Function modules
      3.    Methods (see ABAP Objects)

     

    • Following are the modularization technique ABAP allows you to modularize source code by placing ABAP statements:
      1.     Local Macros
      2.    Global Include Programs

    Advantages of Modularization in ABAP

    Modularization Technique in ABAP brings following advantages to ABAP programs:

    • Improves the readability and maintainability of ABAP programs
    • Prevents redundancies
    • Incorporates reusability of functions
    • Encapsulation of data
  • Parallel Cursor in ABAP

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

    Parallel Cursor in ABAP is a way to modify the conventional nested loop in such a way that the overall performance of the ABAP program gets improved.

    Introduction

    Suppose we have two tables, one table has Employee basic details and another table has Employee monthly salary details. And the requirement is to print Employee Basic details with all his salaries. What a programmer will do, he will write two Loops. The first loop will get the employee basic details one by one and the other loop will get employee salary details. You can check this thing in below ABAP code:

    LOOP AT zemployee_basic INTO wa_employee_basic.
    LOOP AT zemployee_salary INTO wa_employee_salary where employee_id = wa_employee_basic-employee_id.
    Write: wa_employee_basic-employee_id, wa_employee_salary-month, wa_employee_salary-amount.
    ENDLOOP.
    ENDLOOP.

    The above code will work fine and will print Employee ID, Month of the Salary and Amount of the salary of each and every employee.

    Parallel Cursor: Loop in Loop Image Illustration
    Parallel Cursor: Loop in Loop Image Illustration

    What if you want to find Salary Amount of one person for a particular month? You will again write the same loops to find the required details.

    Also, for every new Employee ID, the second Loop still goes through the whole data.

    What we conclude with this process is that, this process is neither good for printing data nor good for search operation as it wastes ample amount of time.

    Parallel Cursor Concept

    To curb this problem, a concept called Parallel Cursor was introduced by SAP ABAP.

    According to Parallel cursor concept, we need to follow following steps:

    Step 01: Sort both the tables

    Step 02: Write the first loop, like the previous way itself.

    Step 03: Read the second table with the key that was required to search the data. This would give the first value regarding the key and also its position in the table [SY-TABIX].

    Step 04: If the above step executed successfully, it means data is there in the second table associated with the key. Hence the SY-SUBRC will be set to zero. In this step we will check the value of SY-SUBRC. If it is zero, than we will proceed.

    Step 05: Assign the SY-TABIX value into a local variable. We do this step to preserve the SY-TABIX value as it changes with the loop.’

    Step 06: Now it’s the time to write the second loop. This time we don’t write the WHERE condition. This time we will write FROM condition with the local variable we initialized above.

    Step 07: In this step we check if the key of first table is equal to the key of second table or not. If not, then exit the loop. This stops the loop from reading non required data.

    Step 08: In this step we write our actual logic or print statements. And then close both the loops.

    Parallel Cursor Example

    Let us implement parallel cursor algorithm in the last example we discussed.

    SORT: zemployee_basic, zemployee_salary. [Step 01]

    LOOP AT zemployee_basic INTO wa_employee_basic. [Step 02]

    READ TABLE zemployee_salary INTO wa_temp WITH KEY employee_id = wa_employee_basic-employee_id BINARY SEARCH. [Step 03]

    IF SY-SUBRC = 0. [Step 04]

    lv_index = SY-TABIX. (you need to declare this variable earlier only) [Step 05]

    LOOP AT zemployee_salary INTO wa_employee_salary FROM lv_index . [Step 06]

    IF wa_employee_basic-employee_id <> wa_employee_salary-employee_id. [Step 07]

    EXIT.

    ENDIF.

    Write: wa_employee_basic-employee_id, wa_employee_salary-month, wa_employee_salary-amount. [Step 08]

    ENDLOOP.

    ENDLOOP.

    From above steps and example, it is clear that Parallel cursor is better than conventional nested loops. Actually nested loops take O(n1*n2) time to execute both the loops while parallel cursor takes O(n1+n2) time to execute the same.

  • SAP Message

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

    While using mobile phone and websites, you might have seen several messages popping up like security message in Mobile phones regarding photo access or antivirus message in PC regarding virus detection. All these messages can be a success, information or error message. In similar way we can showcase SAP message in ABAP programs too.

    Definition

    A Message in ABAP is used to display a text message either stored in table T100 or any text [can be in a message class] where type of the message can be a status, warning, error and etc. A message in ABAP can also be used to raise an exception.

    SAP Message Class

    Message class is a class which holds a list of messages with message id. It is created using transaction code SE91. This helps to maintain all messages related to a project/program all together.

    Syntax

    MESSAGE { msg | text

    | tn

    | tn(id)

    | { ID mid TYPE mtype NUMBER num }

    | {oref TYPE mtype}

    | {text TYPE mtype} }

    { { [DISPLAY LIKE dtype] [RAISING exception] }

    | [INTO text] }

    [WITH dobj1 … dobj4].

    Syntax Explanation

    The above syntax is explained below:

    Syntax Explanation Example
    msg | text
    or, {text TYPE mtype}
    Here msg is taken from class T100 while text is a string. MESSAGE ‘Success Message’ TYPE ‘I’.
    tn It needs a message class. Here t is a single character and n is three digit number; both are written together. These particular tn will represent a text that is stored in message class. Here message class name is written at the starting of report. REPORT ztest MESSAGE-ID zmessageclass.

    MESSAGE s001.

     

    –>  Here s001 is a message having its text stored in zmessageclass.

    tn(id) Like above t and n means same. Here id is the message class name and it is written just after the message REPORT ztest.

    MESSAGE s001 (zmessageclass).

    { ID mid TYPE mtype NUMBER num } Here mid means message class, mtype means type of message (explained later) and num is a message number (a number of length three) DATA: m_id   TYPE sy-msgid VALUE ‘ zmessageclass ‘,

    m_type TYPE sy-msgty VALUE ‘I’, m_num   TYPE sy-msgno VALUE ‘001’.

     

    MESSAGE ID m_id TYPE m_type NUMBER m_num.

    {oref TYPE mtype} Here oref is object reference variable which points to an object whose class implements the system interface IF_T100_MESSAGE. These are mainly used when we deal with local classes inside a report. Check out the example at given link.
    [DISPLAY LIKE dtype] When Display like is used, then instead of type of message the icon will be as per Display Like MESSAGE ‘Success Message’ TYPE ‘I’ DISPLAY LIKE ‘E’.

    *** Note: Above message is informative message but will be displayed as error message.

    [RAISING exception] It raises a non class based exception and only sends a message if the exception was not handled. It can be handled via sy-subrc. CLASS-METHODS msg1 EXCEPTIONS excp1.

    METHOD msg1.

    MESSAGE ‘Exception msg in a Method’ TYPE ‘I’ RAISING excp1.

    ENDMETHOD.

    [INTO text] This type of message does not impact on program flow and it does not matter what is the type of message. It just assigns the value of Message into a user defined field [here text]. MESSAGE i001 INTO DATA(newText).

     

    ***Note: Here newText is created dynamically and holds the value of i001 in it. This value can be used later in the same program.

    [WITH dobj1 … dobj4] This is used to add static text in place of ‘&’.

    Suppose, we are bring text from table T100 and the text is “I am a &&”.

    Then we can put our values in place of & & according to the situation.

    MESSAGE i001 WITH ‘Good’ ‘Player’.

    –>  The above will return “I am a good player”.

    ***Note: We can even give index to ‘&’ in the table. Suppose the text was “I am a 2& 1&”.

    Then the above Message will give output : “I am a player good”.

     

    Types of SAP Message

    Value Type Explanation
    A Termination It is displayed in a dialog box and the program is terminated.
    E Error It displays an error message in status bar and all the input fields are cleared.
    I Information It is displayed in a dialog box.
    S Status It is displayed in status bar of next screen.
    W Warning It displays a warning message in status bar and all the input fields are cleared.
    X Exit Exit messages cancels the running program and returns a dump.

     

    Information Message in SAP
    Information Message in SAP – Image Illustration
    Error Message in SAP
    Error Message in SAP – Image Illustration
    Status Message in SAP
    Status Message in SAP – Image Illustration
  • Open SQL Statements in SAP ABAP

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

    When we talk about Open SQL in ABAP, then it means only Data Manipulation Language (DML) part of SQL. It means we can only manipulate the table created in ABAP Dictionary using open SQL and not create a new table using SQL. The SQL statements of ABAP Reports directly communicate with a database as shown below:

    Open SQL in SAP
    Open SQL in SAP – Image Illustration

    Open SQL Keywords in ABAP

    SAP ABAP provides many SQL keywords that can be used as DML. These are as follow:

    Keyword Function
    SELECT It is used to fetch/ read data from database table.
    INSERT It is used to insert data into a database table.
    UPDATE Changes content of a row of database table based upon condition.
    MODIFY Same as above, Changes content of a row of database table based upon condition. But if the data is not present as per condition, then it creates/ inserts a new data in the database table.
    DELETE It deletes a row or all data based upon condition.
    OPEN CURSOR,

    FETCH,

    CLOSE CURSOR

    To read lines of database tables using cursor. In these statements we don’t fetch data from table into something but assign it to the cursor.

     

    Examples

    *** We will update it soon

  • Control Break statements in SAP ABAP

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

    Control Break statements in SAP ABAP are the are statements enclosed between  AT and ENDAT. These statements help us to control a loop according to our requirement. In this article we will discuss about the four control break statements provided by SAP ABAP.

    Introduction

    In ABAP, there are times where we need to perform some group level processing operations only upon certain set of data. For that purpose, we can take all data from a table to an internal table and push that data into a loop [LOOP … END LOOP]. In that loop we will need something called control break statements which are enclosed between statements AT and ENDAT.

    To utilize these control break statements in ABAP, we will also need some mathematical statements such as SUM, which can be used to total the numeric components of a table.

    ***Note: If inside a Loop you are using INTO wa, then it cannot be modified as it is overwritten with the old one when the AT-ENDAT control structure is entered. In this case use another work area to save your changes.

    Types of Control Break statements in SAP ABAP

    Statement Description
    AT FIRST …. ENDAT ·         This is triggered for the first row of internal table or the first iteration of loop (both are same).

    ·         As soon it is hit (AT FIRST), the value of work area are filled by ‘*’. And as soon as it hit –ENDAT, the work area is restored.

    AT NEW <field_name>   …. ENDAT ·         This is triggered for First row of a group with the same content of internal table based on a field (here <field_name>).
    AT END OF <field_name>   …. ENDAT ·         This is triggered for Last row of a group with the same content of internal table based on a field (here <field_name>).
    AT LAST …. ENDAT ·         This is triggered for the last row of internal table or the last iteration of loop (both are same).

    Image Illustration

    Control Break statements in SAP ABAP: At First
    Control Break statements in SAP ABAP: At First
    Control Break statements in SAP ABAP: At New
    At New
    Control Break statements in SAP ABAP: At End
    At End
    Control Break statements in SAP ABAP: At Last
    At Last

    Example

    *** We will update it soon