Saturday, December 18, 2010
Fieldcatalog plays main role in ALV programming. Beginners when they are learning ALV, get struggle here. Fieldcatalog in simple terms is a internal table which contains descriptions of the list output fields(usually a subset of the internal output table fields).A field catalog is required for every ALV list output. 
They are many ways to populate fieldcatalog itab.
  1. ALV without fieldcatalog(Need to pass table structures. Internally fieldcatalog is generated.
    REPORT ZTEST_FIELDCAT1.
    "Without Fieldcatalog.
    DATA:I_MARD    TYPE MARD OCCURS 0 WITH HEADER LINE.
    DATA:L_PROGRAM TYPE SY-REPID VALUE SY-REPID.
    
    START-OF-SELECTION.
      "Get data from MARD
      SELECT * FROM MARD INTO TABLE I_MARD UP TO 100 ROWS.
      " Call ALV function module and structure name instead of F
      CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
        EXPORTING
          I_CALLBACK_PROGRAM = L_PROGRAM
          I_STRUCTURE_NAME   = 'MARD'
        TABLES
          T_OUTTAB           = I_MARD.
      IF SY-SUBRC <> 0.
        MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
                WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
      ENDIF.
    
  2. Automatic fieldcatalog generation -function module REUSE_ALV_FIELDCATALOG_MERGE.
    REPORT ZTEST_FIELDCAT2.
    "Automatic Fieldcatalog Generation.
    DATA:I_MARD TYPE MARD OCCURS 0 WITH HEADER LINE.
    TYPE-POOLS:SLIS.
    DATA:I_FIELDCAT TYPE SLIS_T_FIELDCAT_ALV.
    DATA L_PROGRAM TYPE SY-REPID VALUE SY-REPID.
    
    SELECT * FROM MARD INTO TABLE I_MARD UP TO 100 ROWS.
    "Use function module create  Fieldcat.
    CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
      EXPORTING
        I_PROGRAM_NAME     = L_PROGRAM
        I_STRUCTURE_NAME   = 'MARD'
        I_INCLNAME         = L_PROGRAM
        I_BYPASSING_BUFFER = 'X'
      CHANGING
        CT_FIELDCAT        = I_FIELDCAT.
    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 ALV Grid fm and pass fieldcatalog and data
    CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
      EXPORTING
        I_CALLBACK_PROGRAM = L_PROGRAM
        IT_FIELDCAT        = I_FIELDCAT
      TABLES
        T_OUTTAB           = I_MARD.
    IF SY-SUBRC <> 0.
      MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
              WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.
    
  3. Semi Automatic Fieldcatalog Generation - function module REUSE_ALV_FIELDCATALOG_MERGE
    REPORT  ZTEST_FIELDCAT3.
    "Semi Automatic Fieldcatalog Generation.
    DATA: BEGIN OF I_MARD OCCURS 0,
            MATNR LIKE MARD-MATNR,
            WERKS LIKE MARD-WERKS,
            LGORT LIKE MARD-LGORT,
            PSTAT LIKE MARD-PSTAT,
          END OF I_MARD.
    TYPE-POOLS:SLIS.
    DATA:I_FIELDCAT TYPE SLIS_T_FIELDCAT_ALV.
    
    SELECT MATNR
           WERKS
           LGORT
           PSTAT
      FROM MARD
      INTO CORRESPONDING FIELDS OF TABLE I_MARD
      UP TO 100 ROWS.
    "Use function module create  Fieldcat.
    DATA:L_PROGRAM TYPE SY-REPID VALUE SY-REPID.
    CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
      EXPORTING
        I_PROGRAM_NAME     = L_PROGRAM
        I_INTERNAL_TABNAME = 'I_MARD'
        I_INCLNAME         = L_PROGRAM
        I_BYPASSING_BUFFER = 'X'
      CHANGING
        CT_FIELDCAT        = I_FIELDCAT.
    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 ALV and pass fieldcatalog and data 
    CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
      EXPORTING
        I_CALLBACK_PROGRAM = L_PROGRAM
        IT_FIELDCAT        = I_FIELDCAT
      TABLES
        T_OUTTAB           = I_MARD.
    IF SY-SUBRC <> 0.
      MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
              WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.
    
  4. Manually populating fieldcatag table
    REPORT  ZTEST_FIELDCAT4.
    "Fieldcatalog creation manually
    DATA: BEGIN OF I_MARD OCCURS 0,
            MATNR TYPE MARD-MATNR,
            WERKS TYPE MARD-WERKS,
          END OF I_MARD.
    TYPE-POOLS:SLIS.
    DATA:I_FIELDCAT TYPE SLIS_T_FIELDCAT_ALV,
         W_FIELDCAT LIKE LINE OF I_FIELDCAT.
    DATA:L_PROGRAM TYPE SY-REPID VALUE SY-REPID.
    "Populate Fieldcatalog table
    W_FIELDCAT-FIELDNAME = 'MATNR'.
    W_FIELDCAT-TABNAME = 'I_MARD'.
    W_FIELDCAT-SELTEXT_M = 'Material No'.
    APPEND W_FIELDCAT TO I_FIELDCAT.
    CLEAR W_FIELDCAT.
    W_FIELDCAT-FIELDNAME = 'WERKS'.
    W_FIELDCAT-TABNAME = 'I_MARD'.
    W_FIELDCAT-SELTEXT_M = 'Plant'.
    APPEND W_FIELDCAT TO I_FIELDCAT.
    CLEAR W_FIELDCAT.
    
    SELECT MATNR WERKS FROM MARD INTO CORRESPONDING FIELDS OF TABLE I_MARD UP TO 100 ROWS.
    "Call ALV and pass fieldcatalog and data 
    CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
      EXPORTING
        I_CALLBACK_PROGRAM = L_PROGRAM
        IT_FIELDCAT        = I_FIELDCAT
      TABLES
        T_OUTTAB           = I_MARD.
    IF SY-SUBRC <> 0.
      MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
              WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.
    

1 comment:

  1. 1)What is the main purpose of using "Field Catalog".
    2)when we should use "Semi Automatic Fieldcatalog Generation"
    3)When we should use "Automatic fieldcatalog generation "
    4)can we use Field Catalog for Field Enhancement in ALV?

    ReplyDelete

Your useful comments, suggestions are appreciated.Your comments are moderated.

Followers

Contact Form

Name

Email *

Message *

Web Dynpro ABAP Book

An SAP Consultant

Follow US


Want to Contribute ?

If you are interested in writing about the new stuff you learn everyday while working, please write to the.sap.consultants@gmail.com.

Click on Contribution for more details.