Skip to Content
author's profile photo
Former Member

Problemas com subtotal - ALV

Boa tarde, estou batendo a cabeça tem dois dias em uma ALV até que eu encontrei um exemplo na internet de como fazer um subtotal de um determinado "registro" no meu caso estou usando a tabela VBAK, eu antes de utilzar o exemplo fui atrás para saber o que é maioria das funções, porem estou tendo alguns problemas para funcionar.

O código que funciona é o seguinte:


REPORT  zprogramateste.

TABLES: vbak.

DATA:
      it_vbak TYPE TABLE OF vbak,
      wa_vbak TYPE vbak,

      r_container TYPE REF TO cl_gui_custom_container,
      r_grid TYPE REF TO cl_gui_alv_grid,

      t_fcat TYPE lvc_t_fcat,
      fs_fcat TYPE lvc_s_fcat.

DATA:
      t_sort  TYPE lvc_t_sort,
      fs_sort TYPE lvc_s_sort.

fs_fcat-fieldname = 'NETWR'.
fs_fcat-do_sum = 'X'.
APPEND fs_fcat TO t_fcat.

fs_sort-spos ='1'.
fs_sort-fieldname = 'VBELN'.
fs_sort-up = 'X'.
fs_sort-group = '*'.
fs_sort-subtot = 'X'.
APPEND fs_sort TO t_sort.

SELECTION-SCREEN: BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.

SELECT-OPTIONS: p_vbeln FOR vbak-vbeln NO-EXTENSION NO INTERVALS. " Empresa

SELECTION-SCREEN: END OF BLOCK b1.

START-OF-SELECTION.

  PERFORM: seleciona_dados.

  CALL SCREEN 0100.

END-OF-SELECTION.

FORM seleciona_dados .

  SELECT * FROM vbak INTO TABLE it_vbak WHERE vbeln IN p_vbeln.

ENDFORM.                    " SELECIONA_DADOS

MODULE z_status OUTPUT.
  SET PF-STATUS 'FF0100'.
  SET TITLEBAR  'TB0100'.
ENDMODULE.                    "z_status OUTPUT

MODULE z_user_command INPUT.
  IF sy-dynnr EQ '0100'.
    CASE sy-ucomm.
      WHEN 'BACK' OR
           'CANC' OR
           'EXIT'  .
        LEAVE TO SCREEN 0. "ELE RETORNA PARA A TELA QUE CHAMOU.
    ENDCASE.
  ENDIF.
ENDMODULE.                 " Z_USER_COMMAND  INPUT


MODULE list OUTPUT.
  CREATE OBJECT r_container
    EXPORTING
      container_name = 'CONTAINER'.
  CREATE OBJECT r_grid
    EXPORTING
      i_parent = r_container.
  CALL METHOD r_grid->set_table_for_first_display
    EXPORTING
      i_structure_name = 'VBAK'
    CHANGING
      it_fieldcatalog  = t_fcat
      it_outtab        = it_vbak
      it_sort          = t_sort.
ENDMODULE. "list OUTPUT




Edited by: victorhsn on May 4, 2011 8:20 PM

Edited by: victorhsn on May 4, 2011 8:21 PM

Add comment
10|10000 characters needed characters exceeded

