Tutorials

Posted by Krishh Webworld | 12:56 PM | | 0 comments »

SALV Table (2D) Dispaly

1. SALV Table Display - Normal (2D) Table Display
Shows how to use the CL_SALV_TABLE class to generate a new ALV instance. Also contains the Base program used as the example.

2. SALV Table Display - Add default PF status
Shows how to add the default PF status in the ALV. This contains the Code snippet as ADD-ON as well as the downloadable example program file.

3. SALV Table Display - Add custom PF status
Shows how to add the Custom PF status in the ALV. Also shows the easy way to create a custom pf status for ALV

4. SALV Table Display - Set Layout
Shows how to set the Layout for the ALV.

5. SALV Table Display - Add Header & Footer
Shows how to add the Header (top of page) and Footer (End of Page) in the ALV. It also shows how to bring that information in the Print out.

6. SALV Table Display - Display Settings
Shows how to set the Zebra type display, Title of the ALV using the SALV model.

7. SALV Table Display - Column Properties
Shows how to individual column properties of the columns as well as the all columns properties.

8. SALV Table Display - Add & Handle Hotspot
Shows how to add hotspot for specific column and handle it using the event handler.

9. SALV Table Display - Apply Colors to List
Shows how to apply colors at - Specific Cell, Specific Column and Specific Row level.

10. SALV Table Display - Editable SALV Model (Overcome the restriction of SALV Model)
Shows how to make the SALV model ALV editable. It overcomes the restriction of SALV model.




SALV Hierarchical (Sequential) List Dispaly

1. SALV Hierarchical Table - Simple List Display
Shows how to use the CL_SALV_HIERSEQ_TABLE class to generate a new ALV instance for the Hierarchical List display. Sometimes, we call this as a Sequential List. This post contains the Base program used as the example.

2. SALV Hierarchical Table - Adding Default PF Status
Shows how add default PF status in the Hierarchical ALV generated using the CL_SALV_HIERSEQ_TABLE.

3. SALV Hierarchical Table - Add Expand/Collapse Option
Shows how add the option to Expand or Collapse the List in the Hierarchical ALV generated using the CL_SALV_HIERSEQ_TABLE.




Dynamic Internal Table

1. Dynamic Internal Table Creation
Shows how to create a dynamic Internal table with help of Run Time Type Services (RTTS). This post also shows how to select the data into dynamic internal table, modify the table and generate output.

2. Dynamic Internal Table with Deep structure
Shows how to create a dynamic Internal table with Deep Structure with help of Run Time Type Services (RTTS).

3. Dynamic Internal table creation using CL_ALV_TABLE_CREATE
Shows how to create a dynamic Internal table with help of class CL_ALV_TABLE_CREATE from the fieldcatalog.




Table Maintenance

Capture Changed Contents after FM VIEW_MAINTENANCE_CALL
Describes how to caputre changed data after the Maintenance FM call.

Add Custom GUI Button in Table Maintenance (SM30)
Describes the steps to add the GUI button on the Table Maintenance when we maintain the data using transaction SM30.

Add Custom Button on Maintianence View (SM30)

Posted by Krishh Webworld | 12:45 PM | , , | 0 comments »

Whenever we generate the table maintianence, SAP generates some new code and use the Shared code for the Table maintainence generator. Since long time, I was wondering how can I add my own custom PF buttons in the table maintainence screen which comes in SM30. Today, this discussion in the SDN form raised this point in my mind again - Adding Custon button on Maintainance View.

I have started searching the GUI status in the Function Group which I have provided in the Table maintainence generator, but I didn't find it in the object browser (SE80) of the Function group. So, I thought this must be taken care by the Events in the Table maintainence. I searched the list and I found suitable one - ST (GUI Menu Main Program Name). Follow this link for mor information on Table maintenace events - Extended Table Maintenance Events

To add a custom button we need to follow couple of steps:
1. Create Table Maintainence.
Obviously today's discussion is based on the Table maintaince, we must have that before we start it. For a information purpose, I will attach a screen shot of the Table maintaince.


2. Copy user Interface of program SAPLSVIM to our FG
We will copy all the user interface of the main table maintainence program to our Fuction Group. Table maintainence runtime uses the user interface from the program SAPLSVIM. So, we are unable to find any user interface in our FG.


There will be one popup of language compatability, which you can pass it on by pressing Enter.

3. Add Custom Button in the PF-Status
Here we need to identify the proper PF-Status from all the statues. We will use the EULG pf-status which is being called when you enter in SM30 with Change mode. We will add the button with code "0POPUP" We can simply check which pf-status is being used in the each screen of the SM30 by going System > Status > GUI status. We will activate the changed status.

4. Add Event ST in the Table modification Events
Open the table maintainence generator screen and follow: Environment -> Modification -> Events. Select ST from the values and enter the FG main program name. For this example SAPLZTEST_TABLES.


5. Add PAI module in the Screen
Now, we need to create a new PAI module in the Screen flow. We will create the module 0CUSTOM_PF_BUTTON in the Screen. We will create this module in a New Custom include by selecting it from the popup.

In this code snippet, I am showing how to handle our user command. Here I am just giving information popup when user interact with the button in the change mode.

Code Snippet for Handling custom button
  *----------------------------------------------------------------------*
***INCLUDE LZTEST_TABLESI01 .

*----------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& Module CUSTOM_PF_BUTTON INPUT
*&---------------------------------------------------------------------*
* Custom Button Handling
* Here TOTAL table is avliable with all the data.
* User command is stored in the field FUNCTION
*----------------------------------------------------------------------*
MODULE 0custom_pf_button INPUT.
*
DATA: l_count TYPE i.
* Table of the same structure of the TOTAL which can be exported
DATA: BEGIN OF itab OCCURS 0.
INCLUDE STRUCTURE ztest_pf_status.
INCLUDE STRUCTURE vimflagtab.
DATA: END OF itab.
*
CASE function.
WHEN '0POPUP'.
itab[] = total[].
*
DELETE itab WHERE mark IS INITIAL.
DESCRIBE TABLE itab LINES l_count.

