Skip to Content

Hi folks,

It’s quite a time that I have published my last ABAP Geek but yesterday I stumbled across a feature that I don’t want to hold back.

You might be well aware of the so called “Dynamic Selections” that are offered at the selection screens of good old logical databases. They allow you to define selections for nodes of the logical database that are not offered statically. But did you know how easy it is to utilize these dynamic selections within any program? Well, I didn’t.

All you have to do is to call the two function modules FREE_SELECTIONS_INIT and FREE_SELECTIONS_DIALOG. The following example source code is a kind of fully fledged data browser for arbitrary database tables that allows you to freely define the selection conditions:

REPORT demo_free_selections.

PARAMETERS dbtab TYPE tabname DEFAULT 'SPFLI'.

CLASS demo DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS main.
  PRIVATE SECTION.
    CLASS-METHODS check_existence_and_authority
      RETURNING VALUE(checked_dbtab) TYPE tabname.
ENDCLASS.

CLASS demo IMPLEMENTATION.
  METHOD main.
    DATA selid         TYPE  rsdynsel-selid.
    DATA field_tab     TYPE TABLE OF rsdsfields.
    DATA table_tab     TYPE TABLE OF rsdstabs.
    DATA cond_tab      TYPE rsds_twhere.
    DATA dref          TYPE REF TO data.

    FIELD-SYMBOLS <table> TYPE STANDARD TABLE.
    FIELD-SYMBOLS <cond>  LIKE LINE OF cond_tab.

    DATA(checked_dbtab) = demo=>check_existence_and_authority( ).

    table_tab = VALUE #( ( prim_tab = checked_dbtab ) ).
    CALL FUNCTION 'FREE_SELECTIONS_INIT'
      EXPORTING
        kind         = 'T'
      IMPORTING
        selection_id = selid
      TABLES
        tables_tab   = table_tab
      EXCEPTIONS
        OTHERS       = 4.
    IF sy-subrc <> 0.
      MESSAGE 'Error in initialization' TYPE 'I' DISPLAY LIKE 'E'.
      LEAVE PROGRAM.
    ENDIF.

    CALL FUNCTION 'FREE_SELECTIONS_DIALOG'
      EXPORTING
        selection_id  = selid
        title         = 'Free Selection'
        as_window     = ' '
      IMPORTING
        where_clauses = cond_tab
      TABLES
        fields_tab    = field_tab
      EXCEPTIONS
        OTHERS        = 4.
    IF sy-subrc <> 0.
      MESSAGE 'No free selection created' TYPE 'I'.
      LEAVE PROGRAM.
    ENDIF.

    ASSIGN cond_tab[ tablename = checked_dbtab ] TO <cond>.
    IF sy-subrc <> 0.
      MESSAGE 'Error in condition' TYPE 'I' DISPLAY LIKE 'E'.
      LEAVE PROGRAM.
    ENDIF.

    CREATE DATA dref TYPE TABLE OF (checked_dbtab).
    ASSIGN dref->* TO <table>.

    TRY.
        SELECT *
               FROM (checked_dbtab)
               WHERE (<cond>-where_tab)
               INTO TABLE @<table>.
      CATCH cx_sy_dynamic_osql_error.
        MESSAGE 'Error in dynamic Open SQL' TYPE 'I' DISPLAY LIKE 'E'.
        LEAVE PROGRAM.
    ENDTRY.

    TRY.
        cl_salv_table=>factory(
          IMPORTING r_salv_table = DATA(alv)
          CHANGING  t_table      = <table> ).
        alv->display( ).
      CATCH cx_salv_msg.
        MESSAGE 'Error in ALV display' TYPE 'I' DISPLAY LIKE 'E'.
    ENDTRY.
  ENDMETHOD.
  METHOD check_existence_and_authority.
    TRY.
        checked_dbtab = cl_abap_dyn_prg=>check_table_name_str(
                        val = dbtab
                        packages = 'SAPBC_DATAMODEL' ).
      CATCH cx_abap_not_a_table.
        MESSAGE 'Database table not found' TYPE 'I' DISPLAY LIKE 'E'.
        LEAVE PROGRAM.
      CATCH cx_abap_not_in_package.
        MESSAGE 'Only tables from the flight data model are allowed'
                 TYPE 'I' DISPLAY LIKE 'E'.
        LEAVE PROGRAM.
    ENDTRY.
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  demo=>main( ).

