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: 

Accessing contents of Field-Symbol

Former Member
0 Kudos

Hi ,

In a case scenerio our data is present in a Field-Symbol which is assigned TABLE type. We need to select a particular row in the Field-Symbol Internal Table depending on certain conditions. The problem we are facing is as the structure of the Field-Symbol is changng so the line type where the values are collected at run time is alos changing. hence we are not able to use the where clause to select a particular row of the table.

So how can we retrieve the values of the single row of the Field symbol. we have been succesful in generating the dynamic where clause, but cacant use it due to the limitation of the dynamic line type.

If anybody has worked something on these line, please help .

Thanks in advance.

Arunava

4 REPLIES 4

ssimsekler
Active Contributor
0 Kudos

Hi Arunava

Won't it work using "...ASSIGNING..." additions of "LOOP AT..." and "READ TABLE..."?

Then you can check your conditions by "CHECK" statement. For this you can take the value of the field by the "ASSIGN COMPONENT..." statement.

e.g.

FIELD-SYMBOLS:

<itab> TYPE TABLE ,

<wa> TYPE ANY ,

<val> TYPE ANY .

LOOP AT <itab> ASSIGNING <wa> .

ASSIGN COMPONENT <i>field_name</i> OF STRUCTURE <wa> TO <val> .

CHECK <val> = <i>comparison_value</i>.

...

ENDLOOP .

Hope this helps...

*--Serdar

0 Kudos

Hi Serdar,

Thanks for your reply. I was thinking in the same lines but got stuck at <val> = component_value, because I donot know the number of <val> that can be there. Because the <wa> or <itab> is different in different cases, hence it is not known before hand how many <val> can exist.

If this problem of identifying the number of <val> can be solved then we can surely follow this logic.

Arunava

0 Kudos

Hi

You told that you can generate the where statement dynamically. Then provided that you know the number of conditions at runtime, you can have a "DO...ENDDO" loop within which you change the assigned field to <val> and do the check.

e.g.

DATA lv_failed TYPE flag .

FIELD-SYMBOLS:

<itab> TYPE TABLE ,

<wa> TYPE ANY ,

<val> TYPE ANY .

CLEAR lv_failed .

LOOP AT <itab> ASSIGNING <wa> .

DO. <i>" "X TIMES" addition may be used here</i>

<i>*... adjust the field_name and consecutive comparison

  • value here, you can use another field-symbol for the

  • comparison value. The comparison fieldnames and values

  • should be stored in an internal table before this loop.</i>

READ TABLE lt_conds INDEX sy-index .

CHECK sy-subrc = 0 . <i>"OR use here to define exit condition</i>

ASSIGN COMPONENT field_name OF STRUCTURE <wa> TO <val> .

IF <val> NE comparison_value.

lv_failed = 'X' .

EXIT .

ENDIF .

ENDDO .

IF lv_failed = 'X' .

EXIT' .

ENDIF .

...

ENDLOOP .

You should of course extend this code some more. But I think this will help...

Best regards...

*--Serdar

You can determine the structure of <itab> at runtime using the cl_abap_typedescr classes.

The following code table an untype data reference and determines its structure at runtime:

Parameter:

@78\QImporting@ ITAB TYPE REF TO DATA

@7B\QReturning@ VALUE( STRUCT ) TYPE EXTDFIEST DD Internal: Information on Table Fields


  data: descrref   type ref to cl_abap_typedescr,
        no_header  type c,
        structref  type ref to cl_abap_structdescr,
        tableref   type ref to cl_abap_tabledescr,
        l_helpid   type string,
        l_typename type dfies-tabname,
        l_compname type dfies-fieldname,
        exttab     type extdfiest,
        extwa      type extdfies.

  field-symbols: <t>           type any,
                 <f>           type any,
                 <tab>         type index table,
                 <component>   type abap_compdescr.

  data: ddiccomptab type ddfields,
             isddic type abap_bool,
        extdfies_wa like line of struct,
         b_no_table type c.          " flags table is not available

  field-symbols <ddiccomp> type dfies.

****Get a pointer to the data table
  assign itab->* to <tab>.
***Read the first record initializing a work area and making sure it has some data
  read table <tab> index 1 assigning <t>.
  if sy-subrc ne 0.
    b_no_table = 'X'.
  else.
    b_no_table = ' '.
  endif.


****Get Type Description by Data Reference
  tableref ?= cl_abap_typedescr=>describe_by_data_ref( itab ).

  try.
****Is the type definition a pointer to a structure?
      structref ?= tableref->get_table_line_type( ).