*
CALL FUNCTION 'POPUP_TO_INFORM'
EXPORTING
titel = 'Information'
txt1 = 'Selected number of entries'
txt2 = l_count.
*
CLEAR l_count.
*
ENDCASE.
*
ENDMODULE. " CUSTOM_PF_BUTTON INPUT


Don't forget to activate the Function Group, since we have created a new include in it.

6. Custom button on the SM30
Here is the screenshot of the SM30 with our new added custom button.

After pressing button, it will react like this:

Posted by Krishh Webworld | 12:43 PM | , , | 0 comments »

The requirement is to capture the changed contents – updated, deleted or inserted – after the table maintenance call using the FM VIEW_MAINTENANCE_CALL.

The simple solution to handle this requirement is:
1. Get all the data from the database table for which we will call the FM VIEW_MAINTENANCE_CALL.
2. Call FM VIEW_MAINTENANCE_CALL
3. Get all the data from the database table again in separate internal table Compare the contents of the data using some utility.

This kind of generic solution would be possible, but the performance of the report would be slow. I thought of having something which can tell us directly just after the FM call, which line was changed: update or deleted or inserted. Suddenly, I thought of implementing the Event and use the same data which are there in the global tables of the Maintenance Function.

Based on the thought, new steps to achieve this solution would be:
1. Implement Event in the Maintenance. Export the Main internal table to ABAP memory, which can be accessed later from the calling program.
2. Call the FM VIEW_MAINTENANCE_CALL
3. Import the data from the ABAP memory

In this later approach, we can get the data instantly from the memory. Since we will have the indicators (ACTION field in the table) which will help us to identify the type of change made to table entries.

You can find more information on the Events of the Table Maintenance at: Extended Table Maintenance Events

To implement the solution, I have used the Event 01 - Before Saving the data into Database. We need to create the subroutine which can be called when the event gets hit. When we create a subroutine, it will be created in the same Function group in which we have created the Table mainteinance. Hence, it will provide us the access to all the global tabbles. From all of these tables, we will export the internal table TOTAL to the memory.

TOTAL table contains the fields: ACTION and MARK.

Content of the ACTION field would help us to indetify the type of change:
D – Delete
U – Update
N – New Entry



Code Snippet to Export data in Event 01
 
*&---------------------------------------------------------------------*
*& Form EXPORT_TOTAL_TABLE
*&---------------------------------------------------------------------*
* This form is called in the Event 01 of the table maintainence
* events
*----------------------------------------------------------------------*
FORM EXPORT_TOTAL_TABLE.
*
* Table of the same structure of the TOTAL which can be exported
DATA: BEGIN OF ITAB OCCURS 0.
INCLUDE STRUCTURE ZTEST_NP_EVENT.
INCLUDE STRUCTURE VIMFLAGTAB.
DATA: END OF ITAB.
*
* Moving data to ITAB from total
ITAB[] = TOTAL[].
*
* Clearing memory
FREE MEMORY ID 'ZTEST_EVENT'.
*
* Exporting data to Memory
EXPORT ITAB = ITAB TO MEMORY ID 'ZTEST_EVENT'.
*
ENDFORM. "EXPORT_TOTAL_TABLE



Code Snippet of the Report
 
*&---------------------------------------------------------------------*
*& Report ZTEST_NP_EVENT
*&---------------------------------------------------------------------*
*& This Report will start the Table Maintainence and after saving the
*& data in the table maintinence, it will generate a list which will
*& what data has been newly added, updated or deleted
*&---------------------------------------------------------------------*
REPORT ZTEST_NP_EVENT.
*
* Table with the Same structure of the table which is exported.
DATA: BEGIN OF ITAB OCCURS 0.
INCLUDE STRUCTURE ZTEST_NP_EVENT.
INCLUDE STRUCTURE VIMFLAGTAB.
DATA: END OF ITAB.
*
* Output table
TYPES: BEGIN OF TY_OUT.
INCLUDE TYPE ZTEST_NP_EVENT.
INCLUDE TYPE VIMFLAGTAB.
TYPES: END OF TY_OUT.
*
DATA: IT_OUTPUT TYPE STANDARD TABLE OF TY_OUT.
*
* Output ALV reference
DATA: O_ALV TYPE REF TO CL_SALV_TABLE.
*
*
START-OF-SELECTION.
* Start the Table maintainence
CALL FUNCTION 'VIEW_MAINTENANCE_CALL'
EXPORTING
ACTION = 'U'
VIEW_NAME = 'ZTEST_NP_EVENT'
EXCEPTIONS
CLIENT_REFERENCE = 1
FOREIGN_LOCK = 2
INVALID_ACTION = 3
NO_CLIENTINDEPENDENT_AUTH = 4
NO_DATABASE_FUNCTION = 5
NO_EDITOR_FUNCTION = 6
NO_SHOW_AUTH = 7
NO_TVDIR_ENTRY = 8
NO_UPD_AUTH = 9
ONLY_SHOW_ALLOWED = 10
SYSTEM_FAILURE = 11
UNKNOWN_FIELD_IN_DBA_SELLIST = 12
VIEW_NOT_FOUND = 13
MAINTENANCE_PROHIBITED = 14
OTHERS = 15.
IF SY-SUBRC <> 0.
ENDIF.
*
* Importing the memory from the Memory ID
* Table has been exported in the Event 01 of the Table
* maintenance events.
IMPORT ITAB TO ITAB FROM MEMORY ID 'ZTEST_EVENT'.
*
* Clear the memory id
FREE MEMORY ID 'ZTEST_EVENT'.
*
* Moving data to output
IT_OUTPUT = ITAB[].
DELETE IT_OUTPUT WHERE ACTION IS INITIAL.
*
* New ALV Instance
TRY.
CL_SALV_TABLE=>FACTORY(
EXPORTING
LIST_DISPLAY = ABAP_FALSE
IMPORTING
R_SALV_TABLE = O_ALV
CHANGING
T_TABLE = IT_OUTPUT ).
CATCH CX_SALV_MSG. "#EC NO_HANDLER
ENDTRY.
*
* Display the ALV
O_ALV->DISPLAY( ).

Dynamic Internal Table Creation using class CL_ALV_TABLE_CREATE

Posted by Krishh Webworld | 12:40 PM | | 0 comments »

