Application Development and Automation 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: 
Read only

get field name from dynamic structure

Former Member
0 Likes
19,127

I have a dynamic structure of a dynamic table. I want to loop thru table and check if there is field name 'ABC' exist in the structure. If it exists, depending on value of the field I will change value of another field in the structure.

For example if field struct-fieldname = 'ABC ' and field value = 'S', I will display field XYZ (struct-xyz) as positive, otherwise display as negative. Can this be achieved using dynamic structure and dynamic table via field symbol? Thanks

I have a dynamic structure of a dynamic table. I want to loop thru table and check if there is field name 'ABC' exist in the structure. If it exists, depending on value of the field I will change value of another field in the structure.

For example if field struct-fieldname = 'ABC ' and field value = 'S', I will display field XYZ (struct-xyz) as positive, otherwise display as negative. Can this be achieved using dynamic structure and dynamic table via field symbol? Thanks

5 REPLIES 5
Read only

Former Member
0 Likes
6,148

hello ,

try like this

data:

ls_comp type abap_componentdescr,

ls_stru type ref to cl_abap_structdescr,

lt_comp type standard table of abap_componentdescr.

ls_stru ?= cl_abap_typedescr=>describe_by_data(<FS1>).

lt_comp = ls_stru->get_components( ).

internal table lt_comp with have both field name , values as well.

loop at lt_comp into ls_comp.

ls_comp-name -->fieldname

assign component sy-tabix

of structure i_copaitem

to <fs_value>.--->fieldvalue

endloop.

regards

Prabhu

Read only

Former Member
0 Likes
6,148

Hi,

you can use the below code snippet

FIELD-SYMBOLS : <lv_value> TYPE any.

ASSIGN COMPONENT 'ABC' of STRUCTURE struc to <lv_value>.

IF sy-subrc = 0.

IF <lv_value> IS INITIAL.

ENDIF.

ENDIF.

This will read the value of field 'ABC' from your structure to <lv_value>

Read only

Former Member
0 Likes
6,148

Hi,

You can try this



DATA:       tb_struct TYPE REF TO cl_abap_structdescr,
      tb_comp   TYPE        abap_component_tab.

* Populate the fields of wa_pc_model_data2
  tb_struct ?=
  cl_abap_typedescr=>describe_by_data( wa_pc_model_data2 ).

  LOOP AT tb_struct->components ASSIGNING <fs_components>.
    wf_var1 = <fs_components>-name.

    PERFORM fill_fields  USING    wf_var1
                                  wa_pc_model_data2
                         CHANGING wf_update_coc.
    CLEAR: wf_var1.
  ENDLOOP.

  CLEAR tb_struct.
  UNASSIGN: <fs_components>,
            <fs>,
            <fs_name>.


FORM fill_fields USING    pf_wf_var1
                          pf_wa_pc_data_field
                 CHANGING pf_wf_update_coc.

** Assign the component of the structure to the field symbol
  ASSIGN COMPONENT pf_wf_var1
    OF STRUCTURE pf_wa_pc_data_field TO <fs_name>.
  IF sy-subrc  = 0.

    ASSIGN COMPONENT pf_wf_var1
      OF STRUCTURE wa_coc_tech TO <fs>.
    IF sy-subrc = 0.

** Check if the IDOC data is filled
** Also check if the IDOC and database table data are same
      IF NOT <fs_name> IS INITIAL AND  "IDOC data
         <fs_name> NE <fs>.    "IDOC data NE database table data
        <fs> = <fs_name>.      "Update database field from IDOC field
        pf_wf_update_coc = 'X'. "Set the flag
      ENDIF.
    ENDIF.
  ENDIF.

ENDFORM.                    " fill_fields

Read only

0 Likes
6,148

Thanks, but your code gave me compilation errors due to no data definition for: <fs_components>

If I declare <fs_components> as any,

there will be no component name <fs_components>-name.

Also what is wf_var1, wf_update_coc?

Anyway, I solved the problem. I just loop at the dynamic structure check if the component field name 'XXX' and 'YYY' exist, if both field name exist,

then I can change value via command

assign component 'XXX' of structure <fs> to <fval>.

assign component 'YYY' of structure <fs> to <fval2>.

if <fval> = 'H'.

<fval2> = <fval2> * - 1.

endif.

thanks all for your help.

Read only

jayesh_mudaliar
Explorer
0 Likes
6,148

Hi Valerie,

Glad to hear you solved this issue but for those who are not aware you can declare <fs_components> as <fs_components> type abap_compdescr.

You can declare lv_name as string.

lv_name = <fs_components>-name.

So here in LV_NAME you will get the feild name of any dynamic Structure. Now using Assign Component you can get the values from the structure.

Regards,

Jayesh