This is a test message to test the length of the message box.
Login
ABAP CDS Types and Enums
Created by Software-Heroes

CDS - Types and Enums

489

What will replace the data elements in the ABAP Dictionary and how can you use the types for Core Data Services today? Find out more here.



In this article, you will learn more about the new types, how you can create them and how you can use them in the system today. We will go into the various special features.

 

Introduction

The table entities will find their way into the cloud products (ABAP Environment and S/4HANA Public Cloud) next year and thus provide an opportunity to replace the old dictionary tables. The problem with today's dictionary is the limitations on the name length and the lack of support for upper and lower case of fields and tables. This is set to change with the new type of table. In this article we compare today's data elements with the new types and their possibilities.

 

Attachment

How can you define the new type? In this section we will look at the two current types and create them in the system.

 

Type (Simple)

First, let's create a simple type. To do this, right-click on the package and start the wizard via "New -> Other ABAP Repository Object". There we look for "Type", this should be under "Core Data Services".

 

Now we give the type a name and a description. Here we can also work in CamelCase, this will be adopted in the types.

 

After selecting the transport, we can choose the corresponding template, in this case "defineType" is sufficient to create the type. We confirm the creation by clicking Finish.

 

The type has now been created and has already been given a label. After the colon we can assign an elementary data type, a data element from the DDIC or another type that we have defined.

@EndUserText.quickInfo: 'Inv. status'
@EndUserText.label: 'Invoice status'
@EndUserText.heading: 'Invoice status'
define type ZBS_DemoCDSInvoiceStatus : abap.char( 1 )

 

In the upper part you can already see the various annotations for the texts in the data element. These can then be translated normally using the "Maintain Translations" app.

 

Type (Enum)

As a second example, we want to create the type "ZBS_DemoCDSInvoiceStatusEnum", which represents an enumeration. To do this, we create a new type using the wizard as above and select the other template in the last step. Now we can define the individual values of the enum and assign a value. The Initial value must always be defined.

@EndUserText.quickInfo: 'Inv. status'
@EndUserText.label: 'Invoice status as Enum'
@EndUserText.heading: 'Invoice status'
define type ZBS_DemoCDSInvoiceStatusEnum : abap.char(1) enum
{
  @EndUserText.label: 'Initial'
  Empty   = initial;
  @EndUserText.label: 'Created'
  Created = 'C';
  @EndUserText.label: 'Payed'
  Payed   = 'P';
  @EndUserText.label: 'Closed'
  Closed  = 'O';
}

 

You can also assign a label to each value to get a text. If we want to use the type from the first step, however, we get an error message; we should use the built-in type.

 

Usage

In principle, you can use the types in Core Data Services if you cast the fields to the new data type. This means that it takes on the properties and labels of our type and we can reuse the element. Currently, neither type can be used for classic DDIC tables, but will later function as types for the table entities. Currently, use is therefore limited to CDS views and objects such as classes.

 

Class (Type)

To use the type in coding, we define a variable or work with inline declaration. In this example, we create the variable using DATA and assign a value. There is no query as to whether the value passed is correct.

DATA ld_simple TYPE ZBS_DemoCDSInvoiceStatus.
ld_simple = 'C'.

 

If we output the value to the console, a corresponding value "C" is also written, just as we passed it to the variable.

out->write( `Simple type:` ).
out->write( ld_simple ).

 

Class (Enum)

Enumeration actually looks similar to how we can assign and use the data type. If we want to assign a value, the easiest way to do this is using the defined values. To do this, we use the type with a hyphen (-), as with structures, and can select the appropriate value using CTRL + 1.

DATA ld_enum   TYPE ZBS_DemoCDSInvoiceStatusEnum.
ld_enum = ZBS_DemoCDSInvoiceStatusEnum-created.

 

If we then output the variable to the console, we get the value "CREATED" and not "C". Let's look at a special case:

ld_enum = 'C'.

 

If we try to assign a value directly, we cannot activate the code and receive an error message in the compiler.

 

Here, however, we can work with a CONV and pass a value. The correct enum is assigned internally. This also allows you to secure various processing or interfaces so that only correct values are passed to the type.

ld_enum = CONV #( 'C' ).

 

However, if you try to pass an incorrect and unavailable value, the exception CX_SY_CONVERSION_NO_ENUM_VALUE is triggered and must be caught or the logic is terminated with a dump.

 

Core Data Service

In the last step, let's look at the current use in a Core Data Service. To do this, we create a new Core Data Service on the invoice table of our data model.

@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Invoice with Types'
@Metadata.ignorePropagatedAnnotations: true
define view entity ZBS_I_DmoInvoiceWithType
  as select from zbs_dmo_invoice
{
  key document                                                as Document,
      doc_date                                                as DocumentDate,
      doc_time                                                as DocumentTime,
      partner                                                 as PartnerNumber,
      cast( 'C' as ZBS_DemoCDSInvoiceStatus preserving type ) as StatusSimpleType,
      ZBS_DemoCDSInvoiceStatusEnum.#Payed                     as StatusEnumType
}

 

In the Core Data Service we have

  • CAST - We create the "StatusSimpleType" column by casting our element to our new target type.
  • Enum value - For the "StatusEnumType" column we use an enum directly. To do this we specify the type and access the various values using a dot (.). Each enum begins with a hashtag (#).

 

If we then look at the data, we get the most meaningful result back via the EnumType. The two new columns are now available and visible in the table. Since we are only assigning constants here, this is just an example.

 

Comparison

In this section we want to compare the new types with the classic DDIC. We have prepared two comparisons for this. In the first case, we compare the different texts from the data element. You can define the different texts in the type.

 

In the second step, we compare the type and the values with the domain. The data type comes from the base type and the fixed values can be compared with the enumeration. In contrast to the fixed values, the values of an enum are checked rigorously and no other values can be entered.

 

Hint: You can also find more information about the texts in the SAP Community Blog on the topic.

 

Resources

Are you looking for the resources of this blog? We have created a GitHub repository for the Core Data Service series, you can find all of today's sources in the following commit.

 

Conclusion

You can already use the new types in development today, but not yet up to the database level. When the first table entities appear in 2025, you will learn to love the new types.

 

Source:
SAP Community - Simple Types


Included topics:
CDSCore Data ServiceTypeENUM
Comments (0)



And further ...

Are you satisfied with the content of the article? We post new content in the ABAP area every Friday and irregularly in all other areas. Take a look at our tools and apps, we provide them free of charge.


ABAP in Practice - String Processing

Category - ABAP

In this practical example we look at the string processing to determine the CDS names in CamelCase and how you can implement this with ABAP.

10/15/2024

ABAP - CDS Extraktor

Category - ABAP

How does the CDS extractor work in ABAP and what challenges are there when developing with it? In this article we will look at a few details.

09/20/2024

ABAP Tools - Working with Eclipse (CDS Templates)

Category - ABAP

Did you select the wrong CDS template when creating the view? Here's a little tip to correct the view in ABAP afterwards.

07/02/2024

ABAP Tools - Work with Eclipse (CDS Analysis)

Category - ABAP

In the complex world of CDS views, it is important to have the right tool at hand to ensure an analysis of the structures and data sources.

08/25/2023

CDS - View Entity

Category - ABAP

In this article you will learn more about the new Core Data Service entities, what they bring and what makes them different from the old views.

07/29/2022