Skip to Content
-4

Assign dynamic Internal Table to Field Symbol (without Looping)

May 31, 2017 at 02:40 PM

270

avatar image
Former Member
TYPES: BEGIN OF ty_0001,
        pernr  TYPE pa0001-pernr,
       END OF ty_0001.
DATA: int_0001 TYPE STANDARD TABLE OF ty_0001, 
      index    TYPE i,
      lv_fs    TYPE string,
      dref     TYPE REF TO data.
CONSTANTS:
  lc_us        TYPE string VALUE
               '000100020007',

FIELD-SYMBOLS  <fs_table> TYPE ANY TABLE .

  LOOP AT int_0000.
    WHILE index LT strlen( lc_us ).
      CONCATENATE 'int_' lc_us+index(4) '[]' INTO lv_fs.
      CREATE DATA dref TYPE STANDARD TABLE OF ty_0001.
      ASSIGN dref->* TO <fs_table>.

*--Unable to assign - errors out
      <fs_table> = ( lv_fs ).

*--Works for a defined table - int_0001[], looking for 
*  a generic Table for multiple iterations
*  <fs_table> = int_0001[]. 

*--Read  
      IF line_exists( <fs_table>[ ('Pernr') = int_0000-pernr ] ).
      ELSE.
        int_miss-line  = |Missing Infotype Record -| &
                         |{ lc_us+index(4) }|.
      ENDIF.
      index = index + 4.
    ENDWHILE.
  ENDLOOP.


Errors: LV_FS cannot be converted to <fs_table>
10 |10000 characters needed characters left characters exceeded

What is the question? What problem are you trying to solve?

1
Former Member

Purpose:

SAP HR System: Identify the Missing Infotypes for a Particular Person.

Logic:

Select Infotype 0 based on dates on selection screen.

Select Infotype 1 based on dates on selection screen.

Select Infotype 2 based on dates on selection screen.

Loop through Infotype 0.

Check other Infotype 1, infotypes 2, Infotype 6, Infotype 7 if the record exists dynamically

Through while iteration I am changing the Infotypes 1 to Infotype 7 and so on.

endloop.

I have tried in a different way, but it does not work

Note: I cannot loop through each dynamic Table just for assigning it to the Field Symbol

as I have multiple tables ( IT0001, IT0002, IT0007...) to assign.

  LOOP AT int_0000.
    WHILE index LT strlen( lc_us ).
      CONCATENATE 'int_' lc_us+index(4) '[]' INTO lv_fs.
      GET REFERENCE OF  lv_fs INTO dref.
      ASSIGN dref->* TO <fs_table>.
*      READ TABLE <fs> WITH KEY ('pernr') = int_0000-pernr
*                           TRANSPORTING NO FIELDS.
      IF line_exists( <fs_table>[ ('Pernr') = int_0000-pernr ] ).
      ELSE.
        int_miss-line  = |Missing Infotype Record -| &
                         |{ lc_us+index(4) }|.
      ENDIF.
      index = index + 4.
    ENDWHILE.
  ENDLOOP.
<br>
0

The major issue in your code is about the readability. Stick to the KISS principle!

Why does the requirement says "dynamically" ? is that an interview question ? then your answer should be "I won't do that, I need a very valid reason to overcomplexify a program".

1
Former Member
Sandra Rossi

We are trying to use the same program for multiple countries,

US has different set of Infotypes (Stored the list of Infotypes in constant lc_us)

while CA has different set. (Stored in constant lc_ca)

and could be extensible to other countries,

Could not keep on writing the same code just for one Infotype change.

Note:

We need to know if the record exists or not ? ( not any particular fields) and all the Infotypes have the following structure

except Infotype 0000.

TYPES: BEGIN OF ty_0001,
        pernr TYPE pa0001-pernr,
       END OF ty_0001.
0
* Please Login or Register to Answer, Follow or Comment.

2 Answers

Best Answer
Mike Pokraka Jun 01, 2017 at 08:31 AM
0

Yes it's technically possible:

itab_name = 'int_0001'.
assign (itab_name) to field_symbol(<infotype_data>). 

Please look at the assign doco:

https://help.sap.com/http.svc/rc/abapdocu_750_index_htm/7.50/en-US/abapassign_mem_area_dynamic_dobj.htm

Show 2 Share
10 |10000 characters needed characters left characters exceeded
Former Member

Mike,

Can you convert your recent post to answer ? So, that i can accept it and award points.

The solution to my problem

  DATA: index  TYPE i,
        lv_fs  TYPE string.
  CONSTANTS:
    lc_us      TYPE string VALUE
               '000100020007'.
  FIELD-SYMBOLS: 
   <fs_table>  TYPE STANDARD TABLE,
   <fs>        TYPE any.

  LOOP AT int_0000.
    WHILE index LT strlen( lc_us ).
      CONCATENATE 'int_' lc_us+index(4)'[]' INTO lv_fs.
      ASSIGN (lv_fs) TO <fs>.
      ASSIGN <fs> TO <fs_table>.
      IF line_exists( <fs_table>[ ('Pernr') = int_0000-pernr ] ).
      ELSE.
        int_miss-line  = |Missing Infotype Record -| &
                         |{ lc_us+index(4) }|.
      ENDIF.
      index = index + 4.
    ENDWHILE.
  ENDLOOP.
0

Done, thanks :-)

Yes, your code was what I had in mind.

0
Mike Pokraka May 31, 2017 at 10:08 PM
0

Accessing infotype tables directly is usually troublesome. And as Sandra said, not very easy to read or understand. How about:

loop at required_infotypes into infotype_nr.

  call function 'HR_READ_INFOTYPE' 
    exporting pernr = <pernr>
              infty = infotype_nr
              begda = start_date
              endda = end_date
    IMPORTING subrc = fm_subrc
    TABLES    infty_tab = infotypes
    EXCEPTIONS INFTY_NOT_FOUND = 1.

  if fm_subrc = 8. 
     "It's missing!
  endif.
endloop. 

That way you can also control validity etc much easier and take advantage of any preexisting buffering/caching.

Show 1 Share
10 |10000 characters needed characters left characters exceeded
Former Member

Approaches:

  1. I agree, as you suggested the above method works. Specific to HR system it is always recommended to use Std FM - HR_READ_INFOTYPE.
  2. Even, Passing Table as a Parameter would work (refer Code snippet below)

I wanted to know - Is it technically possible?

To assign a Dynamic Table to a Field Symbol (without Looping & not as a parameter)?

Main issue in my initially posted logic:

Converting the dynamically composed string to a Table ? (without any issues)

    LOOP AT int_0000.
      PERFORM infotype_read TABLES int_0001
      PERFORM infotype_read TABLES int_0002.
      PERFORM infotype_read TABLES int_0007.
    ENDLOOP.

*--Form 
FORM infotype_read TABLES  p_table TYPE STANDARD TABLE.
  FIELD-SYMBOLS : <fs_table> TYPE STANDARD TABLE.
  DATA dref TYPE REF TO data.
  CREATE DATA dref TYPE STANDARD TABLE OF ty_0001.
  ASSIGN dref->* TO <fs_table>.
  <fs_table> = p_table[].
  IF line_exists( <fs_table>[ ('Pernr') = int_0000-pernr ] ).
  ELSE.
    int_miss-line = |Missing Infotype Record -|.
    APPEND int_miss.
  ENDIF.
ENDFORM.
0