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: 

Read a structure with out specifying its field/component names-Dynamically?

former_member202077
Participant
0 Kudos

Hello

My requirements:

I need to write a module pool prog., which updates a Z_material_master_data_table, which tables has 150 fields (i knew its odd having 150 fields in a master table).

On the module pool prog. i need to place the fileds in 2 columns, say left column(for current data) with 150 fields and right column(user input data for updating the Z_material_master_data_table) with 150 fields

When user opens the prog., user will be presented with a initial screen, where in user enters the material number (for which user want a updation ) then, the user will redirected to 2nd screen where in my prog. will default/prepopulate the left side fields with the current(old) data. Now user has to input his/her data (new) for updating any field of the z_master_data_table.

And after finishing the user clicks a push button with title of 'UPDATE'

Now, my prog. has update the entry in the z_material_master_data_table. The 2nd screen looks like below,

 

_Currnet data_ -------------------------------------- _New data_

Current Material description ---------------------- New Material description
The field will placed here  ------------------------ The field will placed here to 
for showing the current data                              inputting the new data by user

As there are 150 fields, i would not like to write validation (cross checking whether user entered the data on each field) on every field of 150 in number.

The 2nd reason, they want the prog. a kind of dynamic in nature, like going further, if the business want to add a new field, say 151_field to the the table, they dont want to change the prog. (just keeping the 151_field should enough) to address this newely added field.

By thinking field symbols approach, i created 2 structures for old data and another for new data. These structurer are replicas of z_material_master_data_table, but 2nd (new) structure field names posses a an extra 4 chararctes like '_UPD', say for example, ZZMAKTX will be the fileld name in structure_1 (old) and its name will be ZZMAKTX_UPD in 2nd structure(new)

Pls. give me some code snippet for (my above requiremnet) reading a structure DYNAMICALLY with out specifying/mentioning the field names for each and every field or give me any better idea to meet my requirement

Thank you

1 ACCEPTED SOLUTION

kesavadas_thekkillath
Active Contributor
0 Kudos

This is not upto the mark but you will get an idea to do it.


TYPES:BEGIN OF tyn,
     field TYPE c,
     END OF tyn.

TYPES:BEGIN OF tyu,
     field_upd TYPE c,
     END OF tyu.

DATA:it_new TYPE TABLE OF tyn,
     it_upd TYPE TABLE OF tyu,
     wa_new TYPE tyn,
     wa_upd TYPE tyu.

FIELD-SYMBOLS:<fs_n> TYPE ANY,
              <fs_u> TYPE ANY.

DATA lr_structdescr TYPE REF TO cl_abap_structdescr.
DATA:lt_components TYPE TABLE OF abap_compdescr,
     l_component LIKE LINE OF lt_components.
DATA field TYPE fieldname.


wa_new-field = 'A'.wa_new-field = 'B'.APPEND wa_new TO it_new.
wa_new-field = 'C'.wa_new-field = 'D'.APPEND wa_new TO it_new.

wa_upd-field_upd = 'A'.wa_upd-field_upd = ' '.APPEND wa_upd TO it_upd.
wa_upd-field_upd = 'C'.wa_upd-field_upd = ' '.APPEND wa_upd TO it_upd.

lr_structdescr ?= cl_abap_typedescr=>describe_by_name( 'TYU' ).

LOOP AT it_new INTO wa_new.
  READ TABLE it_upd INTO wa_upd INDEX sy-tabix.
  CHECK sy-subrc = 0.
  LOOP AT lr_structdescr->components INTO l_component.
    ASSIGN COMPONENT l_component-name OF STRUCTURE wa_upd TO <fs_u>.
    IF sy-subrc = 0.
      IF <fs_u> IS INITIAL.
        WRITE: 'Data in Structure WA_NEW is', wa_new-field.
        concatenate 'WA_UPD-' l_component-name into field.
        WRITE: 'No Data in field', field.
        SKIP 1.
      ENDIF.
    ENDIF.
  ENDLOOP.
ENDLOOP.

8 REPLIES 8

manigandan_d2
Explorer
0 Kudos

From my understanding, your requirement was creating the Program to display the 150 fields of the custom Z-Table based on the material number.

Once the user enters the Material number and click enter. It has to navigate to another screen with old data in one column and second column for the new data, Update button for saving the new data to backend Z-Table.

Back end 150 fields will be changed based on the user requirement, So you need to implement it as dynamically.

Solution:

data : it_tabdescr type abap_compdescr_tab.

data : ref_table_descr type ref to cl_abap_structdescr.

DATA : cl_struct TYPE REF TO cl_abap_structdescr.

data: p_table(30) type C. value u2018Custom Z-table Nameu2019.

*----


Return structure of the table.

ref_table_descr ?= cl_abap_typedescr=>describe_by_name( p_table ).