3 Answers

  • Best Answer
    author's profile photo
    Former Member
    Posted on May 04, 2011 at 07:39 PM

    Boa tarde Victor,

    Seguindo sua linha de programação, olha o código acima....

    Você não precisa incluir nenhum outro atributo, apenas acrescentar suas colunas na tabela f_cat e retirar o parâmetro citado ( EXPORTING i_structure_name = 'VBAK'). Através da f_cat você pode customizar suas colunas. O t_sort pode continuar da mesma forma.

    Espero ter ajudado.

    Ahhhhh cuidado que troquei o número da tela e o nome do container, motivo, somente para reaproveitar um programa de teste já tinha criado. rsrsrs

    Att,

    Eduardo Da Rós

    Consultor SAP ABAP

    Edited by: Eduardo Da Rós on May 4, 2011 9:40 PM

    Add comment
    10|10000 characters needed characters exceeded

  • author's profile photo
    Former Member
    Posted on May 04, 2011 at 06:24 PM

    E o seguinte código que eu estou tentando fazer funcionar, porem ele dá um erro.

    Agora o código que não funciona.

    *&---------------------------------------------------------------------*
    *& Report  ZPROGRAMATESTE
    *&
    *&---------------------------------------------------------------------*
    *&
    *&
    *&---------------------------------------------------------------------*
    
    REPORT  zprogramateste.
    
    TABLES: vbak.
    
    TYPES:
           BEGIN OF ty_vbak,
             vbeln TYPE vbak-vbeln,
             erdat TYPE vbak-erdat,
             vkorg TYPE vbak-vkorg,
             netwr TYPE vbak-netwr,
           END OF ty_vbak,
    
           BEGIN OF ty_saida,
             vbeln TYPE vbak-vbeln,
             erdat TYPE vbak-erdat,
             vkorg TYPE vbak-vkorg,
             netwr TYPE vbak-netwr,
           END OF ty_saida.
    
    DATA:
          it_vbak TYPE TABLE OF ty_vbak,
          wa_vbak TYPE ty_vbak,
    
          it_saida TYPE TABLE OF ty_saida,
          wa_saida TYPE ty_saida,
    
    
    
          r_container TYPE REF TO cl_gui_custom_container,
          r_grid TYPE REF TO cl_gui_alv_grid,
    
          t_fcat TYPE lvc_t_fcat,
          fs_fcat TYPE lvc_s_fcat.
    
    DATA:
          t_sort  TYPE lvc_t_sort,
          fs_sort TYPE lvc_s_sort.
    
    fs_fcat-fieldname = 'NETWR'.
    fs_fcat-do_sum = 'X'.
    APPEND fs_fcat TO t_fcat.
    
    fs_sort-spos ='1'.
    fs_sort-fieldname = 'VBELN'.
    fs_sort-down = 'X'.
    fs_sort-group = '*'.
    fs_sort-subtot = 'X'.
    APPEND fs_sort TO t_sort.
    
    
    *&---------------------------------------------------------------------*
    *& TELA DE SELEÇÃO
    *&---------------------------------------------------------------------*
    SELECTION-SCREEN: BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
    
    SELECT-OPTIONS: p_vbeln FOR vbak-vbeln NO-EXTENSION NO INTERVALS. " Empresa
    
    SELECTION-SCREEN: END OF BLOCK b1.
    
    START-OF-SELECTION.
    
      PERFORM: seleciona_dados,
               f_saida.
    
      CALL SCREEN 0100.
    
    END-OF-SELECTION.
    *&---------------------------------------------------------------------*
    *&      Form  SELECIONA_DADOS
    *&---------------------------------------------------------------------*
    *       text
    *----------------------------------------------------------------------*
    *  -->  p1        text
    *  <--  p2        text
    *----------------------------------------------------------------------*
    FORM seleciona_dados .
    
      SELECT vbeln erdat vkorg netwr
         FROM vbak
         INTO TABLE it_vbak
         WHERE vbeln IN p_vbeln.
    
    ENDFORM.                    " SELECIONA_DADOS
    
    *&---------------------------------------------------------------------*
    *&      Form  SELECIONA_DADOS
    *&---------------------------------------------------------------------*
    *       text
    *----------------------------------------------------------------------*
    *  -->  p1        text
    *  <--  p2        text
    *----------------------------------------------------------------------*
    FORM f_saida.
    
      SORT:
           it_vbak BY vbeln.
    
    
      LOOP AT it_vbak INTO wa_vbak.
    
        wa_saida-vbeln = wa_vbak-vbeln.
        wa_saida-erdat = wa_vbak-erdat.
        wa_saida-vkorg = wa_vbak-vkorg.
    
        APPEND wa_saida TO it_saida.
    
        CLEAR: wa_vbak.
    
      ENDLOOP.
    
    ENDFORM.                    " SELECIONA_DADOS
    
    
    *&---------------------------------------------------------------------*
    *&      Module  Z_STATUS  OUTPUT
    *&---------------------------------------------------------------------*
    *       text
    *----------------------------------------------------------------------*
    MODULE z_status OUTPUT.
      SET PF-STATUS 'FF0100'.
      SET TITLEBAR  'TB0100'.
    ENDMODULE.                    "z_status OUTPUT
    
    *&---------------------------------------------------------------------*
    *&      Module  Z_USER_COMMAND  INPUT
    *&---------------------------------------------------------------------*
    *       text
    *----------------------------------------------------------------------*
    MODULE z_user_command INPUT.
      IF sy-dynnr EQ '0100'.
        CASE sy-ucomm.
          WHEN 'BACK' OR
               'CANC' OR
               'EXIT'  .
            LEAVE TO SCREEN 0. "ELE RETORNA PARA A TELA QUE CHAMOU.
        ENDCASE.
      ENDIF.
    ENDMODULE.                 " Z_USER_COMMAND  INPUT
    
    *&---------------------------------------------------------------------
    **& Module HANDLER OUTPUT
    *&---------------------------------------------------------------------
    
    MODULE list OUTPUT.
      CREATE OBJECT r_container
        EXPORTING
          container_name = 'CONTAINER'.
      CREATE OBJECT r_grid
        EXPORTING
          i_parent = r_container.
      CALL METHOD r_grid->set_table_for_first_display
        EXPORTING
          i_structure_name = 'VBAK'
        CHANGING
          it_fieldcatalog  = t_fcat
          it_outtab        = it_vbak
          it_sort          = t_sort.
    ENDMODULE. "list OUTPUT
    
    

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      E ele me dá um erro falando

      Erro tpo.exec. ITAB_ILLEGAL_COMPONENT

      Data e hora 04.05.2011 14:00:21

      O que aconteceu ?

      Error in the ABAP Application Program

      The current ABAP program "SAPLSKBH" had to be terminated because it has

      come across a statement that unfortunately cannot be executed.

      In the current ABAP program "SAPLSKBH" the internal table "RT_COLLECT01[]"

      to be

      processed with "SORT".

      At the same time, a component "WAERK " was dynamically specified as content

      of the field "L_UFIELDNAME".

      One of the following errors has occurred:

      - The row type "TY_VBAK" of the internal table "RT_COLLECT01[]" does not

      contain a

      component "WAERK ".

      - "WAERK " enthielt eine unzulässige Offset-/Längenangabe, z.B.

      wenn versucht wurde, in eine Komponente vom Typ I oder

      STRING hineinzugreifen, oder beim Versuch, über die

      Grenzen einer Komponente mit elementarem Typ hinauszugreifen.

      - The program "SAPLSKBH" is a Unicode program. In Unicode programs

      an offset/length access to a structure is only permitted within the

      character-type beginning section of the structure. In the case of "WAERK

      the access extends beyond this beginning section.

      - "WAERK " contains a specification of the form "A->B" for a

      dynamic access to an object attribute.

      This type of attribute access is only permitted statically at this

      point in time.

      - "WAERK " contains significant closing spaces (only possible for the type

      STRING).

      Ponto acionamento do erro em tempo de execução

      Programa SAPLSKBH

      Include LSKBHF20

      Linha 1.040

      Tipo de módulo (FORM)

      Nome do módulo FB_SUBTOTALS_GET

      Não sei porque ficou bagunçando quando eu postei mais de código, por isso postei assim para ficar mais claro o código, fico grato pela a ajuda de todos.

      Grato.

  • author's profile photo
    Former Member
    Posted on May 04, 2011 at 06:54 PM

    Boa tarde Victor,

    No método set_table_for_first_display, você passou i_structure_name = 'VBAK', mas agora você está usando o seu tipo, não o standard, comenta essa linha e cria um fieldcat para seu tipo.

    Fiz isso e funcionou!

    Att,

    Eduardo Da Rós.

    Consultor SAP ABAP

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member Former Member

      *&----


      *

      *& Report ZPROGRAMATESTE

      *&

      *&----


      *

      *&

      *&

      *&----


      *

      REPORT zprogramateste.

      TABLES: vbak.

      TYPES:

      BEGIN OF ty_vbak,

      vbeln TYPE vbak-vbeln,

      erdat TYPE vbak-erdat,

      vkorg TYPE vbak-vkorg,

      netwr TYPE vbak-netwr,

      END OF ty_vbak,

      BEGIN OF ty_saida,

      vbeln TYPE vbak-vbeln,

      erdat TYPE vbak-erdat,

      vkorg TYPE vbak-vkorg,

      netwr TYPE vbak-netwr,

      END OF ty_saida.

      DATA:

      it_vbak TYPE TABLE OF ty_vbak,

      wa_vbak TYPE ty_vbak,

      it_saida TYPE TABLE OF ty_saida,

      wa_saida TYPE ty_saida,

      r_container TYPE REF TO cl_gui_custom_container,

      r_grid TYPE REF TO cl_gui_alv_grid,

      t_fcat TYPE lvc_t_fcat,

      fs_fcat TYPE lvc_s_fcat.

      DATA:

      t_sort TYPE lvc_t_sort,

      fs_sort TYPE lvc_s_sort.

      *&----


      *

      *& TELA DE SELEÇÃO

      *&----


      *

      SELECTION-SCREEN: BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.

      SELECT-OPTIONS: p_vbeln FOR vbak-vbeln NO-EXTENSION NO INTERVALS. " Empresa

      SELECTION-SCREEN: END OF BLOCK b1.

      START-OF-SELECTION.

      PERFORM: seleciona_dados,

      f_saida.

      CALL SCREEN 9000.

      *&----


      *

      *& Form SELECIONA_DADOS

      *&----


      *

      • text

      *----


      *

      • --> p1 text

      • <-- p2 text

      *----


      *

      FORM seleciona_dados .

      SELECT vbeln erdat vkorg netwr

      FROM vbak

      INTO TABLE it_vbak

      WHERE vbeln IN p_vbeln.

      ENDFORM. " SELECIONA_DADOS

      *&----


      *

      *& Form SELECIONA_DADOS

      *&----


      *

      • text

      *----


      *

      • --> p1 text

      • <-- p2 text

      *----


      *

      FORM f_saida.

      SORT:

      it_vbak BY vbeln.

      LOOP AT it_vbak INTO wa_vbak.

      wa_saida-vbeln = wa_vbak-vbeln.

      wa_saida-erdat = wa_vbak-erdat.

      wa_saida-vkorg = wa_vbak-vkorg.

      APPEND wa_saida TO it_saida.

      CLEAR: wa_vbak.

      ENDLOOP.

      ENDFORM. " SELECIONA_DADOS

      *&----

      -


      **& Module HANDLER OUTPUT

      *&----

      -


      MODULE list OUTPUT.

      fs_fcat-fieldname = 'VBELN'.

      fs_fcat-do_sum = ' '.

      APPEND fs_fcat TO t_fcat.

      fs_fcat-fieldname = 'ERDAT'.

      fs_fcat-do_sum = ' '.

      APPEND fs_fcat TO t_fcat.

      fs_fcat-fieldname = 'VKORG'.

      fs_fcat-do_sum = ' '.

      APPEND fs_fcat TO t_fcat.

      fs_fcat-fieldname = 'NETWR'.

      fs_fcat-do_sum = 'X'.

      APPEND fs_fcat TO t_fcat.

      fs_sort-spos ='1'.

      fs_sort-fieldname = 'VBELN'.

      fs_sort-down = 'X'.

      fs_sort-group = '*'.

      fs_sort-subtot = 'X'.

      APPEND fs_sort TO t_sort.

      CREATE OBJECT r_container

      EXPORTING

      container_name = 'CONT_ALV'.

      CREATE OBJECT r_grid

      EXPORTING

      i_parent = r_container.

      CALL METHOD r_grid->set_table_for_first_display

      CHANGING

      it_fieldcatalog = t_fcat

      it_outtab = it_vbak

      it_sort = t_sort.

      ENDMODULE. "list OUTPUT