Friday, October 10, 2014

A SAP program with a selection screen, an ALV and a smartform

This is an example of a SAP program includes

  • A selection screen
  • An ALV
  • A smartform


Figure1. Selection screen

Figure2. ALV screen with Report button

Figure3. Final smartform output


This program has 3 main steps. First the user enter data into selection screen and execute. Then an alv shown due to the data inserted to the selection screen. Then the user can select rows from selection screen and click Report button. After that the smartform output shows according to the selected data of the ALV.
The program has three includes.

CODE

*&---------------------------------------------------------------------*
*&  Include           ZTHI_ALVSF_TOP
*&---------------------------------------------------------------------*
TABLESmara.
TYPE-POOLSslis.

TYPESBEGIN OF ty_mara,
        select TYPE c,
        matnr TYPE mara-matnr,
        ersda TYPE mara-ersda,
        ernam TYPE mara-ernam,
        aenam TYPE mara-aenam,
      END OF ty_mara.

TYPESBEGIN OF ty_makt,
         mandt TYPE mandt,
         matnr TYPE makt-matnr,
         maktx TYPE makt-maktx,
         maktg TYPE makt-maktg,
       END OF ty_makt.
TYPESBEGIN OF ty_matnr,
        matnr TYPE mara-matnr,
       END OF ty_matnr.

DATAit_mara TYPE STANDARD TABLE OF ty_mara,
      wa_mara TYPE ty_mara,
      it_makt TYPE ZTBL_TYMAKT,
      wa_makt TYPE ZTY_MAKT,
      it_matnr TYPE STANDARD TABLE OF ty_matnr.

DATAc_ccont TYPE REF TO cl_gui_custom_container,   "Custom container object
      c_alvgd         TYPE REF TO cl_gui_alv_grid,   "ALV grid object
      it_fcat            TYPE lvc_t_fcat,            "Field catalogue
      it_layout          TYPE lvc_s_layo.            "Layout

DATA lv_fldcat TYPE lvc_s_fcat.
DATAi_rows TYPE lvc_t_row.
DATArow LIKE LINE OF i_rows.
Data fm_name TYPE rs38l_fnam.


*&---------------------------------------------------------------------*
*&  Include           ZTHI_ALVSF_SEL
*&---------------------------------------------------------------------*

SELECTION-SCREEN BEGIN OF BLOCK parm.
  SELECT-OPTIONSp_matnr FOR mara-matnr.
  SELECT-OPTIONSp_ersda FOR mara-ersda.
  SELECT-OPTIONSp_ernam FOR mara-ernam.
  SELECT-OPTIONSp_aenam FOR mara-aenam.
SELECTION-SCREEN END OF BLOCK parm.

AT SELECTION-SCREEN.

START-OF-SELECTION.
PERFORM sel_alv_data.
*PERFORM build_fieldcatalog.
CALL SCREEN 0600.
*PERFORM dis_alv.


*&---------------------------------------------------------------------*
*&  Include           ZTHI_ALVSF_FUN
*&---------------------------------------------------------------------*
FORM sel_alv_data.
  SELECT FROM mara INTO CORRESPONDING FIELDS OF TABLE it_mara
    WHERE matnr IN p_matnr AND
    ersda IN p_ersda AND
    ernam IN p_ernam AND
    aenam IN p_aenam.
ENDFORM.

MODULE STATUS_0600 OUTPUT.
it_layout-SEL_MODE 'A'"multiple row selection mode

*MODULE alv_grid OUTPUT.
  CREATE OBJECT c_ccont
       EXPORTING
          container_name 'CC_CONT'.
  CREATE OBJECT c_alvgd
      EXPORTING
        i_parent c_ccont.
* Set field for ALV
  PERFORM alv_build_fieldcat.
* Set ALV attributes FOR LAYOUT
  PERFORM alv_report_layout.
  CHECK NOT c_alvgd IS INITIAL.
* Call ALV GRID
  CALL METHOD c_alvgd->set_table_for_first_display
    EXPORTING
      is_layout                     it_layout
    CHANGING
      it_outtab                     it_mara
      it_fieldcatalog               it_fcat
    EXCEPTIONS
      invalid_parameter_combination 1
      program_error                 2
      too_many_lines                3
      OTHERS                        4.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
               WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

*************************************************************

ENDMODULE.                 " ALV_GRID  OUTPUT


FORM alv_report_layout.
  it_layout-cwidth_opt 'X'.
  it_layout-zebra 'X'.
ENDFORM.


