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.
The ABAP report application facilitates the following:
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.
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.
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.
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.
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.
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.
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.
To ensure the report performs efficiently, especially with large datasets, consider implementing the following:
VBFA table.Improve the user experience by adding:
Implement comprehensive error handling to manage scenarios such as:
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.