Category: ABAP

ABAP

  • Differences between REFRESH and CLEAR

    Preface – This post is part of the Differences in ABAP for Interviews series.

    Introduction

    Before discussing the differences between REFRESH and CLEAR statement, we will have a short introduction of the two. Both of these Statements are part of SAP ABAP Reports/Editors and are mainly used to clear out the used variables and save memory or reuse the variables.

    REFRESH

    REFRESH statement sets the internal table to its initial size by deleting all the rows of the internal table. This frees up the allocated memory for the internal table.  A FREE statement is used to delete entire rows and free all the memory(used when the internal table is no longer needed).

    Syntax of REFRESH statement:

    REFRESH <itab>.

    For an itab, an internal table has to be specified.

    If an internal table itab has a header line, then the table body is initialized, not the table header. If itab has no header, it acts as a CLEAR itab statement. Hence, it is recommended to always use CLEAR statement instead of REFRESH.

    The use of a table with a header line is forbidden in classes, which makes the use of the REFRESH statement obsolete.

    CLEAR

    CLEAR statement clears the header line as well as the rows of the internal table and work area. This frees up the allocated memory required for the internal table and sets it to its initial size.

    Syntax of CLEAR statement:

    CLEAR <dobj>.

    For a dobj, itab or itab[] has to be specified.

    CLEAR itab is used to clear only the header line and the table rows i.e. clear the content of the structure. If the table header line is not defined, CLEAR itab clears the table content.

    CLEAR itab[] is used to clear the internal table contents.

    Differences between REFRESH and CLEAR

    Now, let’s have a look at their difference.

    REFRESH statement CLEAR statement
    It always refers to an internal table. It refers to an internal table as well as a work area.
    Header line cannot be cleared using this statement. Header line can be cleared using the CLEAR itab statement.
  • Differences between GET CURSOR and HIDE

    Preface – This post is part of the Differences in ABAP for Interviews series.

    Introduction

    Before discussing the differences between GET CURSOR and HIDE, let’ have a quick introduction of the two.

    Interactive report in SAP ABAP is a report which displays the basic information on the basic list and the detailed information on the secondary list.

    Among various list events in the interactive report, the AT LINE-SELECTION is an event that is triggered when the user clicks on any list item. To know the selected list contents, there are 2 keywords or statements, GET CURSOR and HIDE.

    GET CURSOR

    GET CURSOR statement is used to read the name of the output field or the number of the list line on which the screen cursor in the displayed list is positioned and store the value into the variables field or line depending on how the FIELD or LINE is specified.

    Syntax: –

    GET CURSOR { {FIELD field [ field_properties]}
    | {LINE line [ line_properties]} }.

    HIDE

    HIDE statement is used to hide the content of the current output line into a temporary memory called hide area for further processing. HIDE statement is used withing loop…end loop to hide the values in the hide area.

    On double click on any list line, an event AT LINE-SELECTION is triggered and the system automatically identifies the line number and stores the corresponding record into the hide variable.

    Syntax:

    HIDE dobj.

    This statement places the contents of the dobj for the current output line (system field SY­LINNO) into the HIDE area. The data type of the dobj must be flat and no filed symbol or class attributes can be specified.

    Differences between GET CURSOR and HIDE

    Now, let’s have a look at their difference.

    GET CURSOR HIDE AREA
    It is used to store the value of the selected field name. It is used to store the line with line number.
    It responds for a particular field It responds for a particular line.
    Interactive reports can be generated using field name and field value Interactive report cannot be generated using this.

     

  • Constructor in a Class

    Preface – This post is part of the SAP ABAP OData Tutorial series.

    Introduction

    Focusing on the concept of the constructors in a class, we will discuss what is a constructor and its type with an example. So, a constructor can be understood as a special method in a class that is automatically called when an object is created. It does not require any explicit method call.

    Definition

    A constructor is a special method in a class that is invoked at the time object is created or instantiated.

    Type of Constructors

    Constructors are of two types:

    • Instance constructor
    • Static constructor

    Instance Constructors

    The instance constructor is the predefined instance method of the class called constructor. There are few points to consider while declaring the constructor. They are:

    • The name of the constructor method, irrespective of the class name or type, must be always CONSTRUCTOR.
    • It is declared using a method statement in the visibility section of the class.
    • The method signature can only have importing parameters or exceptions. There are no exporting parameters included.
    • The instance method can be used to set default values in the class.
    • The instance method anyhow can access both instance as well as static variables.
    • It is called each time the object is created.

    Static Constructors

    The static constructor is the predefined static method of the class called as class_constructor. There are few points to consider while declaring the static constructor. They are:

    • The name of the class_constructor method must be CLASS_CONSTRUCTOR.
    • It is declared using CLASS_METHODS statement in the public section of class.
    • The method signature cannot contain importing or exporting parameters or exceptions.
    • The static method can be used to set default values globally.
    • The static method can only access the static variables.
    • It is called exactly once before the class is accessed for the first time.

     

    Example

    Let’s spot one simple example:

    CLASS  demo DEFINITION.
    
           PUBLIC SECTION.
    
                 METHODS:  CONSTRUCTOR.                                                         “Instance Constructor”
    
                 CLASS_METHODS: CLASS_CONSTRUCTOR.                             “Static Constructor”
    
    END CLASS.
    
    CLASS  demo IMPLEMENTATION.
    
           METHOD CONSTRUCTOR.
    
                    WRITE: “Instance Constructor is initiated”.
    
           END METHOD.
    
           METHOD CLASS_CONSTRUCTOR.
    
                    WRITE: “Static Constructor is initiated”.
    
           END METHOD.
    
    END CLASS.
    
    **class object creation**
    
    DATA: demo_obj TYPE REF TO demo.
    
    CREATE  OBJECT  demo_obj.

    OUTPUT

    Static Constructor is initiated

    Instance Constructor is initiated

     

    In the definition part of the class, the CONSTRUCTOR is the instance constructor defined using Methods and static constructer is defined as CLASS_CONSTRUCTOR using class_methods. And their implementations are given the IMPLEMENTATION part. When the object  demo_obj is created, the instance and static constructor is called.

    As we can see that the output displays static constructer implementation before the instance constructor, this is because the CLASS_CONSTRUCTOR method is triggered before the CONSTRUCTOR method.

  • Difference between Update function module and Regular function module

    Preface – This post is part of the Differences in ABAP for Interviews series.

    Introduction

    Before discussing the difference between Update function module and Regular function module in SAP ABAP, let’s have a short introduction of the two.

    Function modules are procedures that have a set of re-usable statements with importing, exporting parameters, etc. They are created in ABAP workbench using the Function Module Builder. They play an important role in updating and in the interaction between different SAP systems, between SAP systems and remote systems through remote calls.

    There are 3 Processing types of function module:

    1. Regular Function Module
    2. Remote-Enabled Function Module
    3. Update Module

    Regular Function Module

    Regular Function Module and Update Function module consists of a block of statements that are made available globally all over the SAP landscape.

    Update Module

    The Update function module is a function module for which the processing type property of the function builder is set to Update Module. This function module is not executed immediately but is scheduled for execution in a special work process called update work process. For this purpose, the name of the update function module along with the actual parameter is registered in the database table VBLOG as a log record.

    Syntax to register the function module to the log table:

    CALL FUNCTION <function_name> IN UPDATE TASK

    [EXPORTING p1 = a1 … fn = an] [TABLES  f1 = a1 … fn = an

     The actual execution of the program is triggered by the COMMIT WORK statement. The values of the actual parameters are received by the formal parameter of the function module from the VBLOG table. After registration of the function module, if the COMMIT WORK is not triggered then the function module is not executed and the entries are deleted from the log table when the program ends.

    The regular function module does not need to be registered to be called. This is executed immediately whenever a call to this function module is made. Sy-subrc can be checked for success and failure.

    Syntax to call a Regular function module:

    CALL FUNTION  <function_name>

    [EXPORTING  p1 = a1 … fn = an] [IMPORTING  f1 = a1 … fn = an] [CHANGING   f1 = a1 … fn = an] [TABLES     f1 = a1 … fn = an] [EXCEPTIONS e1 = r1 … en = rn

    [ERROR_MESSAGE = rE] [OTHERS = ro]].

    Now, let’s have a look at their difference.

    Difference between Update function module and Regular function module

    Update Function Module Regular Function Module
    It needs to be registered first in the log table There is no need to register
    It is executed when COMMIT WORK statement is triggered. It is executed immediately when a call is made to this function.
    Sy-subrc is undefined after execution Sy-subrc can be checked after the execution

     

  • Difference between User Exits and Customer Exits

    Preface – This post is part of the Differences in ABAP for Interviews series.

    Introduction

    Before discussing the difference between User Exits and Customer Exits in SAP ABAP, let’s have a short introduction of the two.

    The enhancement allows the user to add their functionalities to the SAP standard applications without modifying the original application. Enhancement Framework is used to modify the standard SAP behavior as per the customer requirements. There are different ways to add custom functionalities in standard codes. User Exit and Customer Exit are among them.

    User Exits

    User exits are implemented in the form of subroutines and hence also called FORM EXITs. They are collected in Include and are attached to the SAP standard program. They are empty subroutines where one can fill their source code. They are the type of system enhancements originally developed for R/3 SD(Sales and Distribution). Technically they modify the existing code.

    Syntax:

    FORM USEREXIT_XXXX
    INCLUDE ZZUSEREXIT_XXXX
    ENDFORM.

    Customer Exits

    Customer exits act as hooks where one can hang their new functionalities. These are created for specific programs, screens, and menus within the standard applications. Technically, they are enhancements as they provide add-on functionality. They are used for all modules like MM, SD, FICO, PP etc.

    There are 3 types of Customer Exits:

    1. Function Module Exits

      It is implemented as a call to the function module. It allows the developer to add code in include that is implemented in a function module at a specific location in an SAP application program.

    Syntax:  CALL CUSTOMER-FUNCTION ‘910’

    1. Screen Exits

      It allows the customers to add fields to a screen in the SAP program via sub-screen which is called within standard screen’s flow logic.

    Syntax:  CALL CUSTOMER-SUBSCREEN CUSTOM_SRC

    1. Menu Exits

      These are implemented by SAP and are reserved menu entries in the GUI interface. It allows adding functionalities to menus.

    Syntax:   +CUS (additional item in GUI status)

    Now, let’s have a look at their difference.

    Difference between User Exits and Customer Exits

    User Exits Customer Exits
    It is implemented in the form of subroutine It is implemented for specific programs, screens, and menu within standard applications
    It is technically a modification as existing code are changed It is technically an enhancement as this provides add-on functionality
    It refers to SD module only It refers to all modules like MM, SD, FICO, PP etc.
    All variables, structures, internal tables, etc. are exposed to be viewed and/or changed Only variables, structures, and tables that are passed into the function through the interface are restricted to be viewed and/or changed
    It requires an access key It does not require any access key
    Changes remain after the upgrade Changes are lost after upgrade
    It gets automatically activated whenever the application/program is activated To implement this, a project is created in CMOD then activated

     

  • Difference between Primary Key and Foreign Key

    Preface – This post is part of the Differences in ABAP for Interviews series.

    Introduction

    Before discussing the difference between, let’s have a short introduction of the two.

    Keys are the fundamental part of the relational database as they establish a relation between the tables and ensure each record in the table is uniquely identified.

    A database contains a lot of tables which holds thousands of records all of which are unsorted and unorganized. Fetching a particular data from those records can be difficult and challenging. This is where Keys comes to the scenario. The two important keys of the relational schema are Primary Key and Foreign Key. In this article we will discuss the Difference between Primary Key and Foreign Key in detail.

    Primary Key

    A primary key in a table uniquely identifies each record in the table. It may refer to a column or a combination of columns to implicitly identify the records. The input values of the key columns defined must be unique for each record and not contain NULL values. Each table has can have one  Primary key or group of keys that are unique for every record.  No duplicate value is allowed in the primary key field.

    Foreign Key

    A foreign key is a set of one or a combination of columns in a table that is a primary key of another table. It is used in a table is used to establish a relation between tables in the ABAP dictionary, to create value checks for the input fields and to link multiple tables in a view or a lock object. Duplicate value is allowed in the foreign key field.

     

    Difference between Primary Key and Foreign Key

    Table 1 is the foreign key table (dependent table) and Table 2 is the check table(referenced table). Here the Column 3(foreign key of Table 1) points to the Col 1(primary key of Table 2).Column 3 of Table 1 and Col 1 of Table 2 must have same data type and length.

    Difference between Primary Key and Foreign Key

    Now, let’s have a look at their difference.

    Primary Key Foreign Key
    It uniquely identifies a record in the table It establishes relation between table and is a primary key in another table
    A table has mianly one primary key A table can have multiple foreign keys
    It cannot have Null value It can have Null values
    It cannot have a duplicate value It can have duplicate values
    It cannot be dropped from the parent table which refers to the foreign key in the foreign key table It can be dropped from the foreign key table even it is referring to the primary key of the main table

     

  • Difference between Append and Insert

    Preface – This post is part of the Differences in ABAP for Interviews series.

    Introduction

    Before discussing the difference between APPEND and INSERT, let’s have a short introduction of the two.

    Internal tables are used to process the data dynamically during the program execution. Data is populated to the internal table during runtime i.e. program execution.

    APPEND and INSERT are the 2 statements that are used to populate the internal table.

    In this article we will explore more about the Difference between Append and Insert.

    Append

    APPEND statement adds one or more records at the end of the existing internal table.

    Syntax:

    APPEND {<work-area>| INITIAL LINE |LINES OF jtab [FROM idx1] [TO idx2]} TO <itable>.

    <work-area> adds the contents of the work area  <work-area> to the internal table <itable>.

    INITIAL LINE appends a blank line to the internal table <itable> with initial values in each field according to the definition.

    LINES OF jtab [FROM idx1] [TO idx2] appends the rows from idx1 index to idx2 index from jtab to the internal table <itable>.

    The APPEND statement sets sy-tabix with the index of the last appended row.

    Insert

    INSERT statement adds one or more records at a specified position of the internal table. The position can be specified by the primary table key or a table index.

    Syntax:

    INSERT {<work-area>| INITIAL LINE | LINES OF jtab [FROM idx1] [TO idx2]} TO <itable> [INDEX <idx>].

    <work-area> adds the contents of the work area  <work-area> to the internal table <itable>.

    INITIAL LINE appends a blank line to the internal table <itable> with initial values in each field according to the definition.

    LINES OF jtab [FROM idx1] [TO idx2] appends the rows from idx1 index to idx2 index from jtab to the internal table <itable>.

    INDEX specifies the position of the internal table where the new record has to be added. If the Index is not specified, the system adds the new record at the end of the internal table. If the internal table has a record at the specified index, then the new record is added at the specified index and the existing record is reinserted by incrementing the index by 1.

    The INSERT statement does not set the system field sy-tabix.

    Difference between Append and Insert

    Now, let’s have a look at their difference.

     

     

     

    APPEND INSERT
    It is a statement that is used to add a record at the end of an internal table. It is a statement that is used to add/insert record at a specific position of Internal Table.
    New records are always added to the end of the internal table. If the position is not specified only then the new records are added to the end of the internal table.
    System field SY-TABIX is set to the index of the last appended row. System field  SY-TABIX is not set.

     

     

  • Difference between Narrow casting and Wide Casting

    Preface – This post is part of the Differences in ABAP for Interviews series.

    Introduction

    Before discussing the difference between Narrow Casting and Wide Casting, let’s have a short introduction of the two.

    Both the Narrow casting and Wide casting is related to Inheritance concept.

    The subclasses which inherit from the parent (super) class would usually have more components than the parent class, hence the subclasses are Wide.

    On the other hand, the parent (super) classes have a smaller number of components, hence the parent classes are Narrow.

    Narrow casting

    In Narrow casting, an instance of the subclass is assigned to the reference of the superclass. As we are switching from a “more specific view” to a “less specific view”, it is called Narrow casting. By this assignment, only the inherited components of the subclass can be accessed using the reference of the superclass.

    Steps to Narrow Cast:

    i) Declare a reference variable of the superclass

    DATA: lr_parent TYPE REF TO super_class.

    ii) Declare a reference variable of the subclass

    DATA: lr_child TYPE REF TO sub_class.

    iii) Create an instance of the subclass reference

    CREATE OBJECT lr_child.

    iv) Assign the instance to reference of the superclass

    lr_parent = lr_child.

    Wide Casting

    In Wide Casting, an instance of the superclass is assigned to the instance of the subclass. As we are switching to a “more specific view” from a “less specific view”, it is called Wide Casting. By this assignment, all the inherited components and specific components of the subclass can be accessed using the instance of the subclass. The Wide Casting will always fail unless the instance of a subclass has the same type as of the instance of the superclass.  So, it is required to do Narrow casting always before Wide casting.

    Syntax to Wide Cast:

    lr_child ?= lr_parent

    Now, let’s have a look at their difference.

    Difference between Narrow casting and Wide Casting

    Narrow Casting Wide Casting
    It means copying an instance of subclass to an instance of the super class. It means copying an instance of the super class to an instance of sub class
    Only inherited components of the subclass can be accessed. Inherited, as well as specific components of the subclass, can be accessed.
    Assigning operator is used. Casting operator is used.
    Syntax:

    lr_parent = lr_child

    Syntax:

    lr_child ?= lr_parent

     

  • Difference between Check Table and Value Table

    Preface – This post is part of the Differences in ABAP for Interviews series.

    Introduction

    Before discussing the difference between Check Table and Value Table, let’s have a short introduction of the two.

    Table validation is a concept through which the field is restricted to have certain values.

    Field level validation and domain level validation are two concepts of table validation. Field level validations are done to validate entries at field level with the help of a check table whereas domain level validations are done to restrict the values of the field at domain level with the help of fixed values of the domain and value table of the domain.

    Check Table

    Check Table is a table that contains all the valid entries for a field. Fields of a database table whose data type is determined by data element can be assigned the check table. The field of the table to which the check table is assigned is considered as a foreign key field and is part of the foreign key of the table. The table becomes a foreign key table and the primary key of the check table is the foreign key of the foreign key table.  The entries of the primary key of the check table are used as an input help (F4 help) for the foreign key field.

    Value Table

    Value Table is a table that contains all the valid entries of a domain and this domain can be reused in multiple tables. This is maintained at domain level and whenever a foreign key relationship has to be created, the system looks for the value table field, if found, it automatically proposes that name as a check table to generate a foreign key proposal.

    This is a kind of master check and is maintained as a customization object i.e. to enter values to this table a development transport request has to be created and transported.

    Now, let’s have a look at their difference.

    Difference between Check Table and Value Table

    Check Table Value Table
    It is a table which contains all valid entries of a field. It is a table which contains all valid entries of a domain.
    It is a type of field level validation. It is a type of domain level validation.
    It uses foreign key relationship. It is used as a proposal for check table whenever a foreign key relationship is created.

  • Function Imports in SAP OData

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

    DATA: ls_parameter TYPE /iwbep/s_mgw_name_value_pair,
          lv_custid    TYPE kunnr,
          lv_flag      TYPE char1,
          lt_custinfo  TYPE TABLE OF ztest_gw_srv,
          ls_custinfo  TYPE ztest_gw_srv,
          ls_entity    TYPE zcl_ztest_gw_srv_mpc=>ts_msg_return.
    
    
        IF iv_action_name = 'demoFuncImport'. " Check what action is being requested
          IF it_parameter IS NOT INITIAL.
    
    * Read Function import parameter value
            READ TABLE it_parameter INTO ls_parameter WITH KEY name = 'CustId'.
            IF sy-subrc = 0.
              lv_custid = ls_parameter-value.
            ENDIF.
    
            READ TABLE it_parameter INTO ls_parameter WITH KEY name = 'Approved_Flag'.
            IF sy-subrc = 0.
              lv_flag = ls_parameter-value.
            ENDIF.
    
            IF  lv_custid IS NOT INITIAL.
              UPDATE ztest_gw_srv SET approved = lv_flag WHERE cust_id = lv_custid.
              IF sy-subrc = 0.
                ls_entity-type   = 'S'.
                ls_entity-message = 'Customer info successfully approved'.
              ELSE.
                ls_entity-type   = 'E'.
                ls_entity-message = 'Error'.
              ENDIF.
    
    
    * Call method copy_data_to_ref and export entity set data
              copy_data_to_ref( EXPORTING is_data = ls_entity
                      CHANGING cr_data = er_data ).
    
            ENDIF.
          ENDIF.
        ENDIF.