Determine the current class and method names in ABAP

In some situations, it can be useful to be able to determine the names of the current class and method dynamically instead of using hard-coded strings. I find this particularly useful for logging purposes. I have a helper class which writes to the system log when certain exceptions are triggered. As input to this helper class (or rather the method writing to the system log) I want to provide the names of the class and method where the exception was raised.

Determining the name of the current class

SAP provides the class CL_ABAP_CLASSDESCR for determining some class attributes dynamically. To determine the name of the current class, the following code snippet can be used:

DATA(lv_class_name) = cl_abap_classdescr=>get_class_name( me ).

LV_CLASS_NAME will contain the class name in the following format: \CLASS=ZCL_MY_CLASS

Determining the name of the current method

The only approach I’ve found for determining the name of the current method is by using a function module to read the call stack. If you are aware of a better way of doing this, please leave a comment! The call stack approach looks like this:

DATA lt_callstack TYPE abap_callstack.

    max_level = 1
    callstack = lt_callstack.

DATA(lv_method_name) = lt_callstack[ 1 ]-blockname.

LV_METHOD_NAME will contain the method name in the following format: MY_METHOD

Happy coding!

OData URL encoding of single quote ‘ (%27) in a search string

When developing an OData service for searching for contact persons by name, I came across a scenario where the name contains the special character ‘ (single quote character). As an example, the user would like to search for contact persons with the last name O’Reilly.

The URL encoding of the single quote character is %27. First I just tried the following:

This lead to an error with HTTP status code 400 and the message “Invalid token detected at position &” with exception /IWCOR/CX_DS_EXPR_SYNTAX_ERROR.

As you might already have guessed, the issue is that %27 is used to enclose the search string. When adding a single %27 within the search string, the parser of the $filter expression gets confused.

The solution is to escape the single quote by adding a single quote before it:

What are pragmas and pseudo comments in ABAP?

When using the ABAP Test Cockpit (ATC) for statically and dynamically checking the quality of your ABAP code and related repository objects, some of the warnings and errors identified by the ATC aren’t relevant in your specific scenario. Leaving errors and warnings unaddressed might cause confusion at some future point-in-time when you or another developer needs to change the code. In order to reduce the confusion, ABAP pragmas and pseudo comments can be used to blend out irrelevant warnings and errors. Let us take a detailed look at what pseudo comments and pragmas are and how to use them.

Pseudo Comments

Pseudo comments are program directives that influence check and test procedures. Pseudo comments have mostly become obsolete and have been replaced by pragmas or real additions.

ABAP Keyword Documentation

Even though mostly obsolete, the pseudo comments are still relevant for some scenarios in the code inspector.

A pseudo comment looks like this:

METHOD get_sources.
     SELECT source, textlong
       INTO TABLE @rt_source
       FROM tb006
       WHERE spras = @sy-langu. "#EC CI_SUBRC

The pseudo comment "#EC CI_SUBRC is used to hide a message telling us that sy-subrc should be checked after a SELECT statement. In the example above the desired behavior is that an empty table should be returned if the SELECT statement is not successful, so there is no need to check the value of sy-subrc.

Sometimes a line of code generates several warnings or errors which you may want to hide. Using pseudo comments, it is only possible to specify one pseudo comment for each program line. When multiple pseudo comments are required, the statement must be divided up to span multiple lines.

Note that the pseudo comment is placed after the ending dot of a statement.


Pragmas are program directives that can be used to hide warnings from various check tools.

ABAP Keyword Documentation

Pragmas can be used to hide warnings from the ABAP compiler syntax check as well as from the extended program check.

The use of a pragma looks like this:

MESSAGE e001(ad) INTO DATA(lv_message) ##NEEDED.

The pragma ##NEEDED tells the check tools that even though the variable lv_message isn’t used for further processing, the variable is still needed. In this specific scenario, it is needed since we want to be able to perform a where-used search for the message AD 001 from transaction SE91.

Not that a pragma is placed before the dot or comma ending the statement to which the pragma is applied. If multiple pragmas are needed for the same statement, they can be placed one after the other separated by space. It is also possible to place pragmas in front of a colon (:) of a chained statement. This applies the pragma to the whole chained statement:

   gt_messages TYPE bapiret2_t,
   gt_sel_data TYPE ty_sel_data_tt.

Personally I think that this is hard to read, and I’d much rather use the following format:

DATA: gt_messages TYPE bapiret2_t ##NEEDED,
      gt_sel_data TYPE ty_sel_data_tt ##NEEDED.

Some pragmas can be used with parameters. The example mentioned in the ABAP Keyword Documentation is ##SHADOW, which can be used with an optional parameter. It looks like this: ##SHADOW[LOG]

I’ve never come across this in practice, so I don’t have much experience to share. Feel free to give some input on this by writing a comment on the blog post.

The mapping between obsolete pseudo comments and pragmas

As already mentioned, most pseudo comments are obsolete and have been replaced by pragmas. SAP provides a program which can be run to find the mapping between obsolete pseudo comments and pragmas. The name of the program is ABAP_SLIN_PRAGMAS. The main table used by the program is SLIN_DESC, if you prefer looking up the pseudo comments and programs directly in a table viewer like SE16.

As an example, the pseudo comment "#EC AUTFLD_MIS has been superseded by the pragma ##AUTH_FLD_MISSING.

The tables TRPRAGMA and TRPRAGMAT contain all pragmas.

I hope that this brief overview of pragmas and pseudo comments was useful. Please leave a comment if you think that I missed mentioning something crucial. Happy coding!

What is the difference between ` and ‘ in ABAP?

When defining a variable containing a text of some sort in ABAP, which of the following options do you typically use?

" Option 1
DATA(my_text) = `my text`.

 " Option 2
DATA(my_text) = 'my text'. 

Are you aware of the differences between these two lines of code? Let us look into the details.

Character literals

What we are looking at are two different types of character literals. The ABAP Keyword Documentation provides us with the following explanation:

“Character literals can be either text field literals or text string literals. A text field literal is a character string enclosed in single quotation marks ('); a text string literal is a character string enclosed in single backquotes (`).”

Revisiting our example, we can define the variables like this:

" Option 1
DATA(my_text_string_literal) = `my text`.

" Option 2
DATA(my_text_field_literal) = 'my text'.

my_text_string_literal has the actual type STRING and the technical type CString of length 7. my_text_field_literal has a generated actual type (e.g. %_T00006S00000000O0000000298) and the technical type C of length 7.

The text field literal can have a length between 1 and 255 characters, whereas a text string literal can have a length between 0 and 255 characters.

That the text field literal has a minimum length of 1 means that '' (no space) has the same meaning as ' ' (a space character).

When to use which character literal?

The SAP Styleguide for clean ABAP contains the following recommendation:

“Refrain from using ', as it adds a superfluous type conversion and confuses the reader whether he’s dealing with a CHAR or STRING”

The following is an example of a superfluous type conversion between CHAR (from the text field literal) and a STRING:

DATA example_string TYPE string.
example_string = 'QWERTY'.

Prefer the following assignment:

DATA example_string TYPE string.
example_string = `QWERTY`.

If however the data element of the variable has been defined as a CHAR, the text field literal should be used to avoid a type conversion:

DATA e_mail_address TYPE ad_smtpadr. " CHAR 241
e_mail_address = ''.

I hope that this short post helped clarify the difference between ' and `. Happy coding!