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

Dynamic assignment !!

Former Member
0 Likes
594

Hi Friends,

Here is my code - I have internal table with fieldname and fieldvalues - and I want to assign fieldvalues of respective fieldname to another structure.


*..source
types: begin of ty_src,
CNAME	type CHAR255,
CVALUE  type CHAR255,
END OF ty_src.

*..target
types: begin of ty_tar,
  ebeln type ebeln,
  ebelp type ebelp,
  end of ty_tar.

data gt_src type ty_src OCCURS 0 WITH HEADER LINE.
data gt_tar type ty_tar OCCURS 0 WITH HEADER LINE.
START-OF-SELECTION.

*filling itab.
gt_src-cname = 'EBELN'.
gt_src-cname = '4900000202'.
append gt_src.

gt_src-cname = 'EBELP'.
gt_src-cname = '00001'.

*..now I have ebeln field in target structure which need to move
*..dynamically to this gt_tat structure

*output should have

gt_tar-ebeln = '4900000202'.
gt_tar-ebeln = '00001'.

Hope I am clear in my questions - please let me know if you need further info..

Source may have different records to target structure - I want somethink like 'move-corresponding' type.

Thanks

Manohar

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
558

Hi,

Check this code.. You need to write the logic..

LOOP AT gt_scr.

CASE gt_scr-cname.
 WHEN 'EBELN'
    clear gt_tar.
    gt_tar-ebeln = gt_src-cvalue.
    APPEND gt_tar.
    l_index = l_index + 1.
 WHEN 'EBELP'.
   gt_tar-ebelp = gt_src-cvalue.
   MODIFY gt_tar TRANSPORTING ebelp  INDEX l_index.
ENDCASE.
  
ENDLOOP.

4 REPLIES 4
Read only

Former Member
0 Likes
559

Hi,

Check this code.. You need to write the logic..

LOOP AT gt_scr.

CASE gt_scr-cname.
 WHEN 'EBELN'
    clear gt_tar.
    gt_tar-ebeln = gt_src-cvalue.
    APPEND gt_tar.
    l_index = l_index + 1.
 WHEN 'EBELP'.
   gt_tar-ebelp = gt_src-cvalue.
   MODIFY gt_tar TRANSPORTING ebelp  INDEX l_index.
ENDCASE.
  
ENDLOOP.

Read only

0 Likes
558

Thanks Avinash - yes this is possible, but I want this to be dynamic as the src table contents are configuraable - and the target structure too. So I want this to make dynamic instead changing everytime when new field is added. Hope am clear.

Read only

0 Likes
558

Hi,

Check this code...

TYPES: BEGIN OF ty_src,
cname	TYPE char255,
cvalue  TYPE char255,
END OF ty_src.

*..target
TYPES: BEGIN OF ty_tar,
  ebeln TYPE ebeln,
  ebelp TYPE ebelp,
  END OF ty_tar.

DATA gt_src TYPE ty_src OCCURS 0 WITH HEADER LINE.
DATA gt_tar TYPE ty_tar OCCURS 0 WITH HEADER LINE.

FIELD-SYMBOLS : <fs1> TYPE ANY.

DATA l_index type i.

DATA : l_field TYPE char30.
DATA : l_field1 TYPE char30.

START-OF-SELECTION.

*filling itab.
  gt_src-cname = 'EBELN'.
  gt_src-cvalue = '4900000202'.
  APPEND gt_src.

  gt_src-cname = 'EBELP'.
  gt_src-cvalue = '00001'.
  APPEND gt_src.


  gt_src-cname = 'EBELN'.
  gt_src-cvalue = '4900000202'.
  APPEND gt_src.

  gt_src-cname = 'EBELP'.
  gt_src-cvalue = '00002'.
  APPEND gt_src.


  LOOP AT gt_src.

     IF gt_src-cname EQ 'EBELN'.
       CLEAR gt_tar.
     ENDIF. 

    CONCATENATE 'GT_TAR' '-' gt_src-cname INTO l_field.
    ASSIGN (l_field) TO <fs1>.
    MOVE gt_src-cvalue TO <fs1>.

    IF gt_src-cname EQ 'EBELN'.
      APPEND gt_tar.
      l_index = l_index + 1.
    ELSE.
      MODIFY gt_tar FROM gt_tar  INDEX l_index.
    ENDIF.

  ENDLOOP.

Read only

Former Member
0 Likes
558

Hi,

Using field-symbol logic ,we can do that,try this ,

DATA: NEW_TABLE TYPE REF TO DATA,

FIELD-SYMBOLS: <L_TABLE> TYPE TABLE ,

<L_LINE> TYPE ANY,

<L_FIELD1> TYPE ANY,

FIELD-SYMBOLS: <FS> TYPE ANY TABLE.

*Creating dynamic field control

CALL METHOD CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE

EXPORTING

IT_FIELDCATALOG = IT_LVC_CAT

IMPORTING

EP_TABLE = NEW_TABLE.

*Creating dynamic internal table

ASSIGN NEW_TABLE->* TO <L_TABLE>.

CREATE DATA NEW_LINE LIKE LINE OF <L_TABLE>.

ASSIGN NEW_LINE->* TO <L_LINE>.

loop at .....

ASSIGN COMPONENT 'SLNO' OF STRUCTURE <L_LINE> TO <L_FIELD1>.

<L_FIELD1> = '1'. ---> passing value to fieldcat

ASSIGN COMPONENT 'MATNR' OF STRUCTURE <L_LINE> TO <L_FIELD2>.

<L_FIELD2> = '01f029323'. ---> passing value to fieldcat

INSERT <L_LINE> INTO TABLE <L_TABLE>.

endloop.

this is only for logic to your reference

<=<< Sharing Knowledge is a way to Innovative >=>>

By,

Yoga