Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
vladimir_erakovic
Contributor
0 Kudos
14,121

In this second document in the ABAP Beginners series, we will create ALV report for quantity of material in stock in the warehouse. We are using MARD table to see Valuated Unrestricted-Use Stock.


Tables that will be used are:

  1. MARD - Storage Location Data for Material
  2. MARA - General Material Data
  3. MAKT - Material Descriptions

As always, we are starting with declaration part:

REPORT  z_scn_mat.

* T A B L E S  U S E D
TABLES: mard,
        makt
,
        mara
.

* T Y P E - P O O L S
TYPE-POOLS: slis.
*----------------------------------------------------------------------*

* D A T A  D E C L A R A T I O N
DATA: BEGIN OF itab OCCURS 0,

matnr
LIKE mard-matnr,
maktx
LIKE makt-maktx,
labst
LIKE mard-labst,
meins
LIKE mara-meins.

DATA: END OF itab.

DATA rec LIKE itab.
DATA pom LIKE itab.

DATA: t_fieldcat            TYPE slis_t_fieldcat_alv,
      r_fieldcat           
TYPE slis_fieldcat_alv,
      z_layout             
TYPE slis_layout_alv,
      t_events             
TYPE slis_t_event,
      r_events             
LIKE LINE OF t_events,
      z_variant            
LIKE disvariant,
      z_event_exit         
TYPE slis_t_event_exit,
      z_repid              
LIKE sy-repid,
      z_callback_ucomm     
TYPE slis_formname,
      t_list_comments      
TYPE slis_t_listheader,
      r_list_comments      
LIKE LINE OF t_list_comments,
      z_exit_caused_by_user
TYPE slis_exit_by_user,
      izlaz                
TYPE c,
      answer               
TYPE c.

On selection screen we have the option to choose plant and storage location:

* S E L E C T I O N   S C R E E N   D E F I N I T I O N
SELECTION-SCREEN BEGIN OF BLOCK first WITH FRAME TITLE text-001.

SELECT-OPTIONS: s_werks FOR mard-werks MEMORY ID wrk
               
NO INTERVALS NO-EXSTENSION,
                s_lgort
FOR mard-lgort NO INTERVALS NO-EXSTENSION

.

SELECTION-SCREEN END OF BLOCK first.

After submitting selection screen the program will execute in this order:

************************************************************************
START-OF-SELECTION.
************************************************************************

* S E L E C T   D A T A
PERFORM data_select.

* M O D I F Y   D A T A
PERFORM data_modify.

* C R E A T E   F I E L D   C A T A L O G
PERFORM field_cat_create.

* C H A N G E   F I E L D   C A T A L O G
PERFORM field_cat_change.

* D I S P L A Y   A L V
PERFORM alv_display.

As you can see, first two subroutines are for data selection and manipulation and the last three are for alv display. Field catalog is like a blueprint on how the displayed table should look like. Let’s put them in that order:

*&---------------------------------------------------------------------*
*&      Form  data_select
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM data_select .

SELECT * INTO CORRESPONDING FIELDS OF TABLE itab
FROM mard
WHERE werks IN s_werks
   
AND lgort IN s_lgort.

ENDFORM.                    " DATA_SELECT

*&---------------------------------------------------------------------*
*&      Form  data_modify
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM data_modify .

LOOP AT itab INTO rec.

SELECT SINGLE maktx INTO rec-maktx
FROM makt
WHERE matnr = rec-matnr
AND spras = sy-langu.

SELECT SINGLE meins INTO rec-meins
FROM mara
WHERE matnr = rec-matnr.

MODIFY itab FROM rec.

ENDLOOP.

ENDFORM.                    " DATA_MODIFY

*&---------------------------------------------------------------------*
*&      Form  field_cat_create
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM field_cat_create .

CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
EXPORTING
i_program_name        
= sy-repid
i_internal_tabname    
= 'ITAB' "capital letters
i_inclname            
= sy-repid
CHANGING
ct_fieldcat           
= t_fieldcat
EXCEPTIONS
inconsistent_interface
= 1
program_error         
= 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.

ENDFORM.                    " FIELD_CAT_CREATE

*&---------------------------------------------------------------------*
*&      Form  field_cat_change
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM field_cat_change .

LOOP AT t_fieldcat INTO r_fieldcat.