Passing kind “T” to function module FREE_SELECTIONS_INIT prepares a dynamic selection for a database table entered via a parameter. Passing the result of FREE_SELECTIONS_INIT to FREE_SELECTIONS_DIALOG calls a special selection screen, where you can select the database fields and the respective selections.

After selecting Save, you receive a ready to use dynamic WHERE-condition via parameter WHERE_CLAUSES (another parameter FIELD_RANGES would return a respective RANGES table).

The rest is history: a SELECT statement with dynamic tokens, a target table that is created dynamiclally, and some ALV display.

Cheers!

 

Horst

 

PS: Of course, this program is part of the ABAP Example Library now.

 

 

 

 

 

 

 

To report this post you need to login first.

19 Comments

You must be Logged on to comment or reply to a post.

  1. Thomas Zloch
    This can come in handy on many occasions. Many thanks for sharing this, Horst.

    FYI, I had to add “TYPE-POOLS: rsds.” to make it compile (Rel. 700).

    Cheers
    Thomas

    (0) 
    1. Horst Keller Post author
      Oh yeah, TYPE-POOLS is snows of yesteryear for me but you need it of course before Releases 7.02/7.2. I added it to the source code. Thanks for the hint.
      (0) 
  2. Sandra Rossi
    There are also very interesting function modules FREE_SELECTIONS_?_2_? to convert from one type of condition to another one, where ? can be RANGE, WHERE and EX (prefixed polish notation).

    All these function modules are mentioned in the ABAP documentation of SUBMIT statement, in the section how to pass selection screen parameters from current report to another one.

    sandra

    (0) 
  3. Guillaume GARCIA
    Hi,

    Just a small question, is there a way to call ‘FREE_SELECTIONS_INIT’ so as to display logical database selection screens.
    Let’s say I would like to dynamically call these screens, is the good way to do that?

    Thanks in advance.
    Best regards,
    Guillaume

    (0) 
    1. Horst Keller Post author
      Hi Guillaume,
      I guess you want to use the screen as a subscreen in another screen. The parameters of FREE_SELECTIONS_DIALOG contain a AS_SUBSCREEN that points in that direction.

      Kind regards

      Horst

      (0) 
  4. Peter Inotai
    Hi,
    We’re facing the problem, that in release 4.70/BASIS 6.20 for numeric fields you have to enter lower and greater value, otherwise you got an error message (DB650). Only lower value is not excepted.
    It’s not happening in BASIS 7.00. Unfortunately we didn’t find any relevant OSS note.
    Has anybody have the same problem?
    Thanks,
    Peter
    (0) 
    1. Peter Inotai
      Actually it turned out that it’s related to fields, where there is a conversion exit in the domain (e.g. NUMC1). Implementing OSS Note 1243840 (Dynamic selection/field with initial value) solved this issue.
      Peter
      (0) 
      1. Peter Inotai
        Transaction SE16N is part of the APPL/CO component, that’s why it’s not part of NW (BASIS/ABA) delivery.
        The difference is, that
        – SE16N is a transaction to display (or depends on the table and client settings create or change ) data on any transparent table
        – The mentioned function module purpose is to enable dynamic selection in your application.
        You get back the conditions and in your application you can do any DB data select, which is required, not only display the data from one table.

        Peter

        (0) 
  5. Pavel Astashonok

    This code don’t compile! Correct following messed lines:
    FIELD-SYMBOLS <table> TYPE STANDARD TABLE.  on the field symbols place.
    and
    ASSIGN dref->* TO <table>.
    INTO TABLE <table>
    CHANGING t_table = <table> ).

    (0) 

Leave a Reply