Get system context information using CL_ABAP_CONTEXT_INFO

 As part of the Useful Libraries series,

I am talking about a new class which allows accessing system runtime information like time, date,user, and language.

As an ABAP developer, you often need to determine the user, date or time to fulfil certain business or technical requirements. For which we rely on SY/SYST structures.

In its push towards cloud adaptation and decreasing reliance on a run-time-filled global structure( SY/SYST), sap introduced the CL_ABAP_CONTEXT_INFO class to achieve the same.

Here is a ready-to-run console output class showing how to use it, just copy and paste the class and hit F9.

class zcl_context_info_demo definition
public final
create public.
public section.
interfaces if_oo_adt_classrun.
private section.
methods get_current_system_time returning value(time) type cl_abap_context_info=>ty_system_time.
methods get_current_system_date returning value(date) type cl_abap_context_info=>ty_system_date.
methods get_current_system_username returning value(user) type cl_abap_context_info=>ty_user_name.
methods get_user_description returning value(desc) type string.
methods get_current_system_language returning value(langu) type cl_abap_context_info=>ty_language_key.
endclass.
class zcl_context_info_demo implementation.
method if_oo_adt_classrun~main.
out->write( | Current system time is: { get_current_system_time( ) } | ).
out->write( | Current system date is: { get_current_system_date( ) } | ).
out->write( | Current system user is: { get_current_system_username( ) } | ).
out->write( | Current system user description is: { get_user_description( ) } | ).
out->write( | Current system language is: { get_current_system_language( ) } | ).
endmethod.
method get_current_system_date.
date = cl_abap_context_info=>get_system_date( ).
endmethod.
method get_current_system_language.
try.
langu = cl_abap_context_info=>get_user_language_abap_format( ).
catch cx_abap_context_info_error.
" handle exception
endtry.
endmethod.
method get_current_system_time.
time = cl_abap_context_info=>get_system_time( ).
endmethod.
method get_current_system_username.
user = cl_abap_context_info=>get_user_technical_name( ).
endmethod.
method get_user_description.
try.
desc = cl_abap_context_info=>get_user_description( ).
catch cx_abap_context_info_error.
" handle exception
endtry.
endmethod.
endclass.

As always, Hope it helps you in your day-to-day work.


Popular posts from this blog

ABAP convert internal table to excel (.xlsx) format and Send email or download

ABAP read excel(.XLSX) file to internal table in ABAP using CL_FDT_XL_SPREADSHEET

Use CL_BCS library to send email with ABAP