Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Creating a range for a field from internal table using RTTS

0 Kudos

Hello, experts.

I want to create a function/custom class method that takes in 2 parameters:

1) IM_ITAB type ANY TABLE

2) IM_COMPONENT type STRING

and spits out 1 parameter:

1) EX_RANGE type PIQ_SELOPT_T

So, algorithm is like this:

First of all, we check if the column with a component name at all exists

Then, we check that internal table is not empty.

Then, we loop through internal table assigning component and filling range table. Code is below.

METHOD compose_range_from_itab.

  DATA: lo_obj TYPE REF TO cl_abap_tabledescr.

  DATA: wa_range TYPE selopt,

        lt_range TYPE piq_selopt_t.


  FIELD-SYMBOLS: <fs_line> TYPE ANY,

                 <fs_component> TYPE ANY.

  lo_obj ?= cl_abap_typedescr=>describe_by_data( p_data = im_itab ).

  READ TABLE lo_obj->key TRANSPORTING NO FIELDS WITH KEY name = im_component.

  IF sy-subrc IS INITIAL.

    IF LINES( im_itab ) GT 0.

      LOOP AT im_itab ASSIGNING <fs_line>.

        ASSIGN COMPONENT im_component OF STRUCTURE <fs_line> TO <fs_component>.

        wa_range-sign = 'I'.

        wa_range-option = 'EQ'.

        wa_range-low = <fs_component>.

        APPEND wa_range TO lt_range.

      ENDLOOP.

      SORT lt_range BY low.

      DELETE ADJACENT DUPLICATES FROM lt_range COMPARING low.

      ex_range[] = lt_range[].

    ENDIF.

  ENDIF.

ENDMETHOD.

But I want to improve the method further. If imported internal table has, let's say, 255 columns, then it will take longer to loop through such table. But I need only one column to compose the range.

So I want to get components of internal table, then choose only one component, create a new line type containing only that component, then create internal table with that line type and copy:

append corresonding fields of im_itab into new_line_type_internal_table.

How can I "cut out" one component and create a new line type using RTTS? Please help

0 REPLIES 0