FORM alv_build_fieldcat.

  CLEAR lv_fldcat.

  lv_fldcat-row_pos   '1'.
  lv_fldcat-col_pos   '1'.
  lv_fldcat-fieldname 'MATNR'.
  lv_fldcat-tabname   'IT_OUTPUT'.
  lv_fldcat-outputlen 8.
  lv_fldcat-scrtext_m 'Material'.
  lv_fldcat-icon 'X'.
  APPEND lv_fldcat TO it_fcat.


  CLEAR lv_fldcat.

  lv_fldcat-row_pos   '1'.
  lv_fldcat-col_pos   '2'.
  lv_fldcat-fieldname 'ERSDA'.
  lv_fldcat-tabname   'IT_OUTPUT'.
  lv_fldcat-outputlen 8.
  lv_fldcat-scrtext_m 'Created On'.
  lv_fldcat-icon ''.
  APPEND lv_fldcat TO it_fcat.


 CLEAR lv_fldcat.

  lv_fldcat-row_pos   '1'.
  lv_fldcat-col_pos   '3'.
  lv_fldcat-fieldname 'ERNAM'.
  lv_fldcat-tabname   'IT_OUTPUT'.
  lv_fldcat-outputlen 8.
  lv_fldcat-scrtext_m 'Created Person'.
  lv_fldcat-icon ''.
  APPEND lv_fldcat TO it_fcat.


  CLEAR lv_fldcat.

  lv_fldcat-row_pos   '1'.
  lv_fldcat-col_pos   '4'.
  lv_fldcat-fieldname 'AENAM'.
  lv_fldcat-tabname   'IT_OUTPUT'.
  lv_fldcat-outputlen 8.
  lv_fldcat-scrtext_m 'Changed Person'.
  lv_fldcat-icon ''.
  APPEND lv_fldcat TO it_fcat.
ENDFORM.


MODULE USER_COMMAND_0600 INPUT.
  CASE sy-ucomm.
    WHEN 'RPT'.
      CALL METHOD c_alvgd->get_selected_rows
      IMPORTING
          ET_INDEX_ROWS i_rows.

      DATArs_selfield TYPE slis_selfield.
      LOOP AT i_rows INTO row.
        READ TABLE it_mara INTO wa_mara INDEX row-index.
        APPEND wa_mara-matnr TO it_matnr.
        CLEAR wa_mara.
      ENDLOOP.
      "MESSAGE row-matnr TYPE 'I'.
      PERFORM makt_data.
      PERFORM call_sf.
  ENDCASE.
ENDMODULE.

FORM makt_data.
  SELECT matnr maktx maktg FROM makt INTO CORRESPONDING FIELDS OF TABLE it_makt FOR ALL ENTRIES IN it_matnr
    WHERE matnr it_matnr-matnr.
ENDFORM.

form call_sf.

  CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
  EXPORTING
    formname                 'ZTHI_ALVSF_FUN_SF'
*   VARIANT                  = ' '
*   DIRECT_CALL              = ' '
 IMPORTING
   FM_NAME                  fm_name
 EXCEPTIONS
   NO_FORM                  1
   NO_FUNCTION_MODULE       2
   OTHERS                   3
          .
IF sy-subrc <> 0.
 MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.


  CALL FUNCTION fm_name
*   EXPORTING
*     ARCHIVE_INDEX              =
*     ARCHIVE_INDEX_TAB          =
*     ARCHIVE_PARAMETERS         =
*     CONTROL_PARAMETERS         =
*     MAIL_APPL_OBJ              =
*     MAIL_RECIPIENT             =
*     MAIL_SENDER                =
*     OUTPUT_OPTIONS             =
*     USER_SETTINGS              = 'X'
*   IMPORTING
*     DOCUMENT_OUTPUT_INFO       =
*     JOB_OUTPUT_INFO            =
*     JOB_OUTPUT_OPTIONS         =
    TABLES
      IT_MAKT                    it_makt
   EXCEPTIONS
     FORMATTING_ERROR           1
     INTERNAL_ERROR             2
     SEND_ERROR                 3
     USER_CANCELED              4
     OTHERS                     5
            .
  IF SY-SUBRC <> 0.
 MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.

ENDFORM.



Then added a screen (screen600) into the program. Following are the screen details.
Screen 600

PROCESS BEFORE OUTPUT.
 MODULE STATUS_0600.
*
PROCESS AFTER INPUT.
 MODULE USER_COMMAND_0600.


Using screen painter I added a custom control and a push button to the screen 600.

Figure4.Screen600

Set the properties of custom control and pushbutton as below.

Figure5.properties of custom control

Figure6. properties of push button


Now we are done with the program. Then smartform 'ZTHI_ALVSF_FUN_SF' is created as follow.

Create table IT_MAKT of type ZTBL_TYMAKT is created in the form interface as below.
Figure7


Create Global data WA_MAKT of type ZTY_MAKT sa below in the Global data.
Figure8


Then create a table in the main window and loop IT_MAKT into WA_MAKT in order to print
wa_makt-matnr, wa_makt-maktx and wa_makt-maktg as below.
Figure9



Finally, ZTY_MAKT is a global structure as shown below.
Figure10

And ZTBL_TYMAKT is a table type with ZTY_MAKT as the line type as shown below.
Figure11








No comments:

Post a Comment