Portable and dynamic selection-screen texts in ABAP programs

Copying and pasting ABAP report programs from one system to another normally leaves one obvious casualty: selection screen texts. Here I show you how to include them in the source of a program to make them portable.

Of course, if you are really serious about sharing program components with colleagues or friends, I would recommend the fantastic abapGit by Lars Hvam. Previously we also used to have SAPLink but I suspect abapGit has now supplanted that. So consider what I am showing you here more of an interesting curiosity, though perhaps it can come in handy one day.

sel screen texts

The other possible use for this is to change selection screen texts dynamically.

Let’s take a very¬†banale example:

data: lv_curr type waers.
parameters: p_sdate type d.
select-options: s_curr for lv_curr.

When you create a report program in ABAP, the system automatically creates additional fields for each element you define on the selection screen. You don’t see these, but you would notice them if you are looping at SCREEN during SELECTION-SCREEN OUTPUT:

debug-sel-screen

The field that SCREEN-NAME refers to here, “%_p_sdate_%_app_%”, is the field generated for the label of the defined parameter. It has a single field, “TEXT”, which you can set in your program.

For the selection-screen defined above, we can set the values dynamically during the INITIALIZATION event:

initialization.
  %_p_sdate_%_app_%-text = 'Switch Date'.
  %_s_curr_%_app_%-text = 'Currency'.

When we run our program now, the selection-screen shows these texts.

sel-screen-with-texts

You can now copy and paste the source code between systems and still preserve your selection screen texts without worrying about copying the selection texts of the program. (Again, use abapGit if you want to do it properly). I just find this useful for sharing code on GitHub Gists, for example.

Moreover, you can internationalize the texts if you choose:

initialization.
  case sy-langu.
    when 'D'. " -> ISO DE
      %_p_sdate_%_app_%-text = 'Wechsel Datum'.
      %_s_curr_%_app_%-text = 'Währung'.
    when others. "Fallback, English
      %_p_sdate_%_app_%-text = 'Switch Date'.
      %_s_curr_%_app_%-text = 'Currency'.
  endcase.

The same can be done for the texts of frames. Here you will notice, however, that instead of a structure, it’s just a simple field:

initialization. 
  %b001000_block_1000 = 'General Selections'.

sel-screen-frame-text

You will probably see the pattern that is applied to the generated field names, but it might be safer to just determine them from looping at SCREEN in debugging.

All of this comes, of course with a caveat: If you already have texts defined in the program, these will overwrite them. But in that case, you can just delete the code that modifies these texts.

The other use for this is to make the texts dynamic. Let’s say we wanted to change the value of a label depending on some other field value. We would do this during the SELECTION-SCREEN OUTPUT event:

at selection-screen output.
  if p_sdate > sy-datum.
    %_s_curr_%_app_%-text = 'Future Currency'.
  endif.

sel-screen-dynamic-text

  • Mathweus Mpati

    Great work! Thanks for sharing