Comprehensive ABAP Application for Sales Document Flow Reporting
Create an ALV Report to Track Sales Orders to SD Invoices Seamlessly
Key Takeaways
- Input Parameter Handling: Efficiently capture and validate user input for sales order numbers to ensure accurate data retrieval.
- Data Retrieval and Processing: Utilize SAP function modules and classes to fetch and process document flow data from relevant tables.
- ALV Grid Display: Implement interactive ALV grids using both procedural and object-oriented approaches for enhanced user experience.
Introduction
Developing an ABAP report to display the flow of sales documents from sales orders to SD invoices and beyond is crucial for comprehensive sales tracking and efficient business operations. This guide provides a detailed ABAP application that leverages ALV (ABAP List Viewer) reporting to visualize the document flow, ensuring users can monitor and analyze each step seamlessly.
Application Overview
The ABAP report application facilitates the following:
- Accepts a sales order number as input.
- Retrieves associated document flow data, including deliveries and invoices.
- Displays the data in an interactive ALV grid for easy analysis.
Implementation Details
1. Data Declarations
Start by defining the necessary data structures and internal tables to hold the document flow information. This includes defining a custom type for the ALV data and declaring internal tables and work areas.
REPORT z_sales_doc_flow.
TYPES: BEGIN OF ty_docflow,
vbeln_va TYPE vbak-vbeln, " Sales Order
erdat TYPE vbak-erdat, " Created On
vbeln_vl TYPE vbfa-vbeln, " Delivery Document
vbeln_vf TYPE vbfa-vbeln, " Billing Document
fkart TYPE vbfa-fkart, " Billing Type
status TYPE char20, " Document Status
END OF ty_docflow.
DATA: gt_docflow TYPE TABLE OF ty_docflow,
wa_docflow TYPE ty_docflow.
DATA: lt_vbfa TYPE TABLE OF vbfa,
wa_vbfa TYPE vbfa.
DATA: gt_fieldcat TYPE slis_t_fieldcat_alv,
wa_layout TYPE slis_layout_alv.
PARAMETERS: p_vbeln TYPE vbak-vbeln OBLIGATORY.
2. Selection Screen
The selection screen captures the sales order number from the user. This parameter is mandatory to ensure the report retrieves relevant data.
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
PARAMETERS: p_vbeln TYPE vbak-vbeln OBLIGATORY.
SELECTION-SCREEN END OF BLOCK b1.
3. Data Retrieval
The core functionality involves fetching the document flow using the SAP function module SD_DOCUMENTFLOW_GET. This function retrieves related documents such as deliveries and invoices associated with the sales order.
START-OF-SELECTION.
PERFORM get_document_flow.
PERFORM display_alv.
3.1 Fetching Document Flow
FORM get_document_flow.
CALL FUNCTION 'SD_DOCUMENTFLOW_GET'
EXPORTING
iv_docnum = p_vbeln
TABLES
et_flow = lt_vbfa
EXCEPTIONS
not_found = 1
OTHERS = 2.
IF sy-subrc = 0.
LOOP AT lt_vbfa INTO wa_vbfa.
CLEAR wa_docflow.
CASE wa_vbfa-vbtyp_n.
WHEN 'C'. " Sales Order
wa_docflow-vbeln_va = wa_vbfa-vbelv.
wa_docflow-status = 'Sales Order'.
WHEN 'J'. " Delivery
wa_docflow-vbeln_vl = wa_vbfa-vbeln.
wa_docflow-status = 'Delivery'.
WHEN 'M'. " Invoice
wa_docflow-vbeln_vf = wa_vbfa-vbeln.
wa_docflow-fkart = wa_vbfa-fkart.
wa_docflow-status = 'Invoice'.
ENDCASE.
wa_docflow-erdat = wa_vbfa-erdat.
wa_docflow-vbtyp = wa_vbfa-vbtyp_n.
APPEND wa_docflow TO gt_docflow.
ENDLOOP.
ELSE.
MESSAGE 'No document flow found for the given sales order.' TYPE 'I'.
EXIT.
ENDIF.
ENDFORM.
4. Building the Field Catalog
The field catalog defines how fields are displayed in the ALV grid. It specifies field names, their headings, and other display attributes.
FORM build_fieldcat.
PERFORM add_fieldcat USING 'VBELN_VA' 'Sales Order' 'Sales Order' ''.
PERFORM add_fieldcat USING 'ERDAT' 'Created On' 'Creation Date' ''.
PERFORM add_fieldcat USING 'VBELN_VL' 'Delivery' 'Delivery Doc' ''.
PERFORM add_fieldcat USING 'VBELN_VF' 'Billing Doc' 'Billing Document' ''.
PERFORM add_fieldcat USING 'FKART' 'Billing Type' 'Billing Type' ''.
PERFORM add_fieldcat USING 'STATUS' 'Status' 'Document Status' ''.
ENDFORM.
FORM add_fieldcat USING p_fieldname p_seltext_s p_seltext_l p_ref_table.
DATA: ls_fieldcat TYPE slis_fieldcat_alv.
ls_fieldcat-fieldname = p_fieldname.
ls_fieldcat-seltext_s = p_seltext_s.
ls_fieldcat-seltext_l = p_seltext_l.
ls_fieldcat-ref_table = p_ref_table.
APPEND ls_fieldcat TO gt_fieldcat.
ENDFORM.
5. Displaying the ALV Grid
The ALV grid presents the document flow data in a structured and interactive manner. This implementation uses the procedural approach with the function module REUSE_ALV_GRID_DISPLAY.
FORM display_alv.
" Set ALV Layout
wa_layout-colwidth_optimize = 'X'.
wa_layout-zebra = 'X'.
" Build Field Catalog
PERFORM build_fieldcat.
" Display ALV
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = sy-repid
is_layout = wa_layout
it_fieldcat = gt_fieldcat
TABLES
t_outtab = gt_docflow
EXCEPTIONS
program_error = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE 'Error displaying ALV' TYPE 'E'.
ENDIF.
ENDFORM.
5.1 Using CL_SALV_TABLE for Enhanced ALV Display
Alternatively, leveraging the class CL_SALV_TABLE offers a more object-oriented approach with enhanced features and easier customization.
FORM display_alv_object_oriented.
DATA: lo_alv TYPE REF TO cl_salv_table.
TRY.
cl_salv_table=>factory(
IMPORTING
r_salv_table = lo_alv
CHANGING
t_table = gt_docflow ).
lo_alv->display( ).
CATCH cx_salv_msg INTO DATA(lx_salv_error).
MESSAGE lx_salv_error->get_text( ) TYPE 'E'.
ENDTRY.
ENDFORM.
Enhancements and Best Practices
1. Performance Optimization
To ensure the report performs efficiently, especially with large datasets, consider implementing the following:
- Indexing relevant fields in the
VBFAtable. - Using parallel processing or background jobs for data retrieval.
- Minimizing data retrieved by selecting only necessary fields.
2. User Interface Enhancements
Improve the user experience by adding:
- Export functionality to Excel or other formats.
- Dynamic sorting and filtering within the ALV grid.
- Custom color coding or conditional formatting based on document status.
3. Robust Error Handling
Implement comprehensive error handling to manage scenarios such as:
- Invalid sales order numbers.
- Failures in data retrieval or ALV display.
- Unexpected exceptions during runtime.
4. Integration with BAPIs
For more robust and scalable implementations, integrate Business Application Programming Interfaces (BAPIs) such as BAPI_DOCUMENTFLOW_GETLIST. This facilitates better data handling and ensures compatibility with SAP's best practices.
Sample ABAP Code
Complete ABAP Report Example
Below is a comprehensive ABAP report that integrates the concepts discussed. This report captures a sales order number, retrieves the document flow, and displays it using an ALV grid.
REPORT z_sales_doc_flow.
"---------------------------------------------------------------------
" Types Definition
"---------------------------------------------------------------------
TYPES: BEGIN OF ty_docflow,
vbeln_va TYPE vbak-vbeln, " Sales Order
erdat TYPE vbak-erdat, " Created On
vbeln_vl TYPE vbfa-vbeln, " Delivery Document
vbeln_vf TYPE vbfa-vbeln, " Billing Document
fkart TYPE vbfa-fkart, " Billing Type
status TYPE char20, " Document Status
END OF ty_docflow.
"---------------------------------------------------------------------
" Data Declarations
"---------------------------------------------------------------------
DATA: gt_docflow TYPE TABLE OF ty_docflow,
wa_docflow TYPE ty_docflow.
DATA: lt_vbfa TYPE TABLE OF vbfa,
wa_vbfa TYPE vbfa.
DATA: gt_fieldcat TYPE slis_t_fieldcat_alv,
wa_layout TYPE slis_layout_alv.
PARAMETERS: p_vbeln TYPE vbak-vbeln OBLIGATORY.
"---------------------------------------------------------------------
" Selection Screen
"---------------------------------------------------------------------
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
PARAMETERS: p_vbeln TYPE vbak-vbeln OBLIGATORY.
SELECTION-SCREEN END OF BLOCK b1.
"---------------------------------------------------------------------
" Start of Selection
"---------------------------------------------------------------------
START-OF-SELECTION.
PERFORM get_document_flow.
PERFORM display_alv.
"---------------------------------------------------------------------
" Form: Get Document Flow
"---------------------------------------------------------------------
FORM get_document_flow.
CALL FUNCTION 'SD_DOCUMENTFLOW_GET'
EXPORTING
iv_docnum = p_vbeln
TABLES
et_flow = lt_vbfa
EXCEPTIONS
not_found = 1
OTHERS = 2.
IF sy-subrc = 0.
LOOP AT lt_vbfa INTO wa_vbfa.
CLEAR wa_docflow.
CASE wa_vbfa-vbtyp_n.
WHEN 'C'. " Sales Order
wa_docflow-vbeln_va = wa_vbfa-vbelv.
wa_docflow-status = 'Sales Order'.
WHEN 'J'. " Delivery
wa_docflow-vbeln_vl = wa_vbfa-vbeln.
wa_docflow-status = 'Delivery'.
WHEN 'M'. " Invoice
wa_docflow-vbeln_vf = wa_vbfa-vbeln.
wa_docflow-fkart = wa_vbfa-fkart.
wa_docflow-status = 'Invoice'.
ENDCASE.
wa_docflow-erdat = wa_vbfa-erdat.
wa_docflow-vbtyp = wa_vbfa-vbtyp_n.
APPEND wa_docflow TO gt_docflow.
ENDLOOP.
ELSE.
MESSAGE 'No document flow found for the given sales order.' TYPE 'I'.
EXIT.
ENDIF.
ENDFORM.
"---------------------------------------------------------------------
" Form: Build Field Catalog
"---------------------------------------------------------------------
FORM build_fieldcat.
PERFORM add_fieldcat USING 'VBELN_VA' 'Sales Order' 'Sales Order' ''.
PERFORM add_fieldcat USING 'ERDAT' 'Created On' 'Creation Date' ''.
PERFORM add_fieldcat USING 'VBELN_VL' 'Delivery' 'Delivery Doc' ''.
PERFORM add_fieldcat USING 'VBELN_VF' 'Billing Doc' 'Billing Document' ''.
PERFORM add_fieldcat USING 'FKART' 'Billing Type' 'Billing Type' ''.
PERFORM add_fieldcat USING 'STATUS' 'Status' 'Document Status' ''.
ENDFORM.
FORM add_fieldcat USING p_fieldname p_seltext_s p_seltext_l p_ref_table.
DATA: ls_fieldcat TYPE slis_fieldcat_alv.
ls_fieldcat-fieldname = p_fieldname.
ls_fieldcat-seltext_s = p_seltext_s.
ls_fieldcat-seltext_l = p_seltext_l.
ls_fieldcat-ref_table = p_ref_table.
APPEND ls_fieldcat TO gt_fieldcat.
ENDFORM.
"---------------------------------------------------------------------
" Form: Display ALV
"---------------------------------------------------------------------
FORM display_alv.
" Set ALV Layout
wa_layout-colwidth_optimize = 'X'.
wa_layout-zebra = 'X'.
" Build Field Catalog
PERFORM build_fieldcat.
" Display ALV
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = sy-repid
is_layout = wa_layout
it_fieldcat = gt_fieldcat
TABLES
t_outtab = gt_docflow
EXCEPTIONS
program_error = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE 'Error displaying ALV' TYPE 'E'.
ENDIF.
ENDFORM.
Understanding the Code
Input Parameter Handling
The report begins by defining a mandatory input parameter p_vbeln, which captures the sales order number. This ensures that the report retrieves and displays data specific to the entered sales order.
Data Retrieval Using Function Modules
The SD_DOCUMENTFLOW_GET function module is pivotal in fetching the document flow related to the sales order. It populates the internal table lt_vbfa with records from the VBFA table, which contains information about preceding and succeeding documents.
Processing Retrieved Data
The report iterates over the lt_vbfa table to process each document flow record. Using a CASE statement on the document type (vbtyp_n), it categorizes the documents as Sales Orders, Deliveries, or Invoices, and populates the corresponding fields in the wa_docflow work area.
Building the Field Catalog
The field catalog defines the layout and attributes of each column in the ALV grid. By specifying the field names, short and long texts, and reference tables, the report ensures a clear and organized display of data.
ALV Grid Display
The REUSE_ALV_GRID_DISPLAY function module is invoked to render the ALV grid. The layout parameters such as optimized column widths and zebra patterns enhance readability. In cases of errors during display, appropriate messages are shown to the user.
Enhancement Examples
Exporting ALV to Excel
To allow users to export the report data to Excel, you can add a menu option or toolbar button within the ALV grid. This enables easy sharing and further analysis of the document flow data.
* Add export functionality
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = sy-repid
is_layout = wa_layout
it_fieldcat = gt_fieldcat
TABLES
t_outtab = gt_docflow
EXCEPTIONS
program_error = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE 'Error displaying ALV' TYPE 'E'.
ENDIF.
" Add handling for user command
*&-----------------------------------------------------------------*
*& Form USER_COMMAND
*&-----------------------------------------------------------------*
FORM user_command USING r_ucomm LIKE sy-ucomm
rs_selfield TYPE slis_selfield.
CASE r_ucomm.
WHEN '&WX'. " Export to Excel
cl_salv_table=>factory(
IMPORTING r_salv_table = lo_alv
CHANGING t_table = gt_docflow ).
lo_alv->get_functions( )->set_all( abap_true ).
lo_alv->display( ).
ENDCASE.
ENDFORM.
Conditional Formatting
Enhance the ALV grid by applying color coding based on document status. For instance, highlight invoices in green and deliveries in blue to quickly identify document types.
FORM display_alv_with_coloring.
DATA: lo_alv TYPE REF TO cl_salv_table,
lo_columns TYPE REF TO cl_salv_columns_table,
lo_column TYPE REF TO cl_salv_column.
TRY.
cl_salv_table=>factory(
IMPORTING
r_salv_table = lo_alv
CHANGING
t_table = gt_docflow ).
lo_columns = lo_alv->get_columns( ).
lo_column = lo_columns->get_column( 'STATUS' ).
lo_column->set_color( if_salv_c_color=>c_green ).
lo_alv->display( ).
CATCH cx_salv_msg INTO DATA(lx_salv_error).
MESSAGE lx_salv_error->get_text( ) TYPE 'E'.
ENDTRY.
ENDFORM.
Conclusion
Creating an ABAP report with ALV reporting to display the flow of sales documents from sales orders to SD invoices is a powerful tool for sales management and operational efficiency. By following the structured approach outlined above, developers can build robust applications that provide clear insights into document progression, enhance data visibility, and support informed decision-making.
References
- Sales Order Document Flow in Reports - SAP Community
- Get SAP Document Flow using SD_DOCUMENTFLOW_GET ABAP Function Module
- How to display document flow using ALV in SAP - Stack Overflow
- SAP Documentation - VBFA Document Flow Table
- Creating ALV reports using CL_SALV_TABLE - Belajar ABAP
-
ABAP ALV Reports - Sastrageek
- ALV List View Programming - Guru99
- What is ALV? An Overview of ABAP List Viewer and SAP List Viewer - SAP Press Blog
References
Last updated January 22, 2025