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 mardmatnr,
maktx
LIKE maktmaktx,
labst
LIKE mardlabst,
meins
LIKE marameins.

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 syrepid,
      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 text001.

SELECT-OPTIONS: s_werks FOR mardwerks MEMORY ID wrk
               
NO INTERVALS NO-EXSTENSION,
                s_lgort
FOR mardlgort 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 recmaktx
FROM makt
WHERE matnr = recmatnr
AND spras = sylangu.

SELECT SINGLE meins INTO recmeins
FROM mara
WHERE matnr = recmatnr.

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        
= syrepid
i_internal_tabname    
= ‘ITAB’ “capital letters
i_inclname            
= syrepid
CHANGING
ct_fieldcat           
= t_fieldcat
EXCEPTIONS
inconsistent_interface
= 1
program_error         
= 2
OTHERS                 = 3.

IF sysubrc <> 0.
MESSAGE ID symsgid TYPE symsgty NUMBER symsgno
WITH symsgv1 symsgv2 symsgv3 symsgv4.
ENDIF.

ENDFORM.                    ” FIELD_CAT_CREATE

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

LOOP AT t_fieldcat INTO r_fieldcat.

CASE r_fieldcatfieldname.
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         
= syrepid
*        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 sysubrc <> 0.
MESSAGE ID symsgid TYPE symsgty NUMBER symsgno
WITH symsgv1 symsgv2 symsgv3 symsgv4.
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_werkslow IS NOT INITIAL.
r_list_comments
typ = ‘S’.
r_list_comments
key = ‘Plant: ‘.
r_list_comments
info = s_werkslow.
APPEND r_list_comments TO t_list_comments.
ENDIF.

IF s_lgortlow IS NOT INITIAL.
r_list_comments
typ = ‘S’.
r_list_comments
key = ‘Storage location: ‘.
r_list_comments
info = s_lgortlow.
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 syucomm
rs_selfield
TYPE slis_selfield.

CASE r_ucomm.
WHEN ‘&IC1’.
IF rs_selfieldfieldname = ‘MATNR’.
*     Read data table, using index of row user clicked on
READ TABLE itab INTO pom INDEX rs_selfieldtabindex.
*     Set parameter ID for transaction screen field
SET PARAMETER ID ‘MAT’ FIELD pommatnr.
*     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:

/wp-content/uploads/2013/10/alv_311500.jpg

/wp-content/uploads/2013/10/alv2_311630.jpg

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.

To report this post you need to login first.

5 Comments

You must be Logged on to comment or reply to a post.

  1. Sander Boers

    Hi Vladimir,

    I’m wondering, learning freshers a ‘simple’ report with an ALV, that’s a good idea.

    But declarations with ‘TABLES’, ‘LIKE’ and OCCURS instead of a decent STANDARD TABLE OF, is not the 2013 way to ABAP, I guess?

    And why not use – as sample – the CL_SALV_TABLE=>FACTORY instance method to create a fancy ALV class? All the labour to create a full blown field category, that’s to old school, don’t you agree?

    Best,

    Sander

    (0) 
    1. Vladimir Erakovic Post author

      Hi Sander,

      You are absolutely right but imagine this scenario. Young programmer without any experience in ABAP start to work in a company where his colleagues and mentors are ‘old school abapers’ and almost all of the z programs and reports in company system are written in that obsolete way. He doesn’t have much choice but first to learn procedural ABAP and obsolete statements and declarations. Then, when he start being comfortable with ABAP, he can explore and learn ABAP Objects and modern ABAP and to pay attention to performance. This scenario was my journey also.

      I planned to make a document in this series with OO ALV and some more complicated user interfaces and latter some dialog programs with modern UI elements such as enjoy controls (trees, pictures, docking containers).

      Either way, thanks for your feedback 🙂

      Best regards,

      Vladimir

      (0) 
      1. Custodio de Oliveira

        Hi Vladimir,

        I’ll have to back Sander’s opinion here. If we assume your scenario, we’ll need to write documents on BDC? Sorry, but I don’t think so.

        Besides, It’s very likely this content already exists, it’s only a matter of searching.

        Regards,

        Custodio

        (0) 
      2. Sander Boers

        In short, yes, the young programmer should be able to read this kind of code. Database cursors, occurs, infotypes, pooltables, header lines, work areas and all that old stuff.

        But for samples on how to program, he should start writing methods and classes. And in the new way, without CALL METHOD, but in the shortest notation functional notation. In this way the first ‘A’ in ABAP can still stand for ‘Advanced’ and he or she doesn’t have to spend his/her time on field catalogues etc.

        (0) 
        1. Vladimir Erakovic Post author

          Interesting discussion 🙂

          Yes, you are both right, but as you can see there are still many questions/discussions on ABAP Development space regarding this ‘old stuff’. These documents were intended for those who ask that kind of questions.

          Ok, you convinced me to write about some advanced techniques.

          Regards

          (0) 

Leave a Reply