In the earlier post Exploring ABAP on HANA: CDS Simple Types, one of the unexplored and newer features within Core Data Services – CDS Simple Types was discussed.

This post talks about a similar concept of defining a data type within CDS – Enumerated Types. CDS enumerated types are the successor of DDIC domains with fixed values.

Enumerated Types

CDS enumerated types make the concept of enumerations available in ABAP CDS. An enumeration is a data type that consists of a set of named values, or constants.

For example, days of the week.

Data Type: ZJP_DAYS_OF_THE_WEEK

  • MON – Monday
  • TUE – Tuesday
  • WED – Wednesday
  • THU – Thursday
  • FRI – Friday
  • SAT – Saturday
  • SUN – Sunday

These days can be considered as constants 0 to 6 assigned to each day. Let us look at how this will be coded.

@EndUserText.label: 'Days of the week'
define type ZJP_DAYS_OF_THE_WEEK: abap.int1 enum
{  
  @EndUserText.label: 'Monday'
  MON = initial;
  @EndUserText.label: 'Tuesday'
  TUE =       1;
  @EndUserText.label: 'Wednesday'
  WED =       2;
  @EndUserText.label: 'Thursday'
  THU =       3;
  @EndUserText.label: 'Friday'
  FRI =       4;
  @EndUserText.label: 'Saturday'
  SAT =       5;
  @EndUserText.label: 'Sunday'
  SUN =       6;
}

Let us create this in Eclipse.

1. Right click on the package, and choose New > Other ABAP Repository Object.

2. Chose Type from Core Data Services node

3. Provide Name and Description, click Next, provide a TR, and then click Next again.

4. Choose Template defineTypeWithEnum from the list and click Finish.

5. Complete the type with the code already provided above and Activate.

How to use Enumerated Types

1. Type for variables in ABAP

The enumerated type can be used for declaring an enumerated variable in ABAP.

CLASS zjp_enum DEFINITION
  PUBLIC
  FINAL
  CREATE PUBLIC .
  PUBLIC SECTION.
    INTERFACES if_oo_adt_classrun.
ENDCLASS.

CLASS zjp_enum IMPLEMENTATION.
  METHOD if_oo_adt_classrun~main.
    DATA day TYPE zjp_days_of_the_week..
    day = zjp_days_of_the_week-mon.
    out->write( day ).
  ENDMETHOD.
ENDCLASS.

Execute the class to get below output.

If you are curious, you can write the type itself to console.

out->write( zjp_days_of_the_week ).

Let us observe the variables in the debug mode. The value is MON but the base value is 0 for the variable day. The base value gets stored in database and the value is displayed.

2. Type for Table Entity fields

Create a CDS Table Entity and use the type ZJP_DAYS_OF_THE_WEEK as reference for the field. Note that this is not a DDIC table, this is a table entity.

@ClientHandling.type: #CLIENT_DEPENDENT
@AbapCatalog.deliveryClass: #APPLICATION_DATA
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'CDS Enumerated Types'
define table entity zjp_cds_te_enum
{
  key client        : abap.clnt;
  key day_of_weee_num : abap.int2;
  day_of_week_enum : ZJP_DAYS_OF_THE_WEEK; 
}

The entity then can be used in ABAP program. The data can be added to the table entity using below code.

CLASS zjp_enum DEFINITION
  PUBLIC
  FINAL
  CREATE PUBLIC .

  PUBLIC SECTION.
    INTERFACES if_oo_adt_classrun.
ENDCLASS.

CLASS zjp_enum IMPLEMENTATION.
  METHOD if_oo_adt_classrun~main.

    DATA : it_days TYPE STANDARD TABLE OF zjp_cds_te_enum.
    it_days = VALUE #(
        ( day_of_week_num = 0 day_of_week_enum = zjp_days_of_the_week-mon )
        ( day_of_week_num = 1 day_of_week_enum = zjp_days_of_the_week-tue )
        ( day_of_week_num = 2 day_of_week_enum = zjp_days_of_the_week-wed )
        ( day_of_week_num = 3 day_of_week_enum = zjp_days_of_the_week-thu )
        ( day_of_week_num = 4 day_of_week_enum = zjp_days_of_the_week-fri )
        ( day_of_week_num = 5 day_of_week_enum = zjp_days_of_the_week-sat )
        ( day_of_week_num = 6 day_of_week_enum = zjp_days_of_the_week-sun ) ).

    out->write( it_days  ).

    INSERT  zjp_cds_te_enum FROM TABLE @it_days.  "Error handling not done

  ENDMETHOD.

ENDCLASS.

The console output shows the table as below. We have one numeric and one enumerated field.

The data also gets added to the CDS table entity.

More details on Enumerated Types

Syntax for the enum type is as below.

[@type_annot1] 
[@type_annot2] 
... 
  DEFINE TYPE EnumType : BaseType ENUM 
  { 
    [@enum_annot1] 
     EnumConstant1 = EnumValue1 | INITIAL; 
    [@enum_annot2] 
     EnumConstant2 = EnumValue2 | INITIAL; 
    [...] 
  } 

Following base types are allowed.

BaseTypeDictionary Type 
abap.int1INT1 
abap.int2INT2 
abap.int4INT4 
abap.char( len ).
len can be 1 to 8.
 CHAR with length len.
abap.numc( len ).
len can be 1 to 8.
 NUMC with length len.

Enumerated operands can be used in –

  1. Logical expressions
  2. WHERE clause
  3. HAVING clause
  4. Association ON condition
  5. Join ON condition
  6. Complex case distinction

Enumerated Types help organize the constants, streamline the code and they also help in debugging as we don’t have to keep looking up what the constant values mean.

Visit ABAP on HANA series for Tutorials on CDS, AMDP, Eclipse, and ALV IDA.


If you like the content, please subscribe…

Join 517 other subscribers

Discovering ABAP YouTube Channel