2

I need to get corresponding fields from cdpos-tabkey dynamically. Say, for example, I need to get plant, material, etc from tabkey.

I tried so far with the below code, but I get type conflict error when I use MOVE statement. When I use ASSIGN statement, values are getting assigned just like variables, not like structure format, and I cannot get exact field from structure.

FIELD-SYMBOLS <table> TYPE any.
DATA ls_data TYPE REF TO data.
CREATE DATA ls_data TYPE (t_cdpos-tabname).
ASSIGN ls_data->* TO <table>.
*MOVE-CORRESPONDING t_cdpos-tabkey TO <table>.
*<table> = t_cdpos-tabkey.
ASSIGN t_cdpos-tabkey to <table>.

anyone please help me to solve.

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
Archana Palani
  • 247
  • 1
  • 6
  • 23

2 Answers2

2

You can use CASTING TYPE construction for this task. Here is the sample solution based on RTTS:

DATA: handle        TYPE REF TO data,
      lref_struct TYPE REF TO cl_abap_structdescr.

SELECT DISTINCT * UP TO 100 ROWS
  FROM cdpos
  INTO TABLE @DATA(t_cdpos)
  WHERE tabname NOT LIKE '/%'.

LOOP AT t_cdpos ASSIGNING FIELD-SYMBOL(<fs_cdpos>).
  lref_struct ?= cl_abap_structdescr=>describe_by_name( <fs_cdpos>-tabname ).

* get key fields
  DATA(key_fields) = VALUE ddfields( FOR line IN lref_struct->get_ddic_field_list( ) WHERE ( keyflag NE space ) ( line ) ).

* filling key field components
  DATA(key_table) = VALUE abap_component_tab( FOR ls_key IN key_fields
                                              ( name = ls_key-fieldname
                                                type = CAST #( cl_abap_datadescr=>describe_by_name( ls_key-domname ) )
                                               )
                                            ).
* create key fields type handle
  TRY.
      DATA(r_type_struct) = cl_abap_structdescr=>create( key_table ).
    CATCH cx_sy_struct_creation .
  ENDTRY.

* create key type
  CHECK r_type_struct IS NOT INITIAL.
  CREATE DATA handle TYPE HANDLE r_type_struct.
  ASSIGN handle->* TO FIELD-SYMBOL(<structure>).

* assigning final key structure
  ASSIGN <fs_cdpos>-tabkey TO <structure> CASTING TYPE HANDLE r_type_struct.
ENDLOOP.

UPD: What concerns OP's question about addressing result structure, one cannot address its components by name (like WERKS), as one cannot know in advance its type, as it changes dynamically. You should either access structure components like:

ASSIGN COMPONENT 1 OF STRUCTURE <table> TO <component>.

Another, more robust variant will be using type handle r_type_struct which contains all fields components[] table:

LOOP AT r_type_struct->components[] ASSIGNING FIELD-SYMBOL(<fs_comp>).
  ASSIGN COMPONENT <fs_comp>-name OF STRUCTURE <table> TO <component>.
  IF <COMPONENT> IS ASSIGNED.
  "do smth
  ENDIF.
ENDLOOP.
Suncatcher
  • 10,355
  • 10
  • 52
  • 90
  • thanks for the example , how to get the structure value , for example i want to get werks from structure , when i write
    -werks i am getting in program like The data object "
    " has no structure and therefore no component called "WERKS". called "WERKS".
    – Archana Palani Aug 03 '17 at 04:31
  • @ArchanaPalani, see the update which answers your question. – Suncatcher Aug 03 '17 at 06:14
  • Thank you so much such a smart answer :-) it solves my problem, learned something new :-) – Archana Palani Aug 07 '17 at 03:44
1

You can find the code that assembles the key in the function module CHANGEDOCU_KEY_ANY2CHAR. The function module CHANGEDOCU_KEY_CHAR2ANY implements the opposite operation. Other function modules of the function group SCD8 might also be relevant - check the inline documentation.

vwegert
  • 18,371
  • 3
  • 37
  • 55
  • i have gone through the function module , to be more clear i need to display change document plant wise , i have got all data's only bottle neck is separating plant and other details from tabkey without using offset method. – Archana Palani Jun 13 '17 at 02:45