Describes how to show the Dynamic Internal Table Creation using class CL_ALV_TABLE_CREATE


In the post Dynamic Internal Table creation and Dynamic Internal Table with Deep Structure, we have seen how we can use the RTTS - Run Time Type Services to create dynamic internal table.

Today we will eloborate another way of creating the dynamic internal table. We will see how we can use the class CL_ALV_TABLE_CREATE to create a dynamic internal table for ALV.

Basic pricipal would be, we need to fill the field catalog table and pass it to static method CREATE_DYNAMIC_TABLE from class CL_ALV_TABLE_CREATE.

We will use the same example as the reference in the post Dynamic Internal Table creation.

Here is the code snippet:
Code Snippet to Display ALV output on the Selection Screen

*&---------------------------------------------------------------------*
*& Report ZTEST_NP_DYN
*& This Code snippet shows how to
*& Create Dynamic Internal Table using the fieldcatalog and
*& class CL_ALV_TABLE_CREATE
*& Dynamic Selection of data
*& Accessing Dynamic data selection
*& Displaying Dynamic internal table in ALV
*&---------------------------------------------------------------------*
*
REPORT ztest_np_dyn.
*
DATA: lt_fieldcat TYPE lvc_t_fcat,
la_fieldcat TYPE lvc_s_fcat,
lo_table TYPE REF TO data.
*
DATA: lf_months TYPE monat,
lf_run_mon TYPE monat.
*
* Dynamic Selection fields
TYPES: BEGIN OF ty_fields,
field TYPE char30,
END OF ty_fields.
*
DATA: lt_fields TYPE STANDARD TABLE OF ty_fields,
la_fields TYPE ty_fields.
*
* field symbols to access the dynamic table
FIELD-SYMBOLS: TYPE ANY TABLE,
TYPE ANY,
TYPE ANY.
*
* Selection Screen
PARAMETERS: p_mon_fr TYPE monat,
p_mon_to TYPE monat.
*
*
START-OF-SELECTION.
* 1. Adding fields in the field catalog
la_fieldcat-fieldname = 'KSTAR'.
la_fieldcat-datatype = 'CHAR'.
la_fieldcat-outputlen = 10.
APPEND la_fieldcat TO lt_fieldcat.
CLEAR la_fieldcat.
*
* 2. Adding required fields based on the selection months
* Determining Number of fields
lf_months = ( p_mon_to - p_mon_fr ) + 1.
lf_run_mon = p_mon_fr.
*
DO lf_months TIMES.
* Field name
CONCATENATE 'WTG0' lf_run_mon INTO la_fieldcat-fieldname.
la_fieldcat-datatype = 'CURR'.
la_fieldcat-outputlen = 17.
*
* Filling the fieldcatalog
APPEND la_fieldcat TO lt_fieldcat.
CLEAR la_fieldcat.
*
lf_run_mon = lf_run_mon + 1.
ENDDO.
*
* Calling method to generate the dynamic internal table based on
* ALV field catalog
cl_alv_table_create=>create_dynamic_table(
EXPORTING
it_fieldcatalog = lt_fieldcat
IMPORTING
ep_table = lo_table ).
*
ASSIGN lo_table->* TO .
*
*$*$*...............Dynamic Selection.............................*$*$*
* Filling up the table for the Selection fields of Select Query
LOOP AT lt_fieldcat INTO la_fieldcat.
la_fields-field = la_fieldcat-fieldname.
APPEND la_fields TO lt_fields.
CLEAR: la_fieldcat, la_fields.
ENDLOOP.
*
* Selecting data
SELECT (lt_fields)
INTO TABLE
FROM cosp
UP TO 10 ROWS.
*
*$*$*...............Accessing dynamic table.......................*$*$*
LOOP AT ASSIGNING .
ASSIGN COMPONENT 'WTG004' OF STRUCTURE TO .
= '100.00'.
ENDLOOP.
*
*$*$*...............Displaying using SALV model...................*$*$*
DATA: lo_alv TYPE REF TO cl_salv_table.
*
TRY.
cl_salv_table=>factory(
EXPORTING
list_display = abap_false
IMPORTING
r_salv_table = lo_alv
CHANGING
t_table = ).
CATCH cx_salv_msg .
ENDTRY.
*
lo_alv->display( ).


