Category: OOABAP

  • SAP BOPF (Business Object Processing Framework)

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

    Introduction

    Every year, more than 1 million custom objects are created by various industries using SAP ABAP technology. All these custom objects starts as a fresh requirement and needs the same monotonous approach of development. SAP hence came up with a new framework in SAP ABAP which focuses upon the individual business logic, rather than expending efforts on the development of application infrastructure. This framework in known as Business Object Processing Framework or SAP BOPF.

    What is SAP BOPF

    SAP BOPF is an Object Oriented ABAP based framework that provides a set of generic services and functionalities that helps to standardize, modularize and speed up the development process. A BOPF is capable of managing the entire life cycle of business objects as well as covers all the aspects of the business application development.

    In simple words, BOPF replaces the work of SE24 class builder with all the custom CRUD operation. It acts as the transaction/application layer in SAP architecture. With the help of ABAP CDS (Core Data Services) it can be exposed directly over HTTPS and used via UI5 Applications.

    SAP BOPF Architecture

    A BOPF simplifies the entire business process by dividing it into small entities. The business object in BOPF is represented as a hierarchical tree of nodes. While each node includes a set of business logic attributes.

    SAP BOPF Basic Architecture

    Here, each of the business logic attributes signifies a specific business logic part. In the diagram we have shown all the elements that are part of a Node of a BOPF:

    SAP BOPF Architecture

    The elements shown above are:

    1. Action: It is an important entity and is used to implement a service of Business object. It is responsible for all the actions the node performs.
    2. Alternative Keys: All the instances of BOPF are controlled via the keys as key plays important role during CRUD operation. Mainly the keys are GUID or UUID, hence it gets important to have alternative value that are human readable.
    3. Association: It is used to associate a node with another one.
    4. Authorization Checks: It is used to manage Authorization at node level.
    5. Determination: It is used to perform automatic execution of codes based on the trigger conditions.
    6. Query: It is the part where business queries are maintained.
    7. Validation: It is the part where validation related to the node is maintained.

    Important BOPF Transaction Codes:

    • BOBF: It stands for Business Object Building framework. Remember, it is not BOPF unlike the name of the framework.
    • BOB: It stands for Business Object Builder
    • BOBT: It stands for BO Test
    • BOBX: It stands for Business Object Enhancement Workbench

    Advantages of SAP BOPF

    SAP BOPF comes with given advantage:

    • It standardize, modularize and speed up the development process.
    • With the help of BOPF, we get the entire application infrastructure and integration of various development components for free.
    • BOPF allows us to rapidly build the applications on a stable and customer-provided infrastructure.
  • Business Add-Ins in SAP (SAP BAdI)

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

    Introduction

    SAP as a solution for any enterprise workflow is one of the best product available in the market. It almost covers all the possible scenarios of any business. Still, based on the requirement of the customer, a developer has to enhance the existing code. For that the developer needs to add their own code within the SAP source code. Every year SAP rolls out its new version of source codes in the form of updates. It means whatever programs or reports you are using, it gets updated yearly. In this scenario, the developer may lose their changes. To fix this issue, SAP provides some enhancement techniques which can be used to enhance the code in such a way that it is preserved for every update. One such type of enhancement is Business Add-Ins. In this article we will explore more about the concept of SAP BAdI in detail.

    What is a SAP BADI

    SAP BADI is an object oriented enhancement option where the enhancement is made with the help of reusable objects and added within the source code with the help of predefined hooks.

    How to use SAP BADI

    A BAdI is created using Enhancement spot under transaction SE80. The name of a BADI should always start with BADI_ for standard BADIs and ZBADI_ for custom BADIs.

    A BAdI can be re-implemented using two elements i.e. GET BADI and CALL BADI.

    Types of BAdIs

    SAP provides given three ways of BAdIs Implementation:

    1. Single-Use BAdI: This type of BAdIs have no filters and are called once.
    2. Multiple-Use BAdI: This type of BAdIs allow multiple implementation
    3. Filter BAdI: A filter in BAdI helps a developer to select among multiple BAdIs with the help of filters.

    Advantages of SAP BAdIs

    Following are the scenarios where a BAdI implementation is useful:

    • A customer wants to enhance his product, but modification in source code is not desired. This is easily achieved with the help of BAdIs.
    • Different country and industry have different business cases for same product. This is easily achieved using multiple BAdI implementation.
    • A customer can do add their codes after delivery. The customer with the help of BAdIs can implement their own codes in the BAdIs objects later.
  • Design Patterns in SAP ABAP

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

    Introduction

    In software development world, every project requires a specific flow of code to achieve its target. After the growth of Information Technology, it was observed that many of the process across various domains and business remains same. For example, the way a user sign in and sign out. These common scenarios are now achieved using a design pattern. In this article we will try to explore Design Patterns in SAP ABAP.

    Design Patterns in SAP ABAP

    In SAP ABAP, a design pattern is a general, reusable solution to a generic and common issue. In reality it is not a finished solution to implement rather it is a template for how to solve a specific problem that can be utilized in many different scenarios.
    In SAP ABAP, design patterns are considered as the best practices, an ABAPer can use to solve common problems while designing a product.

    Following are the design patterns available in SAP ABAP:

    • MVC (model, view, controller) pattern
    • Singleton pattern
    • Factory pattern
    • Builder pattern
    • Observer pattern
    • Visitor pattern
    • Lazy initialization pattern
    • Template method
    • Strategy pattern
    • Decorator pattern
    • ABAP-specific examples
    • Anti-patterns

    We will explore these individually in different articles.

    Advantage

    A design patterns comes with a lot of advantage. Following are a few of them:

    • Almost all the design patterns are object oriented and hence can be reused in multiple projects
    • With a pre-defined flow, they provide more accuracy and transparency to the project architecture and design
    • Design patterns prove to be the only solution for some of the project requirements

    Limitations

    With so many advantages, there are some limitations and criticism too of design patterns. Following are a few of them:

    • Many design patterns are either obsolete or are a part of newly developed frameworks
    • Inappropriate use of SAP ABAP design patterns seems to increase the complexity of the program
    • Design patterns are sometimes considered as Syntax. Well, it is not and can be amended as per the project requirement
    • A design pattern comes with complexity and low reusability costing more time in executing a program. It is advisable to only use a design pattern if it really serves the needs
  • Garbage collector in SAP ABAP

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

    Introduction

    As a developer, we need not to explicitly configure memory allocation and deallocation in the system. This process triggers automatically, when the object is created memory is automatically allocated to it and when it is terminated the memory is destroyed by the background process called Garbage Collector to free up the space for new objects.

    The objects using dynamic memory allocation captures some memory and that memory is allocated until there are references to that object. When there is no reference to the object, it is assumed that the object is no longer need. Garbage Collector identifies the unused objects (no more references to the object are there) and scrap it out for other objects to reuse the space. It is called periodically by ABAP runtime environment.

    The programs that do not de-allocate memory at last collapse when there is no more memory left in the system to allocate. This scenario is termed as a memory leak in the program. As garbage collector is an automated process, the number of memory leak errors is minimised.

    Why proper Garbage Collection is important?

    Garbage collection plays an important role in system programming. The unseemly behaviour of the garbage collector can result in critical problems, like:

    • Increased memory requirement
    • Performance degradation

    Benefits

    Garbage collection eventually reduces or eliminates some bugs:

    • Dangling pointer bugs: When there are one or more references to the object is used in the program but the memory is freed.
    • Double free bugs: To free the memory which is already freed.
    • Memory leaks: When memory is not cleared and objects have no reference, this will lead to memory exhaustion.

    Drawback

    Garbage collectors can sometimes bring runtime overhead that can serve as the performance problem for a larger application. Hence, it is critical to implement the right garbage collection method to optimize the application performance.  

  • User-defined types in SAP Classes

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

    Introduction

    User-defined types in SAP Classes are used to define structures and table types in the class. Structure is a data type that defines structures data objects and table type represents the structure and functions of an internal table.

    A TYPE in classes has following fields that are required to be filled:

    • TYPE: Unique name for type.
    • VISIBILITY: This field defines the visibility of type. It has four options: Public, Protected, And Private and Package.
    • TYPING: This field specifies the Reference Type. It has three options: Type, Type Ref To and Like.
    • Associated Type: Any elementary ABAP type or object type can be defined (generic types, classes or interface).
    • DIRECT TYPE ENTRY: The arrow marked button after Associated Type is called Direct Type Entry. It is used to provide further details of the data type.
    • DESCRIPTION: Short description of the field.

    Syntax

    TYPES: Begin of <structure-name>,

    .

    .

    END of <structure-name>.

    TYPES : <tt-name> TYPE TABLE OF <structure-name>.

    How to create User-defined types in SAP Classes?

    Follow the below steps to declare types in global class:

    1. Provide the object name in SE24.
    2. In Class/Interface screen, provide the method name in Method tab.
    3. Go to Types tab to provide user-defined type.
      • Click on Direct Type entry icon to open the editor.
      • Replace the types statement with the user-defined type.

    Example: TYPES : Begin of s_material,                                         “structure

    matnr TYPE MARC-MATNR,

    werks TYPE  MARC-WERKS,

    END OF s_material.

     

     

     

    TYPES : Begin of ty_mara,

    matnr TYPE MARA-MATNR,

    mtart TYPE  MARA-MTART,

    meins TYPE MARA-MEINS,

    matkl TYPE MARA-MATKL,

    END OF ty_mara.

    TYPES: tt_mara TYPE TABLE OF ty_mara.                  “table type

      • Save it.
    1. In the method parameter, add the parameter with associated type name as user-defined type name. And add the required code in method. Save and Activate it.
  • OOPS ALV in SAP ABAP

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

    Introduction

    The OO ALV is ALV using object-oriented ABAP. ALV stands for ABAP List Viewer and provides the standard list format and user interface for all the ABAP programs. The advantage of using OOPS ALV in SAP ABAP is that is flexible and used to build interactive and modern-designing list. SAP has provided a class (class name: CL_GUI_ALV_GRID) which acts as the wrapper class encapsulating ALV GRID functionality.

    The basic components for ALV Grid are:

    • List data: List data are the internal table data’s to be listed.
    • Field Catalog: The field catalog is the internal table that defines the specification on the display of fields. To generate field catalog there are three methods: Automatic generation, semi-automatic generation and manual generation. The internal table must be referred to LVC_T_FCAT.
    • Layout Structure: The layout structure must be of type “LVC_S_LAYO”. This can be used to modify the layout including colour adjustments, grid customization etc.
    • Event Handler: To handle events triggered by ALV, event handler class should be defined and implemented.
    • Additional Data: To stimulate additional data in ALV Grid, additional data should be passed as a parameter.

    Example

    Follow the steps to create an OOPS ALV in SAP ABAP:

    1. Create an object of class CL_GUI_CUSTOM_CONTAINER and class CL_GUI_ALV_GRID.
    2. Populate internal table.
    3. Call screen in which you want to display the list.
    4. Create Custom Container from layout lab
    5. Call method of class CL_GUI_ALV_GRID and pass the required parameters.

    Program

    "SCARR is the SAP Standard table which stores Airlines data
    DATA: t_flight TYPE TABLE OF SCARR.
    DATA: cust_container TYPE REF TO CL_GUI_CUSTOM_CONTAINER,
    cust_grid TYPE REF TO CL_GUI_ALV_GRID.
    Select * from SCARR into table t_flight.
    CALL screen 100.
    MODULE STATUS_0100 OUTPUT.
    SET PF-STATUS 'SCREEN1'.
    SET TITLEBAR 'AIRLINES LIST'.
    ENDMODULE.
    MODULE USER_COMMAND_0100 INPUT.
    CASE SY-UCOMM.
    WHEN 'BACK'.
    LEAVE TO SCREEN 0.
    ENDCASE.
    ENDMODULE.
    MODULE LIST OUTPUT.
    CREATE OBJECT CUST_CONTAINER
    EXPORTING
    CONTAINER_NAME = 'CONTAINER'.
    CREATE OBJECT CUST_GRID
    EXPORTING
    I_PARENT = CUST_CONTAINER.
    CALL METHOD CUST_GRID->SET_TABLE_FOR_FIRST_DISPLAY
    EXPORTING
    i_structure_name = 'SCARR'
    Changing
    it_outtab = t_flight.
    ENDMODULE.

    After you paste the above code, you need to double-click on “CALL screen 100.” so that you can create a Screen. If you try to execute without creating a screen, then you will get an exception mentioning the same, i.e., “No Screen 100 available.”

    After double-clicking the screen, you will see the below view, update info as shown below:

    OO ALV Screen

    Double click on “SET PF-STATUS ‘SCREEN1’. ” and enter these:

    Screen 1 Status

    Add a button by double-clicking on Screen 100 and performing below:

    Add a button

    Back Code

    Update and Activate the screen after changing the below:

    Flow Logic in Screen

    Now change the Layout as shown below:

    1. Click on Layouts

    Layouts

    2. Create a custom container as shown below, choose the button and draw a rectangular shape:

    Screen Painter

    3. It will create something, as shown below. Double-click on it and then enter the name as “CONTAINER”

    CONTAINER

    5. Now Activate the screen, go back to the program, activate the program and run the program, you will see the OO ALV screen as shown below:

    OO ALV Output

    Tutorial Video

    You can watch the video below to learn implementation:

    [embedyt] https://www.youtube.com/watch?v=_duuxLwTq7I[/embedyt]
  • Class Pools and Interface Pools in SAP ABAP

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

    Introduction

    So far we have learnt about the classes and interfaces, their properties, uses and many more things. We can define the class and interface either globally in the repositories or locally in the ABAP program. When the classes and interfaces are defined globally, class pools and interface pools generated by CLASS BUILDER serves as a container and each class pool and interface pool contains a single class or interface. The benefit of defining globally is that they can be accessed by any ABAP program. Whereas when defining locally, they are accessed only by their own program.

    Class Pools

    A class pool acts as the container for classes defined globally and has program type K. It is somewhat similar to module pools or function groups (act as containers) except the fact that the class pools don’t have their own screens or processing blocks than methods. It contains both declarative as well as executable statements. The CREATE OBJECT statement creates the objects to execute the statements in class pools.

    The class pool is introduced by the statement CLASS-POOL in master program. The naming of the master program will contain the global class name and is padded with “=” up to 30th position and ends with “CP”.

    Example, The name of the interface pool for global interface CL_DEMO_GO_CODING is CL_DEMO_GO_CODING=============CP.

    Structure of Class Pool
    Below is the structure of Class Pools:

    Class Pools and Interface Pools in SAP ABAP

    The types defined in the class pool are only visible in the implementation part of the global class.

    Interface Pool

    An interface pool acts as the container for interfaces defined globally and has program type J. Unlike the class pools, interface pools do not have any executable statements. They contain the interface definition which can be implemented by global and local classes.  When interfaces are implemented in global class, the interface definition is also included in the class definition.

    Similar to class pool, the name of the master program of interface pool contains the global interface name and is padded with “=” up to 30th position and ends with “IP”.

    The name of the interface pool for global interface IF_DEMO_GO_CODING is IF_DEMO_GO_CODING=============IP.

  • Events in ABAP Class

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

    Introduction

    Events in ABAP Class are the functions in a class which triggers the event handlers of other class based on the outcomes. With the use of events, any number of event handler methods can be called. The link between the triggering event and event handler is decided at run-time.

    Definition

    An event is the mechanism by which a method of one class can invoke the method of other class without instantiating that class.

    Steps to add Events in ABAP Class

    • Define an event
    • Define a method
    • Link event and method
    • Create a triggering method to raise the event
    • Define SET HANDLER statement at an instance in the program

    Points to consider

    • Event handler method is defined by FOR EVENT <event-name> OF <class-name>.
    • It has only output parameters.
    • To pass output parameters to the event handler method, RAISE EVENT statement is used.
    • SET HANDLER method is used to link the event to its handler methods in a program.

    Types of Events

    Events are of two types:

    • Instance events: Instance events can only get triggered in instance methods. It is declared using EVENTS statement.

    EVENTS <event-name>….

    • Static events: Static events are triggered by both instance and static methods. And also static methods can only trigger static events. It is declared using CLASS-EVENTS.

    CLASS-EVENTS <event-name>….

    Syntax

    FOR EVENT <event-name> OF <class-name>.

    Example

    Lets’ pin down an example:

    **** main class definition****

    CLASS zcl_demo DEFINITION.

    PUBLIC SECTION.

    DATA : num1 TYPE I.

    METHODS: compare IMPORTING num2 TYPE I.

    EVENTS: event_compare.

    ENDCLASS.

    ****event handler definition****

    CLASS zcl_eventhandler DEFINITION.

    PUBLIC SECTION.

    METHODS: handling_compare FOR EVENT event_compare OF zcl_demo.

    ENDCLASS.

    ****class implementation ****

    CLASS zcl_demo IMPLEMENTATION.

    METHOD compare.

    num1 = num2.

    If num1 > 10.

    RAISE EVENT event_compare.                                                              “Raise an event

    ENDIF.

    ENDMETHOD.

    ENDCLASS.

     

    ****event handler implementation ****

    CLASS zcl_eventhandler IMPLEMENTATION.

    METHOD handling_compare.

    WRITE: “Event triggered… Num1 is greater than 10..”

     

    ENDMETHOD.

    ENDCLASS.

    START-OF-SELECTION.

    ****creating class object****

    DATA : object_demo TYPE REF TO zcl_demo,

    ****creating event handleobject****

    object_eventhandler TYPE REF To zcl_eventhandler.

    CREATE OBJECT object_demo.

    CREATE OBJECT object_eventhandler.

    SET HANDLER object_eventhandler-> handling_compare FOR zcl_demo.        “Setting up handler

    object_demo-> compare(2).

    Events in ABAP Class

  • Interface in ABAP

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

    Introduction

    As we know that the principles of Object-Oriented Programming are Encapsulation, Abstraction, Inheritance and Polymorphism. Polymorphism is implemented through Interfaces. It defines the ability of a message to be displayed in many forms. It allows an interface to have multiple implementations.  In this article we will discuss Interface in ABAP OOPs in detail.

    Consider a real-life example; a person at the same time has many roles. A woman at the same time can have many features, she can be a mother, wife, daughter or an employee but actions are as per the situation and conditions. Similarly, an interface is an independent entity which can have many implementations to extend the scope of the classes.

    Properties of Interface

    • Interfaces only have method definitions. Method implementation is done in implemented classes.
    • Interfaces can be used by any no of classes to extend the scope of the class.
    • Each class can have its own implementation of interface methods depending upon the class functionalities.
    • The declaration of interface does not include the visibility section. But in the declaration part of a class, they must be listed under Public Section.

    You can create an interface in SE24. Provide the interface name, methods and its parameters and implement the interface in classes.

    Definition of Interface in ABAP

    Interface is an independent structure which is used in classes to enhance its functionality.

    Example

    Let’s pin down an example.

    ****interface definition****

    INTERFACE zif_demo.

    METHOD display.

    ENDINTERFACE.

    ****class definition ****

    CLASS zcl_demo DEFINITION.

    PUBLIC SECTION.

    INTERFACES zif_demo.

    END CLASS.

    CLASS zcl_demo1 DEFINITION.

    PUBLIC SECTION.

    INTERFACES zif_demo.

    END CLASS.

     

    ****class implementation****

    CLASS zcl_demo IMPLEMENTATION.

    METHOD zif_demo~display.                                                              “Interface Method implementation

    WRITE : “This is the interface method in class 1..”

    END METHOD.

    END CLASS.

    CLASS zcl_demo1 IMPLEMENTATION.

    METHOD zif_demo~display.                                                              “Interface Method implementation

    WRITE : “This is the interface method in class 2..”

    END METHOD.

    END CLASS.

     

    ****creating class object****

    START-OF-SELECTION

    DATA : object_demo TYPE REF TO zcl_demo.

    CREATE OBJECT object_demo.

    CALL METHOD : object_demo-> zif_demo~display.

     

    DATA : object_demo1 TYPE REF TO zcl_demo1.

    CREATE OBJECT object_demo1.

    CALL METHOD : object_demo1-> zif_demo1~display.

     

    Interface in ABAP

    In the above example, there is one interface zif_demo defined which has only the definition part. And we have two classes zcl_demo and zcl_demo1 implementing the interface. This is how we can achieve Polymorphism through interfaces.

  • Alias in SAP Classes

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

    Introduction

    An alias in general term is used to indicate the alternate name for a given person, or thing. Similarly, in OOPs ABAP, an alias is an alternate name given to the interface methods implemented in the class. Whenever we implement the interface in a class, the interface method is copied with naming convention <Interface-Name>~<Method-Name>. Instead of using the default interface method name in the class, we can provide an alternate name to it (called alias) and can use alias name in the program. In this article we will learn more about Alias in SAP Classes.

    Definition

    An alias is an alternate name for interface methods that are implemented in the class.

    How to give alias name to interface methods in the SAP Classes?

    In transaction SE24, provide the class name and open it in editable mode. There will be one Alias tab. Provide the alias name corresponding to the interface methods.

    Calling interface methods in the class will be easy by the use of an alias.

    CALL METHOD <object-name> -> <alias-interface-method>

    Syntax

    The below syntax is used to give alias in the program:

    ALIASES  <alias-name> FOR <interface-method>.

    Example

    Let us suppose an interface is implemented in the class and we have provided alias name to the interface method.

    For example,

    ZIF_MATERIAL~GET_MATERIAL_DETAILS (Interface Method)    –       DETAILS (Alias Name)

    DATA : object_name TYPE REF TO zcl_demo.

    DATA: wa_mara TYPE MARA.                                                                      “MARA Declaration

    PARAMETERS p_matnr TYPE MARA-MATNR.                                       “User Input

    CREATE OBJECT object_name.                                                                   “Object creation

    START-OF-SELECTION.

    ** CALL METHOD object_name -> ZIF_MATERIAL~GET_MATERIAL_DETAILS.**

     

    CALL METHOD object_name -> details.                                                   “Method call with alias name

    EXPORTING

    IM_MATNR = p_matnr

    IMPORTING

    EX_MATNR = wa_mara.

    The above code retrieves the material details based on the input material number (MATNR) from interface ZIF_MATERIAL. Here we have used the alias name DETAILS as method name for the simplicity of understanding the code. Instead of DETAILS, we can also use ZIF_MATERIAL~GET_MATERIAL_DETAILS, but using this more than once in tricky code will make the code much more complex.