Programming

ABAP/4 Example Code: ALV Grid Control by applying object CL_GUI_ALV_GRID

Create ZPROGRAM and create normal screen no. 100 under this program. And in screen 100, please create custom control name ’CUSTOM1’.

Example

*** ZPROGRAM CODING ***

DATA: save_ok LIKE sy-ucomm,
      ok_code LIKE sy-ucomm,
      it_spfli TYPE TABLE OF spfli WITH HEADER LINE.

DATA: G_CUSTOM_CONTAINER1 TYPE REF TO CL_GUI_CUSTOM_CONTAINER,
      GRID1  TYPE REF TO CL_GUI_ALV_GRID,
      it_cat TYPE LVC_T_FCAT,
      wa_cat TYPE LVC_S_FCAT.

START-OF-SELECTION.
  SELECT * FROM spfli
           INTO TABLE it_spfli.
  CALL SCREEN 100.

*&---------------------------------------------------------------------*
*&      Module  STATUS_0100  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE STATUS_0100 OUTPUT.
  SET PF-STATUS ’100’.
  SET TITLEBAR ’100’.
ENDMODULE.                 " STATUS_0100  OUTPUT
*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_0100  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE USER_COMMAND_0100 INPUT.
  save_ok = ok_code.
  clear ok_code.
  CASE save_ok.
    WHEN ’BACK’.
      LEAVE TO SCREEN 0.
  ENDCASE.
ENDMODULE.                 " USER_COMMAND_0100  INPUT
*&---------------------------------------------------------------------*
*&      Module  INITIAL_GRID  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE INITIAL_GRID OUTPUT.
  PERFORM create_field_catalog.
  PERFORM create_grid_control.
ENDMODULE.                 " INITIAL_GRID  OUTPUT
*&---------------------------------------------------------------------*
*&      Form  create_field_catalog
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM create_field_catalog .
  IF it_cat[] is initial.
    clear wa_cat.
    wa_cat-col_pos   = 1.
    wa_cat-fieldname = ’CARRID’.
    wa_cat-datatype  = ’CHAR’.
    wa_cat-inttype   = ’C’.
    wa_cat-intlen    = 3.
    wa_cat-reptext   = ’Airline Code’.
    wa_cat-coltext   = ’Airline Code’.
    append wa_cat to it_cat.

    clear wa_cat.
    wa_cat-col_pos   = 2.
    wa_cat-fieldname = ’CONNID’.
    wa_cat-datatype  = ’NUMC’.
    wa_cat-inttype   = ’N’.
    wa_cat-intlen    = 4.
    wa_cat-reptext   = ’Flight Conn. No’.
    wa_cat-coltext   = ’Flight Conn. No’.
    append wa_cat to it_cat.

    clear wa_cat.
    wa_cat-col_pos   = 3.
    wa_cat-fieldname = ’CITYFROM’.
    wa_cat-datatype  = ’CHAR’.
    wa_cat-inttype   = ’C’.
    wa_cat-intlen    = 20.
    wa_cat-reptext   = ’Dep. city’.
    wa_cat-coltext   = ’Dep. city’.
    append wa_cat to it_cat.

    clear wa_cat.
    wa_cat-col_pos   = 4.
    wa_cat-fieldname = ’AIRPFROM’.
    wa_cat-datatype  = ’CHAR’.
    wa_cat-inttype   = ’C’.
    wa_cat-intlen    = 3.
    wa_cat-reptext   = ’Dep. airport’.
    wa_cat-coltext   = ’Dep. airport’.
    append wa_cat to it_cat.

    clear wa_cat.
    wa_cat-col_pos   = 5.
    wa_cat-fieldname = ’CITYTO’.
    wa_cat-datatype  = ’CHAR’.
    wa_cat-inttype   = ’C’.
    wa_cat-intlen    = 20.
    wa_cat-reptext   = ’Arrival city’.
    wa_cat-coltext   = ’Arrival city’.
    append wa_cat to it_cat.

    clear wa_cat.
    wa_cat-col_pos   = 6.
    wa_cat-fieldname = ’AIRPTO’.
    wa_cat-datatype  = ’CHAR’.
    wa_cat-inttype   = ’C’.
    wa_cat-intlen    = 3.
    wa_cat-reptext   = ’Dest. airport’.
    wa_cat-coltext   = ’Dest. airport’.
    append wa_cat to it_cat.
  ENDIF.
