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: 

Get field name dynamically

Former Member
0 Kudos

Hi Experts,

I have dynamically creaeted a internal table and it's work area.

My requirement is I want field names from the dynamically created work area.

And enter data into the particular field selected field.

Can anyone explain me how can I achieve it?.

Thanks in advance.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

HI Kish,

You have to  use one more field symbol to hold field name then,

you can dynamically assign field name from work area to new field symbol.

The code will be something like..

 

1. ASSIGN COMPONENT fieldname/field_position OF STRUCTURE <dynamic_work_area> TO <new_work_area>. " Get field name

2. <new_work_area> = field_value.        " Assign value to field

" repeat step 1 and 2 for all the fields and at last.

APPEND <dynamic_work_area> TO <dynamic_internal_table>.

5 REPLIES 5

edgar_nagasaki
Contributor
0 Kudos

Hi Kish,

Try to user LVC_FIELDCATALOG_MERGE:

        CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'

             EXPORTING

                  I_STRUCTURE_NAME                = itab

             CHANGING

                  ct_fieldcat                                  = lt_fieldcat

             EXCEPTIONS

                  INCONSISTENT_INTERFACE       = 1

                  PROGRAM_ERROR                    = 2

                  OTHERS                                     = 3.

        IF sy-subrc IS INITIAL. 

             LOOP at lt_fieldcat INTO wa_fieldcat.

                  WRITE: / wa_fieldcat-fieldname.

             ENLOOP.

        ENDIF.

Regards,

Edgar

former_member184675
Active Participant

parin_doshi3
Explorer
0 Kudos

Hi Kish

*Data declaration

DATA: lo_abap_structdescr TYPE REF TO cl_abap_structdescr,
            lo_abap_typedescr   TYPE REF TO cl_abap_typedescr.

DATA : lv_excel_field(500TYPE c,

*** declare constant containing structure information and structure name local delcared

lc_excel_op_structure

lc_excel_structure - local structure like ls_**** holding the data

*** Get the Structure Description
  lo_abap_typedescr = cl_abap_structdescr=>describe_by_name( lc_excel_op_structure ).
*** Get the structure field
  lo_abap_structdescr ?= lo_abap_typedescr.

lo_abap_structdescr will contain all the fields of the structure

READ TABLE lo_abap_structdescr->components INTO ls_components
                                                 WITH KEY name = (structure field you want)

      IF sy-subrc = 0 AND ls_components-name IS NOT INITIAL.
        UNASSIGN <ls_excel_field_value>.
        CONCATENATE lc_excel_structure ls_components-name INTO lv_excel_field.
        CONDENSE lv_excel_field NO-GAPS.
        TRANSLATE lv_excel_field TO UPPER CASE.
        ASSIGN (lv_excel_field) TO <ls_excel_field_value>.
        IF  <ls_excel_field_value> IS ASSIGNED.

         <ls_excel_field_value> =  ( value which you want to populate )

         endif.

endif.

Thanks & Regards

Parin

Former Member
0 Kudos

Its easy, please do a search on cl_abap_typedescr. you will get it..!!!

Former Member
0 Kudos

HI Kish,

You have to  use one more field symbol to hold field name then,

you can dynamically assign field name from work area to new field symbol.

The code will be something like..

 

1. ASSIGN COMPONENT fieldname/field_position OF STRUCTURE <dynamic_work_area> TO <new_work_area>. " Get field name

2. <new_work_area> = field_value.        " Assign value to field

" repeat step 1 and 2 for all the fields and at last.

APPEND <dynamic_work_area> TO <dynamic_internal_table>.