I would suggest to use the RTTS to create dynamic internal table as depicted in posts:
  • Dynamic Internal Table Creation
  • Dynamic Internal Table with Deep structure
  • Dynamic Internal Table with Deep structure

    Posted by Krishh Webworld | 12:37 PM | | 0 comments »

    We have seen how we can use Run Time Type Services (RTTS) to create a dynamic internal table in the post: Dynamic Internal Table Creation. On the other day, after publishing the post SALV Table 9 - Apply Colors, I was thinking how I can apply these color settings when I have a Dynamic Internal Table. As we have seen into that post, we require an internal table for Color in our Output table means a dynamic internal table with deep structure.

    Dyanmic internal table with deep structure is specially useful when we have to create an ALV with Colors or Style etc. Since we have CL_ABAP_TABLEDESCR class from RTTS, it is also possible to create a dynamic internal table with the deep structure. To create an internal table with deep structure, we need to first get the object definition of the table type. Than we have to add that as a component of our table definition and that's it.

    This code snippet is in the continuation with the code given in the post Dynamic Internal Table Creation. You can put this code just after the DO..ENDO and before 3. Create a New Type.

    Code Snippet to add the deep component in Dynamic Itab
     
    *&---------------------------------------------------------------------*
    *& Report ZNP_DYNAMIC_DEEP_TABLE.
    *&
    *&---------------------------------------------------------------------*
    *& Shows how to create dynamic internal table with deep strcuture
    *& which can be used in the ALV to have Sytle table, Color table
    *&
    *&---------------------------------------------------------------------*
    *
    *
    * 2.1 Creating the Deep strcuture field
    DATA: lo_tab TYPE REF TO cl_abap_tabledescr.
    *
    lo_tab ?= cl_abap_typedescr=>describe_by_name( 'LVC_T_SCOL' ).
    la_comp-name = 'COLOR_TAB'.
    la_comp-type = lo_tab.
    APPEND la_comp TO lt_tot_comp.
    CLEAR: la_comp.
    *

    Dynamic Internal Table Creation

    Posted by Krishh Webworld | 12:35 PM | , , | 1 comments »

    Shows how to use the RTTS to create a dynamic internal table


    From the ABAP release 6.40, SAP has provided RTTS - Run Time Type Services to create types, internal tables at run-time. This RTTS can also be used to describe the properties of the types as well as the fields, internal tables etc.

    Sometimes, when we write a program, we don't have all the information of the fields of the internal table. For example: we are accessing the cost element Actul posting data from the table COSP. Now, we have a requirement to generate an output which will have some specified columns - like amount of period 4 to 8 or Amount of period 1 to 3 or some other combination. This kind of secnarioes are perfect examples of the RTTS. We will see how to create a dynamic internal table using this example.

    To create a dynamic internal table, we need to:
    1. Gather all the Components
    2. Generate a Type from this components
    3. Generate a Table Type from this created type
    4. Create a Data reference of this Table Type
    5. Assign this data reference to the Field-Symbol of table type. This Field-symbol will act as our dyanmic internal table

    In today's example we will see how to:
    1. Create Dynamic internal table
    2. Write dynamic select query to data into this dynamic table
    3. Change the contents of this dynamic internal table
    4. Generate the ALV display using SALV model for this dynamic internal table. Explore how to create ALV using SALV from Tutorials > SALV Table

    We will provide the selection of the period for which user wants to generate an output. Based on the entered periods we will create a dynamic type containg the KSTAR (Costing Element) and Amount fields for the month. After creating the dynamic type, we will create a dynamic table type. Using this table type we will create a reference of the data. From this data reference we will assign the internal table to field-symbols.

    So, let's see the code snippet:
    Code Snippet to create Dynamic ITAB
      
    *&---------------------------------------------------------------------*
    *& This Code snippet shows how to
    *& Create Dynamic Internal Table
    *& Dynamic Selection of data
    *& Accessing Dynamic data selection
    *& Displaying Dynamic internal table in ALV
    *&---------------------------------------------------------------------*
    report zdynamic_itab.
    *
    * Exisiting Table type
    TYPES: BEGIN OF ty_kstar,
    kstar TYPE kstar,
    END OF ty_kstar.
    *
    * Dynamic Table creation
    DATA: lo_struct TYPE REF TO cl_abap_structdescr,
    lo_element TYPE REF TO cl_abap_elemdescr,
    lo_new_type TYPE REF TO cl_abap_structdescr,
    lo_new_tab TYPE REF TO cl_abap_tabledescr,
    lo_data TYPE REF TO data,
    lt_comp TYPE cl_abap_structdescr=>component_table,
    lt_tot_comp TYPE cl_abap_structdescr=>component_table,
    la_comp LIKE LINE OF lt_comp,
    lf_months TYPE monat,
    lf_run_mon TYPE monat.
    *
    * Dynamic Selection fields
    TYPES: BEGIN OF ty_fields,
    field TYPE char30,
    END OF ty_fields.
    *
    DATA: lt_fields TYPE STANDARD TABLE OF ty_fields,
    la_fields TYPE ty_fields.
    *
    * field symbols to access the dynamic table
    FIELD-SYMBOLS: TYPE ANY TABLE,
    TYPE ANY,
    TYPE ANY.
    *
    * Selection Screen
    PARAMETERS: p_mon_fr TYPE monat,
    p_mon_to TYPE monat.
    *
    START-OF-SELECTION.
    *
    *$*$*...............Dynamic Internal Table........................*$*$*
    * 1. Getting Compoents from existing type
    lo_struct ?= cl_abap_typedescr=>describe_by_name( 'TY_KSTAR' ).
    lt_comp = lo_struct->get_components( ).
    APPEND LINES OF lt_comp TO lt_tot_comp.
    *
    * 2. Adding required fields based on the single data element
    * Determining Number of fields
    lf_months = ( p_mon_to - p_mon_fr ) + 1.
    lf_run_mon = p_mon_fr.
    *
    DO lf_months TIMES.
    *
    * Element Description
    lo_element ?= cl_abap_elemdescr=>describe_by_name( 'WTGXXX' ).
    *
    * Field name
    CONCATENATE 'WTG0' lf_run_mon INTO la_comp-name.
    *
    * Field type
    la_comp-type = cl_abap_elemdescr=>get_p(
    p_length = lo_element->length
    p_decimals = lo_element->decimals ).
    *
    * Filling the component table
    APPEND la_comp TO lt_tot_comp.
    CLEAR: la_comp.
    *
    lf_run_mon = lf_run_mon + 1.
    ENDDO.
    *
    * 3. Create a New Type
    lo_new_type = cl_abap_structdescr=>create( lt_tot_comp ).
    *
    * 4. New Table type
    lo_new_tab = cl_abap_tabledescr=>create(
    p_line_type = lo_new_type
    p_table_kind = cl_abap_tabledescr=>tablekind_std
    p_unique = abap_false ).
    *
    * 5. data to handle the new table type
    CREATE DATA lo_data TYPE HANDLE lo_new_tab.
    *
    * 6. New internal table in the fieldsymbols
    ASSIGN lo_data->* TO .
    *
    *$*$*...............Dynamic Selection.............................*$*$*
    * Filling up the table for the Selection fields of Select Query
    LOOP AT lt_tot_comp INTO la_comp.
    la_fields-field = la_comp-name.
    APPEND la_fields TO lt_fields.
    CLEAR: la_comp, la_fields.
    ENDLOOP.
    *
    * Selecting data
    SELECT (lt_fields)
    INTO TABLE
    FROM cosp
    UP TO 10 ROWS.
    *
    *$*$*...............Accessing dynamic table.......................*$*$*
    LOOP AT ASSIGNING .
    ASSIGN COMPONENT 'WTG004' OF STRUCTURE TO .
    = '100.00'.
    ENDLOOP.
    *
    *
    *$*$*...............Displaying using SALV model...................*$*$*
    *
    DATA: lo_alv TYPE REF TO cl_salv_table.
    *
    TRY.
    cl_salv_table=>factory(
    EXPORTING
    list_display = abap_false
    IMPORTING
    r_salv_table = lo_alv
    CHANGING
    t_table = ).
    CATCH cx_salv_msg .
    ENDTRY.
    *
    lo_alv->display( ).

    SALV Hierarchical Table 3 - Add Expand/Collapse Option

    Posted by Krishh Webworld | 12:31 PM | , , | 0 comments »

    Today in the series of the SALV Hiearchical list display, we will see how to add the Expand/Collapse button in the Hierarchical ALV. All discussion related to the Hierarchical table display can be found under Tutorials > SALV HS List.

    Expand/Collapse button will provide an option to users to only see the Header List. By selecting the expand button, it will show both Header and detail List. By selecting the same button again it will collapse the list and gives only the header list.

    Here is the code Snippet which provides the ADD-ON code. This ADD-ON code can be replaced with the relevent section from this code snippet in the base program. You can find the base program code snippet in the post SALV Hierarchical Table 1 : Simple table display. Adding code to base program is like adding the code correction from the OSS Note. Additionally, we will always include the default PF-status in all the code snippet.

    UML diagram for the application would be like:

    Code snippet to get the Expand / Collapse option in Hierarchical ALV.

    Code Snippet to get Expand / Collapse option
     *$*$*.....CODE_ADD_1 - Begin..................................1..*$*$*
    *
    * In this section we will define the private methods which can
    * be implemented to set the properties of the ALV and can be
    * called in the GENERATE_OUTPUT method
    *
    PRIVATE SECTION.
    METHODS:
    set_default_pf
    CHANGING

    co_hs_alv TYPE REF TO cl_salv_hierseq_table.
    *
    METHODS:
    set_expand_option
    CHANGING
    co_hs_alv TYPE REF TO cl_salv_hierseq_table.
    *$*$*.....CODE_ADD_1 - End....................................1..*$*$*
    *
    *
    *$*$*.....CODE_ADD_2 - Begin..................................2..*$*$*
    *

    * In this area we will call the methods which will set the
    * different properties to the ALV
    *
    * Default PF status
    CALL METHOD me->set_default_pf
    CHANGING
    co_hs_alv = o_hs_alv.
    *
    * Expand Option
    CALL METHOD me->set_expand_option
    CHANGING

    co_hs_alv = o_hs_alv.
    *$*$*.....CODE_ADD_2 - End....................................2..*$*$*
    *
    *
    *$*$*.....CODE_ADD_3 - Begin..................................3..*$*$*
    *
    * In this area we will implement the methods which are defined in
    * the class definition
    *
    METHOD set_default_pf.
    DATA: lo_functions TYPE REF TO cl_salv_functions_list.

    lo_functions = co_hs_alv->get_functions( ).
    lo_functions->set_all( abap_true ).
    ENDMETHOD. "set_default_pf
    *
    METHOD set_expand_option.
    *
    DATA:
    lo_columns TYPE REF TO cl_salv_columns_hierseq.
    *
    * Get the Columns of the Master
    TRY.
    lo_columns = co_hs_alv->get_columns( 1 ).
    CATCH cx_salv_not_found.
    ENDTRY.
    *
    * set expand column
    TRY.
    lo_columns->set_expand_column( 'EXPAND' ).
    CATCH cx_salv_data_error. "#EC NO_HANDLER
    ENDTRY.
    *
    DATA: lo_level TYPE REF TO cl_salv_hierseq_level.
    *
    * Set items expanded by default
    TRY.
    lo_level = co_hs_alv->get_level( 1 ).
    lo_level->set_items_expanded( ).
    CATCH cx_salv_not_found.
    ENDTRY.
    *
    ENDMETHOD. "set_expand_option
    *$*$*.....CODE_ADD_3 - End....................................3..*$*$*



    This code will generate output like:

    SALV Hierarchical Table 2 - Adding Default PF Status

    Posted by Krishh Webworld | 11:45 AM | , , | 0 comments »

    Today we will discuss how easy it is to add a Default PF status. In the previous post, we have discussed how to create a simple hierarchical table: SALV Hierarchical Table 1 - Simple table display. All discussion related to hierarchical (sequential) table can be found under Tutorials > SALV HS List.

    To add a default PF status in the hierarchial (sequential) list created using the reference of the class CL_SALV_HIERSEQ_TABLE, we need to get the all the default PF status functions using the method GET_FUNCTIONS of the reference of the CL_SALV_HIERSEQ_TABLE.

    Here is the code Snippet which provides the ADD-ON code. This ADD-ON code can be replaced with the relevent section from this code snippet in the base program. You can find the base program code snippet in the post SALV Hierarchical Table 1 : Simple table display. Adding code to base program is like adding the code correction from the OSS Note.

    UML diagram of the our test program would be like:
    Code snippet to add the default pf status is as follows:


    Code Snippet
      
    *$*$*.....CODE_ADD_1 - Begin..................................1..*$*$*
    *
    * In this section we will define the private methods which can

    * be implemented to set the properties of the ALV and can be
    * called in the GENERATE_OUTPUT method
    *
    PRIVATE SECTION.
    METHODS:
    set_default_pf
    CHANGING
    co_hs_alv TYPE REF TO cl_salv_hierseq_table.
    *$*$*.....CODE_ADD_1 - End....................................1..*$*$*
    *

    *
    *$*$*.....CODE_ADD_2 - Begin..................................2..*$*$*
    *
    * In this area we will call the methods which will set the
    * different properties to the ALV
    *
    CALL METHOD me->set_default_pf
    CHANGING
    co_hs_alv = o_hs_alv.
    *$*$*.....CODE_ADD_2 - End....................................2..*$*$*
    *
    *

    *$*$*.....CODE_ADD_3 - Begin..................................3..*$*$*
    *
    * In this area we will implement the methods which are defined in
    * the class definition
    *
    METHOD set_default_pf.
    DATA: lo_functions TYPE REF TO cl_salv_functions_list.
    lo_functions = co_hs_alv->get_functions( ).
    lo_functions->set_all( abap_true ).
    ENDMETHOD. "set_default_pf
    *$*$*.....CODE_ADD_3 - End....................................3..*$*$*


    This code add-on will generate the default pf status like this:

    SALV Hierarchical Table 1 - Simple table display

    Posted by Krishh Webworld | 11:40 AM | , | 0 comments »

    The new model of ALV - SALV Model - also provides the interface for application developer to develop the Hierarchical list. This type of list are also known as the Sequential List.

    We use the hierarchical-sequential table to display simple hierarchical structures. In addition, exactly two hierarchy levels are available for the use: the header level and the position level. For each hierarchy level, we have to specify a table and also the key columns, like foreign keys.

    We can use the model by using the reference of the calss CL_SALV_HIERSEQ_TABLE. We can use this class instead of the function module REUSE_ALV_HIERSEQ_LIST_DISPLAY.
    Class CL_SALV_HIERSEQ_TABLE has a FACTORY method. We will call this method and ask for the object for the Hierarchical Table. After getting the HS (Hierarchical - Sequential) Object, we will call the DISPLAY method to generate a list on the screen. Isn't it sound too simple? Infact, it is simple.

    You can visit the here to know more about the SALV models: SAP List Viewer : New Programming Model --> https://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/5dc3e690-0201-0010-1ebf-b85b3bed962d
    Check out the Code snippet for the simplest Hierarchical Table:

    Code Snippet to generate Hierarchical List
      
    *&---------------------------------------------------------------------*
    *& This code snippet will show how to use the CL_SALV_HIERSEQ_TABLE to
    *& generate the simplest Hierarchical (Sequential) ALV

    *&---------------------------------------------------------------------*
    REPORT zsalv_hs_simple.
    *
    *----------------------------------------------------------------------*
    * CLASS lcl_report DEFINITION
    *----------------------------------------------------------------------*
    CLASS lcl_report DEFINITION.
    *
    PUBLIC SECTION.
    *
    * Final Header output table
    TYPES: BEGIN OF ty_vbak,
    vbeln TYPE vbak-vbeln,
    erdat TYPE erdat,

    auart TYPE auart,
    kunnr TYPE kunnr,
    expand TYPE char01, "Column for Expand / Collapse
    END OF ty_vbak.
    *
    * FInal Item output table
    TYPES: BEGIN OF ty_vbap,
    vbeln TYPE vbap-vbeln,
    posnr TYPE vbap-posnr,
    matnr TYPE vbap-matnr,

    arktx TYPE vbap-arktx,
    END OF ty_vbap.
    *
    * Standard internal tables
    DATA: t_vbak TYPE STANDARD TABLE OF ty_vbak,
    t_vbap TYPE STANDARD TABLE OF ty_vbap.
    *
    * Hierarchical ALV reference
    DATA: o_hs_alv TYPE REF TO cl_salv_hierseq_table.
    *
    METHODS:
    * data selection
    get_data,
    *
    * Generating output
    generate_output.
    *
    *$*$*.....CODE_ADD_1 - Begin..................................1..*$*$*
    *
    * In this section we will define the private methods which can
    * be implemented to set the properties of the ALV and can be
    * called in the GENERATE_OUTPUT method
    *
    *$*$*.....CODE_ADD_1 - End....................................1..*$*$*

    *
    ENDCLASS. "lcl_report DEFINITION
    *
    *
    START-OF-SELECTION.
    DATA: lo_report TYPE REF TO lcl_report.
    *
    CREATE OBJECT lo_report.
    *
    lo_report->get_data( ).
    *
    lo_report->generate_output( ).
    *
    *----------------------------------------------------------------------*
    * CLASS lcl_report IMPLEMENTATION
    *----------------------------------------------------------------------*
    CLASS lcl_report IMPLEMENTATION.
    *
    METHOD get_data.
    * data selection - Header
    SELECT vbeln erdat auart kunnr
    INTO TABLE t_vbak

    FROM vbak
    UP TO 10 ROWS.
    *
    * data selection - Item
    SELECT vbeln posnr matnr arktx
    INTO TABLE t_vbap
    FROM vbap
    FOR ALL ENTRIES IN t_vbak
    WHERE vbeln = t_vbak-vbeln.
    *
    ENDMETHOD. "get_data
    *
    *.......................................................................

    METHOD generate_output.
    * New ALV instance
    * We are calling the static Factory method which will give back
    * the ALV object reference.
    *
    DATA: lx_data_err TYPE REF TO cx_salv_data_error,
    lx_not_found TYPE REF TO cx_salv_not_found.
    *
    * Fill the Binding table. Here we hae to provide the relationship
    * between all the Common Key fields in the Master and Sl
    ave
    * table. Based on this relationship, we will get the output.

    DATA: lt_bind TYPE salv_t_hierseq_binding,
    la_bind LIKE LINE OF lt_bind.
    *
    la_bind-master = 'VBELN'. " VBELN as field of my T_VBAK
    la_bind-slave = 'VBELN'. " VBELN as field of my T_VBAP
    APPEND la_bind TO lt_bind.

    *
    * call factory method to generate the output
    TRY.
    CALL METHOD cl_salv_hierseq_table=>factory
    EXPORTING

    t_binding_level1_level2 = lt_bind
    IMPORTING
    r_hierseq = o_hs_alv
    CHANGING
    t_table_level1 = t_vbak
    t_table_level2 = t_vbap.

    CATCH cx_salv_data_error INTO lx_data_err.
    CATCH cx_salv_not_found INTO lx_not_found.
    ENDTRY.
    *
    *$*$*.....CODE_ADD_2 - Begin..................................2..*$*$*
    *

    * In this area we will call the methods which will set the
    * different properties to the ALV
    *
    *$*$*.....CODE_ADD_2 - End....................................2..*$*$*

    *
    * Displaying the ALV
    * Here we will call the DISPLAY method to get the output on the screen
    o_hs_alv->display( ).
    *
    ENDMETHOD. "generate_output
    *
    *$*$*.....CODE_ADD_3 - Begin..................................3..*$*$*

    *
    * In this area we will implement the methods which are defined in
    * the class definition
    *

    *$*$*.....CODE_ADD_3 - End....................................3..*$*$*
    *
    ENDCLASS. "lcl_report IMPLEMENTATION


    This code will generate an output like this:
    Note: I had tried to build this report in the Object Oriented manner. You can check the UML diagram as below. This desgin will provide us the great flexibility to add more methods will set and get different properties of the O_HS_ALV object. For example, if want to add the expand & collapse button, we will add one private method SET_EXPAND_OPTION. We will implement this method by adding the relative code and call this method before the list display.


    In the code snippet, I have tried to differential three section which can be changed easily. In next blog posts, I will use this program as the base program and provide the code snippet to add the new functionality. Like: Section of CODE_ADD_1 can be replaced with the code provided in the future blog post.

    SALV Table 10 - Editable SALV Model (Overcome the restriction of SALV Model)

    Posted by Krishh Webworld | 11:36 AM | | 0 comments »

    This is one of my published blog on the SAP Community Network --> https://www.sdn.sap.com/irj/scn.

    In this blog, I described a way to overcome the restrictions of SALV models.

    Follow:
    Power of ABAP Objects: Overcome the Restrictions of SALV Model --> https://www.sdn.sap.com/irj/scn/weblogs?blog=/pub/wlg/12187

    This is only possible because of the power of the ABAP objects which gives us the access to the protected attributes in the derived child class.

    Warning: Here I am trying to overcome the restriction and accessing the SALV attributes which SAP has not intented to expose to outside world and it made it protected. Read Thomas Jung's warning below the post.

    SALV Table 9 - Apply Colors

    Posted by Krishh Webworld | 11:31 AM | , , | 2 comments »

    In the series of the SALV Model Table display, today we will see how to apply colors to the ALV Grid. In this regards, we will see how to apply colors to the Specific Cell, Specific Row or Specific Column. You can find all the Previous discussion at Tutorials > SALV Table Display.

    Color plays a very important role in the formatting. It will help us to generate the a rich list which can help users to notice the exceptional data - for example, negative salary to employee or Low material availabilty.

    At most colors can be applied to three levels:

    1. Perticular Cell: To apply color at perticular cell, we need to add the details about the field and color in COLOR table at each record. Than we have to set this Color Table in the object of the COLUMNS which contains the information about all our information (CL_SALV_COLUMNS_TABLE).
    2. Entire Row: To apply color to Entire row, we have to do the same thing as the applying the color to Perticular Cell, but we will not specify the FIELDNAME. This way system will understand that it has to apply the color to entire row.
    3. Entire Column: To apply color to Entire column, we have to get the specific Column from the COLUMNS object and than we need to set the Color Property of the Specific Column and we are done.


    In this example I need to change the output table sturcture as compared to previous discussion in this series, so I will provide the entire code which generate the output which has all three scenario. For test purpose, We will apply: Red color to Sales Doc Type in 3rd Row, Green Color to 5th row, Yellow color to Created on Column.

    UML Diagram for this application is like:
    Code snippet to generate the ALV with Colors using the SALV model.
    Code Snippet to Generate ALV with Colors
      
    *&---------------------------------------------------------------------*
    *& This code snippet will show how to use the CL_SALV_TABLE to
    *& generate the ALV and COLORs for Column, Row and Specific Cell
    *&---------------------------------------------------------------------*
    REPORT ztest_oo_alv_color.
    *
    *----------------------------------------------------------------------*

    * CLASS lcl_report DEFINITION
    *----------------------------------------------------------------------*
    CLASS lcl_report DEFINITION.
    *
    PUBLIC SECTION.
    *
    * Final output table
    TYPES: BEGIN OF ty_vbak,
    vbeln TYPE vbak-vbeln,
    erdat TYPE erdat,
    auart TYPE auart,

    kunnr TYPE kunnr,
    t_color TYPE lvc_t_scol,
    END OF ty_vbak.
    TYPES: ty_t_vbak TYPE STANDARD TABLE OF ty_vbak.
    *
    DATA: t_vbak TYPE STANDARD TABLE OF ty_vbak.
    *
    * ALV reference
    DATA: o_alv TYPE REF TO cl_salv_table.
    *
    METHODS:
    * data selection
    get_data,
    *
    * Generating output
    generate_output.
    *
    *$*$*.....CODE_ADD_1 - Begin..................................1..*$*$*
    *
    * In this section we will define the private methods which can
    * be implemented to set the properties of the ALV and can be
    * called in the
    *

    PRIVATE SECTION.
    METHODS:
    set_pf_status
    CHANGING
    co_alv TYPE REF TO cl_salv_table.
    *
    METHODS:
    set_colors
    CHANGING
    co_alv TYPE REF TO cl_salv_table
    ct_vbak TYPE ty_t_vbak.
    *
    *$*$*.....CODE_ADD_1 - End....................................1..*$*$*
    *
    ENDCLASS. "lcl_report DEFINITION
    *
    *
    START-OF-SELECTION.
    DATA: lo_report TYPE REF TO lcl_report.
    *
    CREATE OBJECT lo_report.
    *
    lo_report->get_data( ).
    *
    lo_report->generate_output( ).
    *
    *----------------------------------------------------------------------*
    * CLASS lcl_report IMPLEMENTATION
    *----------------------------------------------------------------------*
    CLASS lcl_report IMPLEMENTATION.
    *
    METHOD get_data.
    * data selection
    SELECT vbeln erdat auart kunnr
    INTO CORRESPONDING FIELDS OF TABLE t_vbak
    FROM vbak

    UP TO 20 ROWS.
    *
    ENDMETHOD. "get_data
    *
    *.......................................................................
    METHOD generate_output.
    * New ALV instance
    * We are calling the static Factory method which will give back
    * the ALV object reference.

    *
    * exception class

    DATA: lx_msg TYPE REF TO cx_salv_msg.
    TRY.
    cl_salv_table=>factory(
    IMPORTING
    r_salv_table = o_alv
    CHANGING
    t_table = t_vbak ).
    CATCH cx_salv_msg INTO lx_msg.
    ENDTRY.

    *
    *$*$*.....CODE_ADD_2 - Begin..................................2..*$*$*
    *

    * In this area we will call the methods which will set the
    * different properties to the ALV
    *
    * Set default PF status
    CALL METHOD set_pf_status
    CHANGING
    co_alv = o_alv.
    *
    * Set the colors to ALV display
    CALL METHOD set_colors
    CHANGING

    co_alv = o_alv
    ct_vbak = t_vbak.
    *$*$*.....CODE_ADD_2 - End....................................2..*$*$*
    *
    *
    * Displaying the ALV
    * Here we will call the DISPLAY method to get the output on the screen
    o_alv->display( ).
    *
    ENDMETHOD. "generate_output
    *
    *$*$*.....CODE_ADD_3 - Begin..................................3..*$*$*

    *
    * In this area we will implement the methods which are defined in
    * the class definition
    *
    *
    METHOD set_pf_status.
    *
    DATA: lo_functions TYPE REF TO cl_salv_functions_list.
    *
    lo_functions = co_alv->get_functions( ).
    lo_functions->set_default( abap_true ).

    *
    ENDMETHOD. "set_pf_status
    *
    METHOD set_colors.
    *
    *.....Color for COLUMN.....
    DATA: lo_cols_tab TYPE REF TO cl_salv_columns_table,
    lo_col_tab TYPE REF TO cl_salv_column_table.

    DATA: ls_color TYPE lvc_s_colo. " Colors strucutre
    *
    * get Columns object
    lo_cols_tab = co_alv->get_columns( ).
    *
    INCLUDE .
    *
    * Get ERDAT column & set the yellow Color fot it
    TRY.
    lo_col_tab ?= lo_cols_tab->get_column( 'ERDAT' ).
    ls_color-col = col_total.

    lo_col_tab->set_color( ls_color ).
    CATCH cx_salv_not_found.
    ENDTRY.
    *
    *.......Color for Specific Cell & Rows.................

    * Applying color on the 3rd Row and Column AUART
    * Applying color on the Entire 5th Row
    *
    DATA: lt_s_color TYPE lvc_t_scol,
    ls_s_color TYPE lvc_s_scol,
    la_vbak LIKE LINE OF ct_vbak,
    l_count TYPE i.

    *
    LOOP AT ct_vbak INTO la_vbak.
    l_count = l_count + 1.
    CASE l_count.

    * Apply RED color to the AUART Cell of the 3rd Column
    WHEN 3.
    ls_s_color-fname = 'AUART'.
    ls_s_color-color-col = col_negative.
    ls_s_color-color-int = 0.
    ls_s_color-color-inv = 0.
    APPEND ls_s_color TO lt_s_color.

    CLEAR ls_s_color.
    *
    * Apply GREEN color to the entire row # 5
    * For entire row, we don't pass the Fieldname

    WHEN 5.
    ls_s_color-color-col = col_positive.
    ls_s_color-color-int = 0.
    ls_s_color-color-inv = 0.
    APPEND ls_s_color TO lt_s_color.
    CLEAR ls_s_color.

    ENDCASE.
    * Modify that data back to the output table
    la_vbak-t_color = lt_s_color.
    MODIFY ct_vbak FROM la_vbak.
    CLEAR la_vbak.
    CLEAR lt_s_color.

    ENDLOOP.
    *
    * We will set this COLOR table field name of the internal table to
    * COLUMNS tab reference for the specific colors
    TRY.
    lo_cols_tab->set_color_column( 'T_COLOR' ).

    CATCH cx_salv_data_error. "#EC NO_HANDLER
    ENDTRY.
    *
    ENDMETHOD. "set_colors
    *
    *
    *
    *$*$*.....CODE_ADD_3 - End....................................3..*$*$*
    ENDCLASS. "lcl_report IMPLEMENTATION


    Without Colors it will look like:

    With Colors it will look like:

    ABAP Objects: Widening Cast (Down Casting)

    Posted by Krishh Webworld | 11:21 AM | , , | 0 comments »

    When we assign the instance of the Superclass to the Subclass, than it is called the Widening Cast, because we are moving to the "More Specific View" from the "Less specific view". Due to this reason it is also called the "Down Casting".

    Everytime it is not possible to move the Superclass reference to the Subclass, because subclass will(or might) have always more functionality compare to the Superclass. So, we need to be more careful when we use the down casting.

    We will use the same ANIMAL Superclass and LION subclass as per the blog post - ABAP Objects: Narrowing Cast

    When this Widening Cast is used?
    Widening cast can be used when we need to access the Specific functionality of the subclass from the Superclass.

    In ABAP, it is necessary to catch the exception CX_SY_MOVE_CAST_ERROR while doing the widening cast to avoid the short-dump.

    Code Snippet for Widening Cast (Downcasting)
      
    *----------------------------------------------------------------------*
    * This code shows how to use the Widening cast (downcasting)
    *----------------------------------------------------------------------*
    START-OF-SELECTION.
    DATA: lo_animal TYPE REF TO lcl_animal,
    lo_lion TYPE REF TO lcl_lion,
    lo_cast_error TYPE REF TO cx_sy_move_cast_error.
    *
    * First We have to do Narrow casting in order to set the
    * Animal reference to LION reference.

    DATA: lo_tmp_lion TYPE REF TO lcl_lion.
    CREATE OBJECT lo_tmp_lion.
    lo_animal = lo_tmp_lion.
    *
    * Now, we will do the Widening cast to move the reference from the
    * Animal to LION (more specific class).
    TRY.
    lo_lion ?= lo_animal.
    CATCH cx_sy_move_cast_error INTO lo_cast_error.
    WRITE: / 'Widening cast failed 1'.
    ENDTRY.
    IF lo_lion IS NOT INITIAL.

    CALL METHOD lo_lion->hungry( ).
    CALL METHOD lo_lion->fasting( ).
    ENDIF.
    *
    * Now, we will try to do the widening cast without setting up
    * the proper object reference in the Super reference.
    CLEAR: lo_animal, lo_lion, lo_cast_error.
    CREATE OBJECT lo_animal.
    TRY.
    lo_lion ?= lo_animal.
    CATCH cx_sy_move_cast_error INTO lo_cast_error.
    WRITE: / 'Widening cast failed 2'.

    ENDTRY.
    IF lo_lion IS NOT INITIAL.
    CALL METHOD lo_lion->hungry( ).
    CALL METHOD lo_lion->fasting( ).
    ENDIF.



    Output of this code snippet is:

    Archives

    Subscribe Now: Feed Icon