David Teixeira
David Teixeira

Reputation: 73

Pick field value from an ABAP structure

I'm trying to pick up the value from a field which is in a table line. But I cannot access it because my table has no header line. I have the following code:

DATA: t2            TYPE TABLE OF komlfp,
      go_settlement TYPE REF TO cl_wb2_rebate_settlement_2.
TRY.

  CREATE OBJECT go_settlement.

  go_settlement->settlement(
                   EXPORTING
                     iv_testrun        = 'X'
                     iv_bldat          = sy-datum
                     iv_wfdat          = sy-datum
                     is_contract       = wa_view
                     is_contract_first = wa_view
                   IMPORTING
                     et_komlfk         = t1
                     et_komlfp         = t2 ).

CATCH cx_wb2_rebates.
ENDTRY.

What I want now is to use the value t2-kzwi6d, but I always get:

"T2" is a table without a header line and therefore does not have a component called "KZWI6D".

I've already tried declaring the table with a header line, but when I try that, I get:

Tables with headers are no longer supported in the OO context.

Can someone help me please?

Thanks

Upvotes: 1

Views: 2836

Answers (1)

Legxis
Legxis

Reputation: 916

You created t2 as a table without a header line, so the program cannot recognize any column names.

One solution could be to define it as a table with a header line, though that approach is considered obsolete and outdated.

DATA: t2 TYPE STANDARD TABLE OF komlfp WITH HEADER LINE.

Another option is to dynamically get the field value. But then you have to make sure it actually exists and that the assign worked, so more error management is necessary.

DATA: t2 TYPE STANDARD TABLE OF komlfp.

FIELD-SYMBOLS: <t2_line> LIKE LINE OF t2,
               <kzwi6d>  TYPE kzwi6.

LOOP AT t2 ASSIGNING <t2_line>.
  ASSIGN COMPONENT 'KZWI6D' OF STRUCTURE <t2_line> TO <kzwi6d>.
ENDLOOP.

Edit: As Sandra has pointed out, you don't have to do it dynamically and can just get the value directly like this as well:

DATA: t2 TYPE STANDARD TABLE OF komlfp.

FIELD-SYMBOLS: <t2_line> LIKE LINE OF t2.

LOOP AT t2 ASSIGNING <t2_line>.
  " you now have access to the value using <t2_line>-kzwi6d
ENDLOOP.

Upvotes: 4

Related Questions