CASE r_fieldcat-fieldname.
WHEN 'MAKTX'.
r_fieldcat
-reptext_ddic = 'Material name'.
r_fieldcat
-seltext_s = 'Mat.Name'.
r_fieldcat
-seltext_m = 'Material name'.
r_fieldcat
-seltext_l = 'Material name'.
r_fieldcat
-outputlen = 40.
MODIFY t_fieldcat FROM r_fieldcat.
WHEN 'LABST'.
r_fieldcat
-reptext_ddic = 'Quantity'.
r_fieldcat
-seltext_s = 'Quantity'.
r_fieldcat
-seltext_m = 'Quantity'.
r_fieldcat
-seltext_l = 'Quantity'.
r_fieldcat
-outputlen = 15.
MODIFY t_fieldcat FROM r_fieldcat.
WHEN 'MEINS'.
r_fieldcat
-reptext_ddic = 'Base Unit of Measure'.
r_fieldcat
-seltext_s = 'Unit'.
r_fieldcat
-seltext_m = 'Base Unit of Measure'.
r_fieldcat
-seltext_l = 'Base Unit of Measure'.
r_fieldcat
-outputlen = 8.
MODIFY t_fieldcat FROM r_fieldcat.
ENDCASE.
ENDLOOP.

ENDFORM.                    " FIELD_CAT_CHANGE

*&---------------------------------------------------------------------*
*&      Form  ALV_DISPLAY
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM alv_display .

* A L V   C U S T O M I Z A T I O N
z_callback_ucomm
= 'CALLBACK_UCOMM'.


*   D I S P L A Y   A L V   T A B L E
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_background_id            
= 'SIWB_WALLPAPER'
i_callback_program         
= sy-repid
*        i_callback_html_top_of_page = w_html_top_of_page
i_callback_top_of_page     
= 'ALV_TOP_OF_PAGE'
i_default                  
= 'X'
i_save                     
= 'A'
i_callback_user_command    
= 'USER_COMMAND'
it_fieldcat                
= t_fieldcat
it_events                  
= t_events
it_event_exit              
= z_event_exit
is_layout                  
= z_layout
is_variant                 
= z_variant
*      it_excluding                = i_excluding
*        is_print                    = w_print
IMPORTING
es_exit_caused_by_user       
= z_exit_caused_by_user
TABLES
t_outtab
= itab
EXCEPTIONS
program_error
= 1
OTHERS = 2.
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.                    " ALV_DISPLAY

Those were the main subroutines, but we create three more. One for popup on exit, one for top of alv page and one for user command.

*&---------------------------------------------------------------------*
*&      Form  alv_alertizlaz
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM alv_alertizlaz .

CALL FUNCTION 'POPUP_TO_CONFIRM_STEP'
EXPORTING
textline1     
= 'Exit from table view?'
titel         
= 'WARNING'
start_column  
= 40
start_row     
= 12
cancel_display
= ' '
defaultoption 
= 'Y'
IMPORTING
answer        
= answer.
IF answer = 'J' OR answer = 'Y'.
izlaz
= 'X'.
ELSE.
izlaz
= ' '.
ENDIF.

ENDFORM.                    " ALERTIZLAZ

*&---------------------------------------------------------------------*
*&      Form  alv_top_of_page
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM alv_top_of_page.

CLEAR: t_list_comments[].

* NASLOV IZVEŠTAJA
r_list_comments
-info = 'INVENTORY'.
r_list_comments
-typ  = 'H'.
r_list_comments
-key  = ''.
APPEND r_list_comments TO t_list_comments.

IF s_werks-low IS NOT INITIAL.
r_list_comments
-typ = 'S'.
r_list_comments
-key = 'Plant: '.
r_list_comments
-info = s_werks-low.
APPEND r_list_comments TO t_list_comments.
ENDIF.

IF s_lgort-low IS NOT INITIAL.
r_list_comments
-typ = 'S'.
r_list_comments
-key = 'Storage location: '.
r_list_comments
-info = s_lgort-low.
APPEND r_list_comments TO t_list_comments.
ENDIF.

CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
EXPORTING
it_list_commentary
= t_list_comments.

ENDFORM.                    "alv_top_of_page


* U S E R   C O M M A N D   F O R   A L V
FORM user_command  USING r_ucomm LIKE sy-ucomm
rs_selfield
TYPE slis_selfield.

CASE r_ucomm.
WHEN '&IC1'.
IF rs_selfield-fieldname = 'MATNR'.
*     Read data table, using index of row user clicked on
READ TABLE itab INTO pom INDEX rs_selfield-tabindex.
*     Set parameter ID for transaction screen field
SET PARAMETER ID 'MAT' FIELD pom-matnr.
*     Sxecute transaction ME23N, and skip initial data entry screen
CALL TRANSACTION 'MM03' AND SKIP FIRST SCREEN.
ENDIF.
ENDCASE.
ENDFORM.                    "callback_ucomm


You may notice that on the double click on MATNR field user goes to transaction MM03 for material display.

Create the transaction code and test it on client with data. It should look as on these screen shots:

In the next document, we will create dialog program with table control for filling two Z tables with one to many relationship.

Thank you for reading.

5 Comments
Labels in this area