Search code examples
fieldabapcorresponding-records

Access any structure field chosen dynamically at run time


I have a problem, so I have a huge table where some fields contain only numbers from 1-20 and I want to move the values of the fields to a new table where there are 3 fields with a name and the number (zjdc01 or zadc01).

Now I want to check the field value from the huge table and append the values to the new fields.

For Example :

CASE LS_ATLAS_DC-ZJDC01.
    WHEN 1.
      LS_ATLAS-ZJDC01 = LS_ATLAS_DC-ZJDC01.
      LS_ATLAS-ZADC01 = LS_ATLAS_DC-ZADC01.
      LS_ATLAS-ZBDC01 = LS_ATLAS_DC-ZBDC01.
    WHEN 2.
      LS_ATLAS-ZJDC02 = LS_ATLAS_DC-ZJDC01.
      LS_ATLAS-ZADC02 = LS_ATLAS_DC-ZADC01.
      LS_ATLAS-ZBDC02 = LS_ATLAS_DC-ZBDC01.
    WHEN 3.
      LS_ATLAS-ZJDC03 = LS_ATLAS_DC-ZJDC01.
      LS_ATLAS-ZADC03 = LS_ATLAS_DC-ZADC01.
      LS_ATLAS-ZBDC03 = LS_ATLAS_DC-ZBDC01.
    WHEN 4.
      LS_ATLAS-ZJDC04 = LS_ATLAS_DC-ZJDC01.
      LS_ATLAS-ZADC04 = LS_ATLAS_DC-ZADC01.
      LS_ATLAS-ZBDC04 = LS_ATLAS_DC-ZBDC01.

But this is very exhausting and I think there is another Solution but I dont know if ABAP have something for this.

Maybe some of you have a Solution or have a similiar problem which he solved.


Solution

  • Use ASSIGN COMPONENT name OF STRUCTURE structure TO <field_symbol>.

    DATA name TYPE string. " component name
    FIELD-SYMBOLS: <zjdc_xx> TYPE any,
                   <zadc_xx> TYPE any,
                   <zbdc_xx> TYPE any.
    
    IF number BETWEEN 1 and 4.
    
      name = |ZJDC{ number WIDTH = 2 ALIGN = RIGHT PAD = '0' }|.   "<== ZJDC01 to ZJDC04
      ASSIGN COMPONENT name OF STRUCTURE ls_atlas TO <zjdc_xx>.
      name = |ZADC{ number WIDTH = 2 ALIGN = RIGHT PAD = '0' }|.   "<== ZADC01 to ZADC04
      ASSIGN COMPONENT name OF STRUCTURE ls_atlas TO <zadc_xx>.
      name = |ZBDC{ number WIDTH = 2 ALIGN = RIGHT PAD = '0' }|.   "<== ZBDC01 to ZBDC04
      ASSIGN COMPONENT name OF STRUCTURE ls_atlas TO <zbdc_xx>.
    
      <zjdc_xx> = LS_ATLAS_DC-ZJDC01.
      <zadc_xx> = LS_ATLAS_DC-ZADC01.
      <zbdc_xx> = LS_ATLAS_DC-ZBDC01.
    
    ENDIF.