it_tabdescr[] = ref_table_descr->components[].

cl_struct ?= cl_abap_structdescr=>create( it_tabdescr ).

CREATE DATA dyn_line TYPE HANDLE cl_struct.

assign dyn_line->* to <fs_wa>. u201C<fs_wa> have the same structure of the Z-table

CREATE DATA dyn_line1 TYPE HANDLE cl_struct.

assign dyn_line->* to <fs_wa_update>. u201C<fs_wa_ update> have the same structure of the Z-table

Best Regards,

Manigandan D

Edited by: Manigandan Damodharan on Feb 4, 2012 8:58 PM

Clemenss
Active Contributor
0 Kudos

Hi,

use an editable ALV grid with editable and non-editable columns. Then any future structure changes will be automatically included.

Regards,

Clemens

0 Kudos

Thank you.

But, because of some reasons, business do not want to have it in ALV format!! Hence we designed a module pool prog.

Thank you

kesavadas_thekkillath
Active Contributor
0 Kudos

In 2nd screen is it with two table controls or single one having two columns ?

Kesav

0 Kudos

Thank you, i was on vacation.

They are 2 table controls.

0 Kudos

Hi,

This basic idea should help you handle this dynamically, it is expanded on one of the other posts but allows you to dynamically assign based on the structure dynamically no matter how many fields there are:


DATA: lt_fields       TYPE abap_compdescr_tab,
      ref_table       TYPE REF TO cl_abap_structdescr,
      lv_lines        TYPE i.

FIELD-SYMBOLS: <zold> TYPE ztable,
               <old>  TYPE any,
               <znew> TYPE ztable,
               <new>  TYPE any.     

ref_table_descr ?= cl_abap_typedescr=>describe_by_name( 'ZTABLE' ).
it_tabdescr = ref_table_descr->get_components( ).
DESCRIBE TABLE it_tabdescr LINES lv_lines.

DO lv_lines TIMES.
  ASSIGN COMPONENT sy-index OF STRUCTURE <zold> TO <old>.
* Assign value at sy-index of table control one to <old>
  ASSIGN COMPONENT sy-index OF STRUCTURE <znew> TO <new>.
* Assign value at sy-index of table control two to <new>
ENDDO.

* At the end update ztable from <znew>.

Regards,

Ryan Crosby

Former Member
0 Kudos

Hello,

for your requirement if you use a module pool program then you will end up in making your life tough. Try using TABLE MAINTENANCE VIEWS(with 2 steps), this will take care of your validations. Then in future if they add any fields to the table then no need to modify the program, you can simply regenerate the Maintenance.

In order to achieve your second screen, to display the old and new values, you can add an single screen using the module pool.

Let me know if you have any questions.

Thanks & Regards

Nivash

kesavadas_thekkillath
Active Contributor
0 Kudos

This is not upto the mark but you will get an idea to do it.


TYPES:BEGIN OF tyn,
     field TYPE c,
     END OF tyn.

TYPES:BEGIN OF tyu,
     field_upd TYPE c,
     END OF tyu.

DATA:it_new TYPE TABLE OF tyn,
     it_upd TYPE TABLE OF tyu,
     wa_new TYPE tyn,
     wa_upd TYPE tyu.

FIELD-SYMBOLS:<fs_n> TYPE ANY,
              <fs_u> TYPE ANY.

DATA lr_structdescr TYPE REF TO cl_abap_structdescr.
DATA:lt_components TYPE TABLE OF abap_compdescr,
     l_component LIKE LINE OF lt_components.
DATA field TYPE fieldname.


wa_new-field = 'A'.wa_new-field = 'B'.APPEND wa_new TO it_new.
wa_new-field = 'C'.wa_new-field = 'D'.APPEND wa_new TO it_new.

wa_upd-field_upd = 'A'.wa_upd-field_upd = ' '.APPEND wa_upd TO it_upd.
wa_upd-field_upd = 'C'.wa_upd-field_upd = ' '.APPEND wa_upd TO it_upd.

lr_structdescr ?= cl_abap_typedescr=>describe_by_name( 'TYU' ).

LOOP AT it_new INTO wa_new.
  READ TABLE it_upd INTO wa_upd INDEX sy-tabix.
  CHECK sy-subrc = 0.
  LOOP AT lr_structdescr->components INTO l_component.
    ASSIGN COMPONENT l_component-name OF STRUCTURE wa_upd TO <fs_u>.
    IF sy-subrc = 0.
      IF <fs_u> IS INITIAL.
        WRITE: 'Data in Structure WA_NEW is', wa_new-field.
        concatenate 'WA_UPD-' l_component-name into field.
        WRITE: 'No Data in field', field.
        SKIP 1.
      ENDIF.
    ENDIF.
  ENDLOOP.
ENDLOOP.