ABAP Obsolete - Create GUID
How do you create a unique identification (ID) in the system? You probably know the answer, but is it still up to date?
In most help sites on the Internet or in forums, you will always stumble upon the use of a function module. If you've been following our series on New ABAP, you already know that the use of function module should now be reduced to a minimum. Not because function modules are obsolete, but rather should be used for other purposes.
What is it?
GUID means "globally unique identifiers", so it is a unique key in the system that can not happen twice. This key is generated from various information from the system, the time and random numbers, comparable to a hash in other languages/systems.
A number can be generated in three output formats:
- 16-digit type x (42D85ED56C0A1EE9A5CA741861FC924B)
- 32-digit type c (42D85ED56C0A1EE9A5CA741861FC924B)
- 22-digit type c (GjXUrMmA7kcbodGOOVoIIm)
The first two formats are the same in the output, but differ by their type in which they are stored.
When you call the function module GUID_CREATE, you get all three GUIDs returned with one call. Via the Write output, you can view the values directly.
DATA: ld_guid_16 TYPE guid_16, ld_guid_22 TYPE guid_22, ld_guid_32 TYPE guid_32. CALL FUNCTION 'GUID_CREATE' IMPORTING ev_guid_16 = ld_guid_16 ev_guid_22 = ld_guid_22 ev_guid_32 = ld_guid_32. WRITE: / ld_guid_16, ', ', ld_guid_22, ', ', ld_guid_32
The use of the function module has already been marked as obsolete, and the comments contain the replacement class for generating the IDs.
The replacement to the function module is the class CL_SYSTEM_UUID, with which you can also generate all IDs. SAP provides a separate method for each data type at this point. Of course, the advantage of using it, is the inline declaration in the program, which makes the code clearer and cleaner.
DATA(ld_new_16) = cl_system_uuid=>create_uuid_x16_static( ). DATA(ld_new_22) = cl_system_uuid=>create_uuid_c22_static( ). DATA(ld_new_32) = cl_system_uuid=>create_uuid_c32_static( ). WRITE: / ld_new_16, ', ', ld_new_22, ', ', ld_new_32.
In addition to the use of static methods, instance methods are also provided that can be called after the object has been created. For these methods, no interface aliases are provided in the class, which slightly lengthens the spelling.
DATA(ld_new_16x) = NEW cl_system_uuid( )->if_system_uuid~create_uuid_x16( ). DATA(ld_new_22x) = NEW cl_system_uuid( )->if_system_uuid~create_uuid_c22( ). DATA(ld_new_32x) = NEW cl_system_uuid( )->if_system_uuid~create_uuid_c32( ). WRITE: / ld_new_16x, ', ', ld_new_22x, ', ', ld_new_32x.
The creation of unique IDs has changed a bit, but should not be a problem for you. With the methods you are definitely a bit more flexible, especially when it comes to the chaining of functions. If this is not new to you, then just to refresh your knowledge.