HTTP status codes in ABAP

I’ve been working a lot with OData and REST lately, outbound as well as inbound. Some of my code needs to handle HTTP status codes in different ways. Since I didn’t want to hard-code the status codes in my code, I started looking around for standard classes and interfaces which could be of use to me. This blog post is a brief summary of what I found.

Interface IF_HTTP_STATUS

The interface IF_HTTP_STATUS contains a list of status texts in the form of constants of data type STRING. Some examples are:

CONSTANTS reason_200 TYPE string VALUE 'OK' . "#EC NOTEXT
CONSTANTS reason_201 TYPE string VALUE 'Created' . "#EC NOTEXT

This interface is quite handy if you want to present the user with not only a status code but also the corresponding text.

Class CL_REST_STATUS_CODE

The classes CL_REST_STATUS_CODE and /IWCOR/CL_REST_STATUS_CODE also contain constants. The two classes are identical in regard to the constants but have some minor differences in one of the methods.

The constants in these two classes contain the numerical HTTP status codes of data type I. Some examples are:

constants GC_SUCCESS_OK type I value 200 . "#EC NOTEXT
constants GC_SUCCESS_CREATED type I value 201 . "#EC NOTEXT

The classes have some useful static methods as well:

  • Method GET_REASON_PHRASE: Takes in a status code and returns the corresponding status text. The method actually uses interface IF_HTTP_STATUS to accomplish this.
  • Method IS_CLIENT_ERROR: Takes in a status code and returns abap_true if the status code is in the range 400 – 499. Otherwise, the method returns abap_false.
  • Method IS_REDIRECTION: Takes in a status code and returns abap_true if the status code is in the range 300 – 399. Otherwise, the method returns abap_false.
  • Method IS_SUCCESS: Takes in a status code and returns abap_true if the status code is in the range 200 – 299. Otherwise, the method returns abap_false.
  • Method IS_SERVER_ERROR: Takes in a status code and returns abap_true if the status code is in the range 500 – 599. Otherwise, the method returns abap_false.
  • Method IS_ERROR: If one of the methods IS_CLIENT_ERROR and IS_SERVER_ERROR return abap_true, this method also returns abap_true. Otherwise, the method returns abap_false.

Limitations

The interface and the two classes mentioned in this blog post don’t contain constants for every HTTP status code mentioned in the Wikipedia article. However the most common ones are available, and for the use cases I’ve experienced so far the interface and the classes have been sufficient.

Happy coding!

The progress indicator in ABAP reports

I’m sure most of you have been asked to develop a report displaying a progress indicator during the execution of the report. I just developed a migration report which needs several minutes to execute, so I decided to put in a progress indicator.

When running the ATC check with abapOpenChecks for the report I’ve developed, I got an error telling me that function module SAPGUI_PROGRESS_INDICATOR should not be used. Even though this is documented in the documentation of check 53 for abapOpenChecks, an SAP Knowledge Base Search for information on what should be used instead of function module SAPGUI_PROGRESS_INDICATOR didn’t give me any results. The documentation at abapOpenChecks states that class CL_PROGRESS_INDICATOR should be used instead of the function module. I decided to write this short blog post to share this information with the community as well as to provide a tiny code example.

Instead of using the function module like this:

CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'
  EXPORTING
    percentage = progress_percent
    text       = progress_text.

You can use the class like this:

cl_progress_indicator=>progress_indicate(
    i_text = |Processing: { current_record }/{ total_records }|
    i_output_immediately = abap_true ).

The result isn’t exactly the same since the function module only works with percentages. This could easily be achieved with the class as well, but in this case, I was more interested in seeing how many of the total records had been processed.

Happy coding!

My two first conference talks

I held my two first conference talks earlier this week at the Swedish SAP user group conference SAPSA IMPULS 2019, and in this post I will share some of my experiences. SAPSA IMPULS is a yearly conference targeting people working with SAP solutions in the Nordics, and the event had more than 800 attendees this year. This was a two days event, and I was scheduled to hold two different presentations, one on each day of the conference.

Day 1 – a broader appeal

On the first day I held a presentation titled “How to stay up-to-date within the SAP space with openSAP“. openSAP is an excellent MOOC platform to stay current with the SAP space and acquire new skills. I explained the structure and advantages of using MOOCs in general and openSAP in particular, to expand my knowledge within SAP. I also shared my personal experiences attending about 30 openSAP courses.

I was quite nervous before the talk, since I haven’t really done any public speaking. After having rigged up my computer, there were still about ten minutes to wait for the planned starting time of my talk. I found these ten minutes the most nervous part of the talk, since I was just standing around waiting. Thankfully a former colleague of mine came into the room about five minutes before my talk was to start, and we engaged in some small-talk which helped me relax. Once I got started presenting, my nerves calmed down. There were seven parallel tracks as I was presenting, and my presentation attracted about 70 people. This meant that the room was full, but not crowded. After the presentation, several attendees came forward to discuss some questions they had and they also gave me some positive feedback. I think that the talk was generally well-received.

