ABAP - XCO UUID
How do you generate unique IDs in ABAP using the XCO classes? In this article, you will learn how quickly and easily this can be done.
Table of contents
The XCO classes are auxiliary classes that provide various everyday functions bundled under a public API. You can find more information and an overview of the XCO libraries on the overview page.
Introduction
The generation of unique IDs is done less and less via number range objects in the Fiori and RAP world. The data records still need a key, but this does not have to be descriptive, as it is hidden in the UI in many cases. The user should know, for example, how the customer "xyz" means, but it is no longer so important that it has the customer number "0815". In such cases, the RAP Framework already generates keys automatically. But what if we have to generate the keys ourselves?
Generation
In this section we look at the generation of the various UUIDs via the class XCO_CP=>UUID and how it works for all types.
Type X16
The standard type for UUIDs in the ABAP RESTful Programming Model is now SYSUUID_X16. This is a 16-digit binary.
" Classic
CALL FUNCTION 'GUID_CREATE'
IMPORTING
ev_guid_16 = uuid.
" Modern
TRY.
uuid = cl_system_uuid=>create_uuid_x16_static( ).
CATCH cx_uuid_error.
ENDTRY.
" XCO
uuid = xco_cp=>uuid( )->value.
In classic development, the GUID or UUID is generated using a function module. If you want to do the whole thing with a class, you use the CL_SYSTEM_UUID class, but you have to handle exception handling or live with the warning in the ABAP Development Tools, although that would be the dirty part. In the XCO library, you can find the object for the UUID in the XCO_CP class and you can access VALUE directly to get a current ID.
Type C22
If you look at the above class, you will notice that only X16 UUIDs are supported and the character-based UUIDs are missing. For this purpose, the standard implements a formatter to generate the other formats.
" Classic
CALL FUNCTION 'GUID_CREATE'
IMPORTING
ev_guid_22 = char_uuid.
" Modern
TRY.
char_uuid = cl_system_uuid=>create_uuid_c22_static( ).
CATCH cx_uuid_error.
ENDTRY.
" XCO
char_uuid = xco_cp=>uuid( )->as( xco_cp_uuid=>format->c22 )->value.
In the example above, we call the AS method and pass the corresponding formatter for the desired format. As a result, we receive a String object, where we can access the corresponding content via VALUE.
Formatter
The following formatters are currently offered for the UUID. There are the classic Char 22 and 32 IDs, but also a more readable form as Char 36.
DATA(uuid_c22) = xco_cp=>uuid( )->as( xco_cp_uuid=>format->c22 )->value.
DATA(uuid_c32) = xco_cp=>uuid( )->as( xco_cp_uuid=>format->c32 )->value.
DATA(uuid_c36) = xco_cp=>uuid( )->as( xco_cp_uuid=>format->c36 )->value.
If we execute the code above, we get the following output and the corresponding format.
Converting
If we know the external format, we can also read the ID again to create a UUID object in the system. However, we must know the format for this. In this example, we create a UUID object from the Char 36 ID using the TO_UUID method.
DATA(uuid) = xco_cp_uuid=>format->c36->to_uuid( `BAF0A1E7-5FB0-1EDF-B5E8-89F53894CA3A` ).
In the debugger we can now look at the current value in the object. This makes it possible to convert the external ID back into an internal object in order to process the ID or put it into a new format.
Complete example
Here you can find the complete example of the executable class from above with the corresponding implementations and data types. In the GitHub repository you can find all examples of the XCO classes and in the Commit you can find the changes to this article.
CLASS zcl_bs_demo_xco_uuid DEFINITION
PUBLIC FINAL
CREATE PUBLIC.
PUBLIC SECTION.
INTERFACES if_oo_adt_classrun.
PRIVATE SECTION.
METHODS operation_for_uuid_x16
IMPORTING !out TYPE REF TO if_oo_adt_classrun_out.
METHODS operation_for_uuid_c22
IMPORTING !out TYPE REF TO if_oo_adt_classrun_out.
METHODS operation_for_uuid_formatter
IMPORTING !out TYPE REF TO if_oo_adt_classrun_out.
METHODS operation_convert_uuid
IMPORTING !out TYPE REF TO if_oo_adt_classrun_out.
ENDCLASS.
CLASS zcl_bs_demo_xco_uuid IMPLEMENTATION.
METHOD if_oo_adt_classrun~main.
operation_for_uuid_x16( out ).
operation_for_uuid_c22( out ).
operation_for_uuid_formatter( out ).
operation_convert_uuid( out ).
ENDMETHOD.
METHOD operation_for_uuid_x16.
DATA uuid TYPE sysuuid_x16.
" Classic
CALL FUNCTION 'GUID_CREATE'
IMPORTING
ev_guid_16 = uuid.
" Modern
TRY.
uuid = cl_system_uuid=>create_uuid_x16_static( ).
CATCH cx_uuid_error.
ENDTRY.
" XCO
uuid = xco_cp=>uuid( )->value.
ENDMETHOD.
METHOD operation_for_uuid_c22.
DATA char_uuid TYPE c LENGTH 22.
" Classic
CALL FUNCTION 'GUID_CREATE'
IMPORTING
ev_guid_22 = char_uuid.
" Modern
TRY.
char_uuid = cl_system_uuid=>create_uuid_c22_static( ).
CATCH cx_uuid_error.
ENDTRY.
" XCO
char_uuid = xco_cp=>uuid( )->as( xco_cp_uuid=>format->c22 )->value.
ENDMETHOD.
METHOD operation_for_uuid_formatter.
DATA(uuid_c22) = xco_cp=>uuid( )->as( xco_cp_uuid=>format->c22 )->value.
out->write( uuid_c22 ).
DATA(uuid_c32) = xco_cp=>uuid( )->as( xco_cp_uuid=>format->c32 )->value.
out->write( uuid_c32 ).
DATA(uuid_c36) = xco_cp=>uuid( )->as( xco_cp_uuid=>format->c36 )->value.
out->write( uuid_c36 ).
ENDMETHOD.
METHOD operation_convert_uuid.
DATA(uuid) = xco_cp_uuid=>format->c36->to_uuid( `BAF0A1E7-5FB0-1EDF-B5E8-89F53894CA3A` ).
out->write( uuid->value ).
ENDMETHOD.
ENDCLASS.
Conclusion
You can also find the topic of UUID in the XCO area and can easily access new IDs. You are currently spared the need for exception handling, as no exceptions are passed on to the outside and lead to processing being aborted in an emergency.