Adventure on SAP ALV Function Modules [SAP]

I have developed an ABAP program. Basically it gets billing document (invoice) data, then show as ALV report. The one of the functionalities is you can send selected rows via e-mail. The e-mail has an Excel file attachment that has data for selected rows.  The question is here which fields will be inserted into Excel file attachment. I decided to use a dynamic logic. What I mean is whatever current ALV layout; the Excel file layout will be same. So users can decide the Excel file layout, instead of hard-coded file layout/structure.

I used REUSE_ALV_GRID_DISPLAY function module to display the report as ALV. So when e-mail send command is run, I needed to get current ALV layout. To do that I used REUSE_ALV_GRID_LAYOUT_INFO_GET function module.

CALL FUNCTION ‘REUSE_ALV_GRID_LAYOUT_INFO_GET’
IMPORTING
et_fieldcat   = lt_fieldcat
EXCEPTIONS
no_infos      = 1
program_error = 2
OTHERS        = 3.

So it gives you current field catalog in lt_fieldcat internal table. As it is known field catalog internal table has field name.
When I loop my main internal table gt_output, I also need to loop field catalog internal table like:

FIELD-SYMBOLS:   <lfs_field> TYPE ANY.

LOOP AT gt_output INTO gs_output.

LOOP AT put_fieldcat INTO ls_fieldcat
WHERE no_out <> abap_true.

ASSIGN COMPONENT ls_fieldcat-fieldname
OF STRUCTURE pus_output TO <lfs_field>.

CONCATENATE ls_attach lv_field
INTO ls_attach
SEPARATED BY gc_tab.

ENDLOOP.

APPEND ls_attach TO  lt_attach.

ENDLOOP.

no_out field value must be different than abap_true (X). So here we get all available fields on the screen.  Then use a field-symbol to get field value from gs_output. As a result <lfs_field> has value for each field name from gs_output. Then use CONCATENATE and get  a work area for attachment file.

Also there might be some trick points here. You may need to identify <lfs_field> type, then do some formatting for specific types as shown below:

DESCRIBE FIELD <lfs_field> TYPE lv_type.

CASE lv_type.
*  Date
WHEN gc_type_date.
WRITE <lfs_field> TO lv_field MM/DD/YYYY.

ENDCASE.

This logic all works in foreground run. But suddenly when we run the program in background we got a blank/empty Excel file as an attachment in the e-mail. After investigation the issue I realized that REUSE_ALV_GRID_LAYOUT_INFO_GET function module returns nothing in background. It is obvious that because actually REUSE_ALV_GRID_DISPLAY function module does not run for background runs. It runs but when it realizes the background run, it calls another function module REUSE_ALV_LIST_DISPLAY.  And REUSE_ALV_GRID_LAYOUT_INFO_GET can’t able to get field catalog for REUSE_ALV_LIST_DISPLAY. So there is another function module to do that REUSE_ALV_LIST_LAYOUT_INFO_GET.

But when you call this function module exactly at the same place as for REUSE_ALV_GRID_LAYOUT_INFO_GET , it does not work. Because ALV List catalog and list show kind of not available yet! So I needed to call REUSE_ALV_LIST_LAYOUT_INFO_GET, when the report is listed. Then I defined a subroutine name alv_top_of_page for i_callback_top_of_page parameter on REUSE_ALV_GRID_DISPLAY. Then I called  REUSE_ALV_LIST_LAYOUT_INFO_GET in subroutine alv_top_of_page. Here I have same field catalog and feel free to use it…

Tags: , , ,

6 Responses to “Adventure on SAP ALV Function Modules [SAP]”

  1. eddai Says:

    Hi Mr. Tuncay,
    I have just added your site to my directory under http://social.sapdocs.info/abap/
    for this i retrieved your SAP category posts.
    but your this post is not seen under this category, so please include this also in :)
    looking forward to hear more frequent updates from you :)
    Cheers~

  2. Tuncay Karaca Says:

    @eddai

    Thanks for the link… I appreciate.

  3. Bobby Says:

    Hi Mr. Tuncay,
    I’ve sent an Email to you, and would like to add your blog link into my blog( http://www.sapalv.net ). Could you reply my mail when you have free time? Thanks.

  4. goutham Says:

    What happens if there are multiple pages in a alv report, will it be appended multiple times?

  5. mySAP Says:

    I think SAP report with ALV format is friendly more than list view. Anyway, thanks for article

  6. Persefone Says:

    Hi,

    I am applying the same logic for send an excel. I have done some tests executing in background using FM REUSE_ALV_GRID_LAYOUT_INFO_GET without implementing the subroutine and it seems that it works. The excel is being informed.

    Could you send me the exactly code for the subroutine to my e-mail? Could you also send me more info about in what cases the excel was not informed?

    Thanks in advance. Your post was very helpful.

Leave a Reply