ABAP CDS has released a new killer feature: CDS enumerated types. CDS enumerated types make enumerations globally available and reusable in different contexts.
Enumerations define a fixed set of values, and objects typed with an enumerated type can have only one of the predefined values. So enumerations contribute to type safety and data consistency. Enumerations were first introduced in the ABAP programming language with ABAP release 7.51. For details, see horst.keller's blog post ABAP News for Release 7.51 – Enumerations | SAP Blogs. 0281bd377f70426e8bb9d2d0005e7762 also explains various enumeration techniques, such as enumerations as constants in interfaces, DDIC domains with fixed values, and OO enumerations Enumerations in ABAP | SAP Blogs.
This blog post explains the new ABAP CDS enumerations.
In ADT, an enumerated type is created as a repository object of the type Type.
A template for enumerated types is also available.
A CDS enumerated type is defined in the ABAP development tools for Eclipse (ADT) as a CDS type definition with the following syntax:
Definition of an enumerated type DEMO_CDS_ENUM_WEEKDAY. The base type is abap.int1 and the enumerated values of the enumerated constants MON, TUE, ... are 0 to 6.
@EndUserText.label: 'Days of the week' define type DEMO_CDS_ENUM_WEEKDAY : 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, bloody Sunday' SUN = 6; }
In ABAP, an equivalent enumerated type can be defined as follows:
"ABAP enumerated type with enumerated structure TYPES: BEGIN OF ENUM abap_wd STRUCTURE abap_wd BASE TYPE int1, mon, tue, wed, thu, fri, sat, sun, END OF ENUM abap_wd STRUCTURE abap_wd.
But please note that you of course cannot make assignments between the two enumerated types, because an enumerated type is compatible only with itself.
In ABAP Dictionary, you can define domains with fixed values. The fixed values are a set of allowed values, similar to the enumerated constants. However, the domain fixed values are evaluated for the input help of dynpro fields. CDS enumerated types make enumerations globally available and reusable in different contexts. CDS enumerated types are intended to replace domains with fixed values.
CDS enumerated types can be used as follows:
The enumerated type can be used for declaring an enumerated variable wd in an ABAP program. By doing so, an enumerated structure named demo_cds_enum_weekday is implicitly declared in the current context and can be used there. wd initially contains the content of the initial enumerated constant demo_cds_enum_weekday-mon.
DATA wd TYPE demo_cds_enum_weekday. ASSERT wd = demo_cds_enum_weekday-mon. cl_demo_output=>display( demo_cds_enum_weekday ).
The result looks as follows:
The CDS enumerated type can be used in CDS entities for typing of elements or parameters, as operands in expressions, and in comparisons.
Here’s an example:
@EndUserText.label: 'Cast to enum' @AccessControl.authorizationCheck: #NOT_REQUIRED define view entity demo_cds_enum1 with parameters p_weekday :DEMO_CDS_ENUM_WEEKDAY as select from DEMO_CDS_ENUM_2 { key id, int1, cast(int1 as DEMO_CDS_ENUM_WEEKDAY) as weekday1, DEMO_CDS_ENUM_CHAR.#first_value as EnumConstant } where weekday = $parameters.p_weekday
The elements weekday and EnumConstant both have the data type ENUM. You can see this in the debugger and in the code element information in ADT.
In ABAP SQL, CDS enumerated constants can be used in operand positions such as the WHERE clause and the ORDER BY clause. The enumerated constants of a CDS enumerated type can be accessed with a component selector: EnumName-EnumConstant.
The following code snippet demonstrates accessing the CDS view entity using ABAP SQL. It first inserts data into the database table DEMO_DDIC_TYPES and then it selects all elements from the CDS view entity. The columns weekday and EnumConstant of the inline declared table result both have an enumerated type.
Note: If the field INT1 contains a value that is not contained in the list of enumerated values of the CDS enumerated type DEMO_CDS_ENUM_WEEKDAY, a runtime error occurs.
*fill database table DELETE FROM demo_ddic_types. INSERT demo_ddic_types FROM TABLE @( VALUE #( ( id = 'A' int1 = 1 ) ( id = 'B' int1 = 6 ) *( id = 'C' int1 = 7 ) -> runtime error because 7 is not an allowed value ) ). *SELECT from cds view entity SELECT * FROM demo_cds_enum_2 INTO TABLE @DATA(result) WHERE EnumConstant = @demo_cds_enum_char-first_value. *display result cl_demo_output=>display( result ).
The result looks as follows:
The following screenshot shows the debugger view of the variable result. You can see that the columns Weekday and EnumConstant of the result table declared inline both have an enumerated type.
It is currently not possible to define database tables with enumerated fields. The ENUM data type is not available for typing in ABAP Dictionary. However, the ABAP CDS team is currently preparing a new CDS entity as a successor to DDIC database tables, called CDS table entity. The fields of CDS table entities can have the data type ENUM.
Happy Coding!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
3 | |
2 | |
2 | |
2 | |
2 | |
1 | |
1 | |
1 | |
1 | |
1 |