****Is the structure a Data Diction Type?
      isddic = structref->is_ddic_type( ).
      if isddic eq abap_true.
****Kernel and Dynamic data defintions can't come from the Data Dictionary
        if structref->absolute_name+6(2) eq '%_'.
          isddic = abap_false.
        endif.
      endif.
      if isddic eq abap_true.
****Definition is in the data Dictionary- We can read details from the returned
****DDic Structure
        ddiccomptab = structref->get_ddic_field_list( ).
        loop at ddiccomptab assigning <ddiccomp>.
          extdfies_wa-fieldname = <ddiccomp>-fieldname.
          extdfies_wa-coltitle  = <ddiccomp>-reptext.
          extdfies_wa-inttype   = <ddiccomp>-inttype.
          extdfies_wa-convexit  = <ddiccomp>-convexit.
          append extdfies_wa to struct.
        endloop.
      else.
        if b_no_table is initial.
****Not in DDic - Get a Type description of the Workarea so we can analyse each field
          descrref = cl_abap_typedescr=>describe_by_data( <t> ).
          try.
****Get the structure of our type description for the workarea
              structref ?= descrref.
****Loop through the list of components(fields) in our work area
              loop at structref->components assigning <component>.
                extdfies_wa-fieldname = <component>-name.
                extdfies_wa-coltitle  = <component>-name.
****Get a pointer to the individual component(field) that we are currently processing
                assign component sy-tabix of structure <t> to <f>.
****Ask for the help ID (F1 help) belonging to field  - this will give the Data dictionary
****LIKE reference that this field was created from - See DESCRIBE FIELD - HELP-ID in on-line
****Help for a useful example
                describe field <f> help-id l_helpid.
                if l_helpid is not initial.
****Split the help-id into structure - element
                  split l_helpid at '-'
                          into l_typename l_compname.
                  extdfies_wa-tabname = l_typename.
*****Get details about the structure or table part of the helpid
                  call function 'DD_INT_TABLINFO_GET'
                    exporting
                      typename       = l_typename
                    tables
                      extdfies_tab   = exttab
                    exceptions
                      not_found      = 1
                      internal_error = 2
                      others         = 3.
                  if sy-subrc eq 0.
****Read the details about the element portion of the help-id from the information
****we just received using the field name from the original component
                    read table exttab into extwa with key fieldname = extdfies_wa-fieldname.
                    if sy-subrc eq 0.
                      extdfies_wa-coltitle = extwa-coltitle.
                      extdfies_wa-convexit = extwa-convexit.
                    else.
****No entry for the original component name, try the one from the help-id
                      read table exttab into extwa with key fieldname = l_compname.
                      if sy-subrc eq 0.
                        extdfies_wa-coltitle = extwa-coltitle.
                        extdfies_wa-convexit = extwa-convexit.
                      else.
****No Luck so far -> Lets ask the ABAP Descriptor class to try and describe the elment
****using a reference to the component we are processing
                        data: elemref type ref to cl_abap_elemdescr,
                              ddicobj type dd_x031l_table.
                        field-symbols <ddic> type x031l.
****Descriptor for tte Element
                        descrref = cl_abap_typedescr=>describe_by_data( <f> ).
                        try.
****Can we cast this to an DDic element descriptor?
                            elemref ?= descrref.
****Read the DDic defintion for this element
                            ddicobj = elemref->get_ddic_object( ).
****Get the Conversion Exit recorded in the DDic for this element
                            read table ddicobj index 1 assigning <ddic>.
                            if sy-subrc eq 0.
                              extdfies_wa-convexit = <ddic>-convexit.
                            endif.
****Catch all Global Execptions - like bad casts
                          catch cx_root.                 "#EC CATCH_ALL
                                                        "#EC NO_HANDLER
                        endtry.
                      endif.
                    endif.
                  endif.
                endif.
****Use Describe Field on the Element to get its basic data type
                describe field <f> type extdfies_wa-inttype.

                append extdfies_wa to struct.
              endloop.
****Catch all Global Execptions - like bad casts
            catch cx_root.                               "#EC CATCH_ALL
              no_header = 'X'.
          endtry.
        else.
          no_header = 'X'.
        endif.
      endif.

****Catch all Global Execptions - like bad casts
    catch cx_root.                                       "#EC CATCH_ALL
      no_header = 'X'.
  endtry.

You can then process all the fields in your <itab> dynamically with the following code:


*****Loop through the Data Table
  loop at <tab> assigning <wa>.
****For each component (field) in the table -Output the data
    loop at struct assigning <wa_desc>.
      assign component sy-tabix of structure <wa> to <f>.
  endloop.
endloop.