ENDFORM.                    " create_field_catalog
*&---------------------------------------------------------------------*
*&      Form  create_grid_control
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM create_grid_control .
  IF G_CUSTOM_CONTAINER1 is initial .
    CREATE OBJECT G_CUSTOM_CONTAINER1
           EXPORTING CONTAINER_NAME = ’CUSTOM1’.
    CREATE OBJECT GRID1
           EXPORTING I_PARENT = G_CUSTOM_CONTAINER1.

    CALL METHOD GRID1->SET_TABLE_FOR_FIRST_DISPLAY
*      EXPORTING
*        I_BUFFER_ACTIVE               =
*        I_BYPASSING_BUFFER            =
*        I_CONSISTENCY_CHECK           =
*        I_STRUCTURE_NAME              =
*        IS_VARIANT                    =
*        I_SAVE                        =
*        I_DEFAULT                     = ’X’
*        IS_LAYOUT                     =
*        IS_PRINT                      =
*        IT_SPECIAL_GROUPS             =
*        IT_TOOLBAR_EXCLUDING          =
*        IT_HYPERLINK                  =
*        IT_ALV_GRAPHICS               =
*        IT_EXCEPT_QINFO               =
*        IR_SALV_ADAPTER               =
      CHANGING
        IT_OUTTAB                     = it_spfli[]
        IT_FIELDCATALOG               = it_cat[]
*        IT_SORT                       =
*        IT_FILTER                     =
      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.
  ENDIF.
ENDFORM.                    " create_grid_control
*** FLOW LOGIC ON SCREEN 100 ***
PROCESS BEFORE OUTPUT.
  MODULE STATUS_0100.
  MODULE INITIAL_GRID.
*
PROCESS AFTER INPUT.
  MODULE USER_COMMAND_0100.

How to insert variable in text element in SAP smartforms

After we insert text element in SAP smartforms, the inserted text element can contain variable. We will introduce step by step to insert variable in text element.

Step by Step

  • Double click text element that you want to insert variable in left tree navigator.
    img120
    In the example, i selected text element name ’TEXT1’.

How to insert text element in SAP Smartforms

You can insert text element in SAP smartforms as following:

  • Select window that you want to insert graphic object and right click. System will list object that you can create.
    img114
    In the example, I have window name ’HEADER2’ and I want to insert text element in this window.
  • On popup menu, you will select Create -> Text
    img115

    img116

How to insert graphics image (Bitmap) in SAP Smartforms

You can insert graphics object (Bitmap) in SAP smartforms as following:

  • Select window that you want to insert graphic object and right click. System will list object that you can create.
    img106
    In the example, I have window name ’HEADER’ and I want to insert graphic object in this window.
  • On popup menu, you will select Create -> Graphic
    img107

    img108

ABAP/4 Example Code: HIDE and AT LINE-SELECTION

Example

DATA: it_spfli TYPE TABLE OF spfli WITH HEADER LINE,
      it_sflight TYPE TABLE OF sflight WITH HEADER LINE.


START-OF-SELECTION.
  SELECT * FROM spfli
           INTO TABLE it_spfli.
  IF SY-SUBRC = 0.
    LOOP AT it_spfli.
      WRITE:/ it_spfli-CARRID, it_spfli-CONNID,
              it_spfli-CITYFROM, it_spfli-AIRPFROM,
              it_spfli-CITYTO, it_spfli-AIRPTO.
      HIDE: it_spfli-CARRID, it_spfli-CONNID.
    ENDLOOP.
  ENDIF.

AT LINE-SELECTION.
  SELECT * FROM sflight
           INTO TABLE it_sflight
           WHERE carrid = it_spfli-carrid
             AND connid = it_spfli-connid.
  IF SY-SUBRC = 0.
    FORMAT COLOR = 3.
    WRITE:/ it_spfli-CARRID, it_spfli-CONNID.
    FORMAT COLOR OFF.
    LOOP AT it_sflight.
      WRITE:/ it_sflight-FLDATE, it_sflight-PRICE.
    ENDLOOP.
  ENDIF.

How to activate ABAP Trace

You can find the table that used in your program by using ABAP trace ( Transaction ST05 ).

Step by Step

  • Go to transaction ST05
    img92
    Select trace type as SQL trace for trace SQL statement.
  • Activate Trace by select img93
  • Run your program that you want to list SQL statement.
  • After your program finished, back to transaction ST05 and select Deactivate Trace img94