Skip to Content
avatar image
-1
Former Member

how to change the field name dynamically in Loop

Dear All,


I have to change the field name dynamically in the loop. Database Name is PA0027 having fields PSP01, PSP02, PSP03 up to PSP25. I want to pass the data to these fields in loop and update.

Following is my Code.

LOOP AT IT_FINAL INTO WA_FINAL
MOVE WA_FINAL-WBS TO WA_PA0027-PSP01.
ENDLOOP.

In above code I have to change the Field name in each loop pass that WA_PA0027-PSP01, then in next loop WA_PA0027-PSP02 then next loop pass WA_PA0027-PSP03. So please suggest me how to change the field name dynamically in Loop.

Waiting for reply.

Thank You

Regards

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

1 Answer

  • Best Answer
    Jul 20, 2017 at 12:07 PM

    You can use field-symbols here:

    Here is the link to Help topic on this

    https://help.sap.com/viewer/cfae740a0a21455dbe6e510c2d86e36a/7.31.20/en-US/fceb3923358411d1829f0000e829fbfe.html

    And a code snippet. Please note that I did not take care of performance etc. into conderation, I just want to educate you on use of field-symbols.

    FIELD-SYMBOLS <fs_field> TYPE any.
    DATA lv_field TYPE char5.
    DATA lv_counter TYPE n LENGTH 2.
    
    LOOP AT lt_final INTO ls_final.
    
      DO 10 TIMES.  
        lv_counter = sy-index.
        lv_field = 'PSP' && lv_counter.
        ASSIGN COMPONENT lv_field OF STRUCTURE ls_final TO <fs_field>.
        <fs_field> = '123'.
      ENDDO.
    
    "modify record here
    
    ENDLOOP.
    Add comment
    10|10000 characters needed characters exceeded

    Further commenting has been locked.