Skip to Content
0
May 25 at 12:17 PM

"LOOP AT ... GROUP BY" vs "FOR GROUPS ... OF"

66 Views Last edit May 25 at 04:00 PM 3 rev

Hello,

Can someone explain why:

This compiles and works without errors:

data:
      begin of ls_hotel_item_group,
        posnr       type accit-posnr,
        group_index type i,
        item_type   type cte_s_fin_post_entry_item_rel-item_type,
      end of ls_hotel_item_group,
      lt_hotel_item_group like sorted table of ls_hotel_item_group
        with unique key primary_key components posnr group_index.

    data(ls_document_data) = zcl_im_concur_fin_exp_add_data=>ms_document_data.

    data(lt_hotel_lodging_entry) =
      value cte_t_fin_post_entry( for ls_entry in ls_document_data-entry
                                    where ( expense_type_name = 'Hotel'
                                        and expense_type_code = 'LODNG' )
                                    ( ls_entry ) ).
    
    " Group by vendor code and bill number
    loop at lt_hotel_lodging_entry into data(ls_hotel_lodging_entry)
      group by ( vendor = conv cte_api_string( ls_hotel_lodging_entry-entry_custom6_code )
                 bill_number = conv cte_api_string( ls_hotel_lodging_entry-entry_custom36_code )
                 size = group size
                 index = group index ) ascending into data(ls_hotel_group_key).

      " Group expense entries...except EVD = empl vendor
      loop at group ls_hotel_group_key into ls_hotel_lodging_entry.
        loop at ct_accrel into data(ls_accrel) where entry_id = ls_hotel_lodging_entry-entry_id
                                                 and item_type <> 'EVD'.
          insert value #( posnr = ls_accrel-posnr
                          group_index = ls_hotel_group_key-index
                          item_type = ls_accrel-item_type ) into table lt_hotel_item_group.
          clear ls_accrel.
        endloop.
        clear ls_hotel_lodging_entry.
      endloop.
      clear ls_hotel_lodging_entry.
    endloop.

But this does not? - syntax errors: structures in an OO context must be compatible.

    lt_hotel_item_group =
      value #( for groups ls_hotel_group_key of ls_hotel_lodging_entry in lt_hotel_lodging_entry
                 group by ( vendor      = conv cte_api_string( ls_hotel_lodging_entry-entry_custom6_code )
                            bill_number = conv cte_api_string( ls_hotel_lodging_entry-entry_custom36_code )
                            size        = group size
                            index       = group index ) ascending
                 for <ls_hotel_lodging_entry> in group ls_hotel_group_key
                   for ls_accrel in ct_accrel
                     where ( entry_id  =  <ls_hotel_lodging_entry>-entry_id
                         and item_type <> 'EVD' )
                   ( posnr       = ls_accrel-posnr
                     group_index = ls_hotel_group_key-index
                     item_type   = ls_accrel-item_type ) ).