Before holding my presentation, I had the opportunity to attend the key-note. This enabled me to tie the topic of the key-note back into my own presentation. The key-note was mainly about customer experiences and experience management. Since there is currently a course on the topic on openSAP, The Power of Experience Management, I referenced this in my presentation. Even though that this was just a small detail, I still think that it was a nice bridge between the key-note and my presentation.

I was also able to attend a few other sessions before I held my presentation. This made me realize that I had completely overlooked the fact that I should have included a slide presenting the organization I work for in my presentation. I was able to correct this mistake in the break before my presentation.

Day 2 – the technical niche talk

On the second day, I held a talk titled “Improving your ABAP code quality with open-source tools“. The talk was about how we have improved our code quality through the use of open-source tools at the organization where I work. The tools I explained and demonstrated are:

Since the session was only 20 minutes long, and I had a lot of material to cover, the talk felt a bit rushed. I feel very comfortable with the topic I presented, which probably made me talk a bit more than during my rehearsals of the talk. I felt much more relaxed than on day 1, most likely since the first talk was well received and I had already received positive feedback.

In retrospect, the talk might have been a little too technical for the audience of this conference. However, I still had about 20-25 people showing up. I was approached after this talk with some interesting comments and questions, so even though it was more of a niche talk, I’m happy that I gave it. I also learned a lot as I was preparing for the talk.

Key takeaways

I’m happy to have participated in the conference and to have shared my experiences and knowledge through the two talks. It was a learning experience both when it comes to the subjects I presented as well as regarding the whole process of writing presentation proposals, preparing and finally delivering a talk.

Some of my takeaways are:

  • Start preparing the talks early. I fine-tuned my talks over a period of about two months, and still ended up doing last-minute changes.
  • Ask someone to review your presentation. I asked my manager as well as my colleagues to review the presentations, and this gave me some valuable feedback.
  • Rehears, rehears, rehears. I practiced my talks alone as well as in front of my wife and kids, and it was important for discovering transitions in the presentations which weren’t smooth enough as well as practicing my presentation skills.
  • Try to engage in small-talk and use the opportunity to network ahead of a presentation. This helped me shift into a more talkative mode and made me more relaxed.
  • People want to hear your story. Both of my presentations contained personal stories about why the topics were important to me, and how they helped me to be successful in real-world situations. This made the presentations as well as me as a presenter easier to relate to.
  • Attend the key-note, so that you can relate to it in your own presentation if relevant.
  • Attend other sessions to see how the other presenters are presenting. This helped me fine-tune my presentation.

If you are considering submitting an abstract to a conference, just do it! It’s a fun learning experience!

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.

CALL FUNCTION 'SYSTEM_CALLSTACK'
  EXPORTING
    max_level = 1
  IMPORTING
    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!

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
   ENDMETHOD.

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

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:

DATA ##NEEDED:
   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 = 'mail@test.com'.

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

Open-source project: SAP/styleguides – Clean ABAP

A highly valuable resource for the ABAP developer is the clean ABAP guide available on GitHub.com under SAP/styleguides. The guide helps the ABAP developer in writing clean code.

What is clean code?

The bible of the clean coding movement is the book Clean Code: A Handbook of Agile Software Craftsmanship by Robert C. Martin. The opening quote of the book states:

Writing clean code is what you must do in order to call yourself a professional. There is no reasonable excuse for doing anything less than your best.

Robert C. Martin

In the foreword of the book, the following summary of clean code is given:

… Yet attentiveness to detail is an even more critical foundation of professionalism than is any grand vision. First, it is through practice in the small that professionals gain proficiency and trust for practice in the large. Second, the smallest bit of sloppy construction, of the door that does not close tightly or the slightly crooked tile on the floor, or even the messy desk, completely dispels the charm of the larger whole. That is what clean code is about.

James O. Coplien

As you understand from the quotes above, we need to focus on the details in order to write clean, professional code.

Clean ABAP

There are numerous open-source projects and style guides available for writing clean code in different programming languages. The open-source project SAP/styleguides concerns itself with ABAP, the high-level programming language created by the software company SAP.

The Clean ABAP guide gives the developer recommendations on a number of basic topics, such as naming, constants, tables, strings, booleans, and conditions. The guide also digs deeper into more advanced topics, such as OOP best practices, error handling, and testing.

The guide is written in a dense way, without lengthy discussions. It is very much to the point and provides a number of useful examples on patterns and anti-patterns. For quick reference, there are also cheat sheets available.

The project license is CC BY 3.0. For a human-readable summary of the license, see the summary by Creative Commons.

Conclusion

Writing clean code is an essential characteristic of a professional ABAP developer. There are many useful patterns and recommendations presented in the SAP/styleguides project. I think that every ABAP developer should be familiar with the guide and its concepts.

For the developer who has worked for several years with ABAP, I particularly recommend the sub-section Modern ABAP Language Elements which gives a neat summary of newer ABAP syntax.

Start reading about and applying clean code concepts today! I promise that the quality of your code will improve a lot.

Disclosures

I’m an active contributor to SAP/styleguides.

The link to the book Clean Code: A Handbook of Agile Software Craftsmanship is an affiliate link. As an Amazon Associate I earn from qualifying purchases.