User-defined types in SAP Classes

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.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.