Category: SAP

  • 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.

  • SAP SmartForms Trace

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

    Introduction

    The SmartForms trace allows us to trace the processing of the Smart Form during printing. It can be analyzed to determine the sequence of the processing of the nodes and the point at which the processing was terminated due to an error.

    SAP SmartForms Trace

    To trace the SAP SmartForms processing, the SFTRACE transaction is used. When a trace is activated using the generated function module, the Smart Form stores the trace for each output in the database.  The degree of the details of the trace can be set. The trace is automatically deactivated after a day; however, the trace files are retained.  The trace output is user-specific which means it is activated only for the user who activated it.

    Trace Settings

    The following settings are available if the trace is active:

    • Level: Determines the degree of the details of the trace.
    • Logical Page (Name): Restricts the trace output to a specific page.
    • Window: Restricts the trace output to a specific window on a page.
    • ABAP Breakpoints: Warning and Error options are used internally.

    Trace Level

    This determines the degree of the details of the trace. Level 5 contains general information while Level 80 contains the most detailed trace.

    The following are the trace levels:

    • Warning or error (Level 5): It contains trace level, trace version, system date and time, and system information.
    • Document information (Level 15): It contains additional information like output channel, form attributes, form structure, and output results.
    • Pages (Level 30): It contains additional information like the name of the processed pages (except the name of the start page).
    • Window (Level 40): It contains additional information like names of the processed windows
    • Output area (Level 45): It also contains information about the table output area.
    • Cell change (table) (Level 47): It also contains the outputs on line types and cells processed in the tables.
    • Text information (Level 50): It also contains attributes values of the processed output nodes like text elements, text modules, etc.
    • Fields (Level 60): It also contains the names and values of the fields in the text outputs,
    • Scanner token (Level 80): It is the maximum trace level and also contains the scanner outputs when analyzing the texts in the text nodes.

    Activating and Configuring the Trace

    The trace is deactivated by default. To trace the form, trace has to be activated and configured. Steps to activate and configure the trace:

    1. Enter SFTRACE in the common field.

    SAP SmartForms Trace

    1. Click on Switch On Once clicked, the status changes from Trace Inactive to Trace Active.

    SAP SmartForms Trace Admin

    1. Set the trace level and other settings.
    2. Press Enter to confirm the entries.

    Once the trace is active for the user, whenever the Smart Form is called, a new trace is created every time. The trace is automatically deactivated after a day; however to manually deactivate the trace click on Switch Off pushbutton.

     

    Analyzing Trace Output

    Traces are stored in the Traces in the database frame of the SFTRACE transaction window. Use the Refresh button to get the updated trace.

    To analyze the trace in the system:

    1. Select the line by pressing the pushbutton on the left side of the table.

    Traces in Database

    1. Click on Display Trace to see the trace screen.

    Analyzing Trace Output

    The traces can be stored locally by selecting the Export   button. To read the locally stored trace, select Read trace from local file button.

    Trace Output

  • Output Types in SAP SmartForms

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

    Introduction

    In classic form printing, the forms are printed using a printer. Smart Forms provides different output types or formats for different use cases. Form Attributes under General Settings node is used to determine the default settings for the form output. The default settings can be overruled using the standard parameters while calling the Smart Forms. In this article we will discuss the Output Types in SAP SmartForms in details.

    Output Types in SAP SmartForms

    Different output types/formats for the SmartForms are:

    • Output Text Format (OTF): It is a standard output format for printing forms in SAP systems. It is the default format in the Form Builder. It can be converted to other formats. It is usually output using spool processing. For this output, in the system, a printer must be set up with a device type that understands OTF.
    • XML for Smart Forms (XSF): It is an XML schema designed for the output of form contents. It does not contain any layout information. It is used to connect to external components of the SAP system to process the contents of the form. To use spool processing for XSF output, at least one printer of type ‘XSF’ must be set in the system.
    • XML Data for Forms (XDF): It is an XML schema that describes the contents of the form. It neither contains the form content nor the layout. To use spool processing, at least one printer of type ‘PLAIN’ must be set in the system. It is stored in the spool in binary format to ensure that no data is lost during conversion.
    • Hypertext Markup Language (HTML): It is a combination of XSF and HTML format that generates the layout information to be able to display into the internet application in order to complete business processes via electronic media rather than using printed mail. The Smart Forms returns a number of structures and tables for HTML outputs.
  • SAP SmartForms Flow Control

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

    Introduction

    The flow of the processing of the Smart Form can be controlled using the tree hierarchy defined in the navigation window. In this article we will explain the flow control in SAP SmartForms.

    Conditions affecting Flow Control

    There are multiple options that affect the sequence of the processing of the form.

    Output Conditions:

    This is used to suppress the processing of individual nodes or entire subtrees in the tree structure.

    • To enter the output condition, select the node and go to the Condition tab in the maintenance window.
    • In this tab, a condition can be entered with two operands in each line. An operand can be a value or a field. To select a comparison operator, press on the pushbutton between the two columns. The most important operators are AND and OR. By default, the conditions are evaluated using AND
    • Perform local check to validate output conditions.
    • The output conditions can be linked to output events of pages and windows. Using AND condition in Additional event box is used to link them.

    When the conditions are met, the system processes the nodes/sub-nodes according to the condition value. If not, the system ignores the current node and its sub-nodes.

     

    Alternative Execution:

    It is used to process one of two inferior nodes. To do so,

    • Create a Alternative node in the navigation tree.
    • On the General Attributes tab, define an unstructured condition in the Node Conditions
    • This node has two directly inferior nodes: TRUE and FALSE. Insert the inferior nodes here.
    • Perform local check to validate output conditions.

    If the condition is met, the system processes the directly inferior node TRUE, if not, FALSE.

    Repeated Output:

    To process the output repeatedly, loop nodes are used which reads the data from the internal table line by line. To do so,

    • Create a Loop node in the navigation tree.
    • Enter a unique name and description for the node.
    • Read the data from an internal table to a work area and create inferior nodes that display the fields of the read table lines.

     

    Dynamic Processing:

    This is done by page sequence and numbering. This is determined by

    • The Next page attribute
    • The volume of the data and space available in the main window
    • As soon as a page is full, the Smart Form triggers a page break, known as a dynamic page break.

    During the processing of the form, the system maintains internal counters, which can be used to display the current page number.

  • Node Types in SAP SmartForms

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

    Introduction

    When a Smart Form is created, the form builder already has two nodes in the Navigation window. “Global Settings” and “Pages and Windows” are the two roots that are created by default for every Smart Form.  In this article we will explore more about Node Types in SAP SmartForms.

    Nodes in SAP SmartForms

    The inferior nodes of the Global Settings are used to maintain Form Attributes, Form Interface, and Global Settings.

    The inferior nodes of the Pages and Windows are used to create pages of the form, position elements on the pages, and determine the flow of the process of the elements.

    Each sub-node of the two root nodes has attributes. These attributes are maintained in the Maintenance Window. The attributes that can be maintained are General Attributes, Output Options and Conditions are the same for almost every type of the nodes.

    • General Attributes shows the general description of the node.
    • Output Options contain properties such as Positions, Style, Box and Shading. It may have additional options depending on the position of the node.
    • Conditions allow the processing of the node only if the conditions are true.

    Node Types in SAP SmartForms

    Let’s discuss the types of nodes:

    1. Output Areas:

    Output Areas

    1. Elementary Nodes:

    Elementary Nodes

    1. Table Outputs:

    Table Outputs

     

    1. Flow Control:

    Flow Control

  • SAP SmartForms Form Logic

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

    Introduction

    As we have discussed already in the previous tutorial about the Form Builder, let’s see about the Form Logic in SAP SmartForms.

    In SAP SmartForms, the form logic defines the data to be read from the database, the conditions, and controls the flow of the form output. The form logic is described by the form structure on the left pane of the Smart Forms interface.

    SAP SmartForms Form Logic
    SAP SmartForms Form Logic

    Nodes in SmartForms

    When a new Smart form is created, there are default nodes on the left side.  Global Settings and Pages and Windows are the two roots that are created by default for every Smart Form.

    The Global Settings node has 3 sub-folders, namely Form Attributes, Form Interface, and Global definitions. The Form Interface contains the data which will be passed to the Smart Form from the calling ABAP program and the Global Definitions contain the data that are available throughout the Smart Form.

    Defining Form Logic in SAP SmartForms

    To define the form logic, a hierarchical structure has to be defined under Pages and Windows in Navigation Window. This hierarchy determines the rules for processing the Smart Form.

    The form logic is used to control the flow of the form output. The following rules for form logic should be remembered:

    • In the tree structure, a tab is defined in the Maintenance Window for every node to link the node to a condition. If the condition is fulfilled, the node is processed and if not, then the node is skipped as well as all its sub-nodes.
    • The nodes are processed from top to bottom.
    • The page break on each page depends on how much space is left on the current page.
    • A next page should be defined for each page and flow control is defined for each page. However, one can go to other pages dynamically.
  • 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.