Is it that difficult to NOT use FM ‘REUSE_ALV_GRID_DISPLAY’ to display ALV? Or is it human nature to stay in our comfort zone? ABAPers who are addicted to this FM, have the code snippet ready in their repository to reuse in every other report, so they do not care to look for any other alternative.

SAP released the class ‘CL_SALV_TABLE’ more than a decade ago but still it could not reach the minds of most of those FM loving ABAPers. SAP has different other classes to display the ALV but factory method is one of the simplest to implement. In fact, I find factory method takes even less lines of code than the popular ‘REUSE_ALV_GRID_DISPLAY’.

Internet is flooded with tutorials of how to use this class ‘CL_SALV_TABLE’ for ALV display. So I do not want to add another tutorial. In this post, I would simply provide the working code snippet which can be used in any project. Hope with this ready made solution, some old school ABAPers would take a dip in this class method to display the ALV.. 🙂

Also Read: 4 Versions of the same program to understand OOPs ABAP.

Declare a method in your report class with the below code passing your final internal table ( for beginner you can copy it into a subroutine and call it in your program).

Save this code snippet and start reusing it with confidence.. 😛

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 METHOD display _ alv _ report . DATA : lr _ alv TYPE REF TO cl _ salv _ table , lr _ columns TYPE REF TO cl _ salv _ columns _ table , lr _ column TYPE REF TO cl _ salv _ column , lr _ functions TYPE REF TO cl _ salv _ functions _ list , lr _ display TYPE REF TO cl _ salv _ display _ settings , lr _ layout TYPE REF TO cl _ salv _ layout , lr _ sorts TYPE REF TO cl _ salv _ sorts , ls _ key TYPE salv _ s _ layout _ key . * Check to make sure the internal table has data. IF lines ( it _ final ) > 0. TRY . * Create ALV instance * This is the factory method we were talking about CALL METHOD cl _ salv _ table = > factory IMPORTING r _ salv _ table = lr _ alv CHANGING t _ table = it _ final . * Get functions object lr _ functions = lr _ alv - > get_functions ( ) . * Set all the functions lr _ functions - > set_all ( ) . * Get column object lr _ columns = lr _ alv - > get_columns ( ) . * These are optional functions * If you want to change the description of columns, call and set as shown below * CALL METHOD lr_columns->get_column * EXPORTING * columnname = 'NETWR_PO' * RECEIVING * value = lr_column. * Important to mention. If you want to give custom short text, then make * Medium text and long text blank (important trick). Similarly applicable for med and long text overriding. * CALL METHOD lr_column->set_short_text * EXPORTING * value = ' '. * CALL METHOD lr_column->set_medium_text * EXPORTING * value = 'PO Srv Net Val'. * CALL METHOD lr_column->set_long_text * EXPORTING * value = ' '. * * Optimize the columns * lr_columns->set_optimize( ). * Get sort column * lr_sorts = lr_alv->get_sorts( ). * lr_sorts->clear( ). * Sort table * lr_sorts->add_sort( columnname = 'AUFNR' ). * Get Layout lr _ layout = lr _ alv - > get_layout ( ) . ls _ key - report = sy - repid . * Set Layout lr _ layout - > set_key ( ls _ key ) . lr _ layout - > set_default ( ' ' ) . lr _ layout - > set_save_restriction ( cl _ salv _ layout = > restrict _ none ) . * Optional, if there is a layout from selection screen use it * IF NOT p_layout IS INITIAL. * lr_layout->set_initial_layout( p_layout ). * ENDIF. * Get Display setting lr _ display = lr _ alv - > get_display_settings ( ) . lr _ display - > set_striped_pattern ( cl _ salv _ display _ settings = > true ) . * Display the ALV lr _ alv - > display ( ) . * Catch exceptions CATCH cx _ salv _ msg . WRITE : 'Error displaying grid CX_SALV_MSG!' . CATCH cx _ salv _ not _ found . WRITE : 'Error displaying grid CX_SALV_NOT_FOUND!' . CATCH cx _ salv _ data _ error . WRITE : 'Error displaying grid CX_SALV_DATA_ERROR!' . CATCH cx _ salv _ existing . WRITE : 'Error displaying grid CX_SALV_EXISTING!' . ENDTRY . ELSE . WRITE : 'There is no data to display!' . ENDIF . ENDMETHOD . " DISPLAY_ALV_REPORT

If you do not want to display certain fields of the final table, we can set it to invisible.

1 2 3 4 5 6 7 8 lr _ column - > set_visible ( value if _ salv _ c _ bool _ sap = > false ) . Or CALL METHOD lr _ column - > set _ visible EXPORTING value = if _ salv _ c _ bool _ sap = > false .

Updated 12/26/2018

If you want to put header information on top of the ALV, you can use the below snippet.

Below function would put the header.

1 2 lr _ alv < span class = "L0S70">-></span>set_top_of_list<span class="L0S55">( </span>lr_content <span class="L0S55">)</span><span class="L0S55">.</span>

Check the reference snippet to build the header content.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 < span class = "L0S31">*&---------------------------------------------------------------------*</span> < span class = "L0S31">*& Form built_header</span> < span class = "L0S31">*&---------------------------------------------------------------------*</span> < span class = "L0S52">FORM </span>built_header <span class="L0S52">CHANGING </span>cr_content <span class="L0S52">TYPE </span><span class="L0S52">REF </span><span class="L0S52">TO </span>cl_salv_form_element<span class="L0S55">.</span> < span class = "L0S52">DATA</span><span class="L0S55">: </span>lr_grid <span class="L0S52">TYPE </span><span class="L0S52">REF </span><span class="L0S52">TO </span>cl_salv_form_layout_grid<span class="L0S55">,</span> lv _ row < span class = "L0S52">TYPE </span><span class="L0S52">i</span><span class="L0S55">,</span> lv _ fdate < span class = "L0S52">TYPE </span>char10<span class="L0S55">,</span> lv _ tdate < span class = "L0S52">TYPE </span>char10<span class="L0S55">,</span> lwa _ error < span class = "L0S52">TYPE </span>ty_error<span class="L0S55">,</span> lv _ head < span class = "L0S52">TYPE </span>string <span class="L0S52">VALUE </span><span class="L0S33">'Report of Dunned Accounts with No POSID'</span><span class="L0S55">,</span> lv _ text < span class = "L0S52">TYPE </span>string<span class="L0S55">,</span> lr _ grid _ 1 < span class = "L0S52">TYPE </span><span class="L0S52">REF </span><span class="L0S52">TO </span>cl_salv_form_layout_grid<span class="L0S55">,</span> lr _ text < span class = "L0S52">TYPE </span><span class="L0S52">REF </span><span class="L0S52">TO </span>cl_salv_form_text<span class="L0S55">,</span> lr _ label < span class = "L0S52">TYPE </span><span class="L0S52">REF </span><span class="L0S52">TO </span>cl_salv_form_label<span class="L0S55">.</span> lv _ row < span class = "L0S55">= </span><span class="L0S52">lines</span><span class="L0S55">( </span>it_error <span class="L0S55">)</span><span class="L0S55">.</span> lv _ row < span class = "L0S55">= </span>lv_row + <span class="L0S32">2</span><span class="L0S55">.</span> < span class = "L0S52">CREATE </span>OBJECT lr_grid<span class="L0S55">.</span> lr _ grid < span class = "L0S70">-></span>create_header_information<span class="L0S55">(</span> row < span class = "L0S55">= </span><span class="L0S32">1</span> column < span class = "L0S55">= </span><span class="L0S32">1</span> < span class = "L0S52">text </span><span class="L0S55">= </span>lv_head tooltip < span class = "L0S55">= </span>lv_head <span class="L0S55">)</span><span class="L0S55">.</span> lr _ grid < span class = "L0S70">-></span>add_row<span class="L0S55">( </span><span class="L0S55">)</span><span class="L0S55">.</span> lr _ grid _ 1 < span class = "L0S55">= </span>lr_grid<span class="L0S70">-></span>create_grid<span class="L0S55">( </span>row <span class="L0S55">= </span>lv_row column < span class = "L0S55">= </span><span class="L0S32">1 </span><span class="L0S55">)</span><span class="L0S55">.</span> lr _ label < span class = "L0S55">= </span>lr_grid_1<span class="L0S70">-></span>create_label<span class="L0S55">(</span> row < span class = "L0S55">= </span><span class="L0S32">1</span> column < span class = "L0S55">= </span><span class="L0S32">1</span> < span class = "L0S52">text </span><span class="L0S55">= </span><span class="L0S33">'Dunning Run Date:'</span><span class="L0S55">(</span><span class="L0S32">015</span><span class="L0S55">)</span> tooltip < span class = "L0S55">= </span><span class="L0S33">'Dunning Run Date:'</span><span class="L0S55">(</span><span class="L0S32">015</span><span class="L0S55">) </span><span class="L0S55">)</span><span class="L0S55">.</span> < span class = "L0S52">IF </span>s_id <span class="L0S52">IS </span><span class="L0S52">NOT </span><span class="L0S52">INITIAL</span><span class="L0S55">.</span> lr _ label < span class = "L0S55">= </span>lr_grid_1<span class="L0S70">-></span>create_label<span class="L0S55">(</span> row < span class = "L0S55">= </span><span class="L0S32">2</span> column < span class = "L0S55">= </span><span class="L0S32">1</span> < span class = "L0S52">text </span><span class="L0S55">= </span><span class="L0S33">'Dunning Run ID:'</span><span class="L0S55">(</span><span class="L0S32">016</span><span class="L0S55">)</span> tooltip < span class = "L0S55">= </span><span class="L0S33">'Dunning Run ID:'</span><span class="L0S55">(</span><span class="L0S32">016</span><span class="L0S55">) </span><span class="L0S55">)</span><span class="L0S55">.</span> < span class = "L0S52">ENDIF</span><span class="L0S55">.</span> < span class = "L0S52">CLEAR </span>lv_text<span class="L0S55">.</span> < span class = "L0S52">IF </span>s_date<span class="L0S70">-</span>high <span class="L0S52">IS </span><span class="L0S52">NOT </span><span class="L0S52">INITIAL</span><span class="L0S55">.</span> < span class = "L0S52">WRITE </span>s_date<span class="L0S70">-</span>low <span class="L0S52">TO </span>lv_fdate<span class="L0S55">.</span> < span class = "L0S52">WRITE </span>s_date<span class="L0S70">-</span>high <span class="L0S52">TO </span>lv_tdate<span class="L0S55">.</span> < span class = "L0S52">CONCATENATE </span>lv_fdate <span class="L0S33">'to' </span>lv_tdate <span class="L0S52">INTO </span>lv_text <span class="L0S52">SEPARATED </span><span class="L0S52">BY </span>space<span class="L0S55">.</span> < span class = "L0S52">ELSE</span><span class="L0S55">.</span> < span class = "L0S52">WRITE </span>s_date<span class="L0S70">-</span>low <span class="L0S52">TO </span>lv_fdate<span class="L0S55">.</span> lv _ text < span class = "L0S55">= </span>lv_fdate<span class="L0S55">.</span> < span class = "L0S52">ENDIF</span><span class="L0S55">.</span> lr _ text < span class = "L0S55">= </span>lr_grid_1<span class="L0S70">-></span>create_text<span class="L0S55">(</span> row < span class = "L0S55">= </span><span class="L0S32">1</span> column < span class = "L0S55">= </span><span class="L0S32">2</span> < span class = "L0S52">text </span><span class="L0S55">= </span>lv_text <span class="L0S31">" s_date-low</span> tooltip < span class = "L0S55">= </span>lv_text <span class="L0S55">)</span><span class="L0S55">. </span><span class="L0S31">" s_date ).</span> < span class = "L0S52">CLEAR </span>lv_text<span class="L0S55">.</span> < span class = "L0S52">IF </span>s_id <span class="L0S52">IS </span><span class="L0S52">NOT </span><span class="L0S52">INITIAL</span><span class="L0S55">.</span> < span class = "L0S52">IF </span>s_id<span class="L0S70">-</span>high <span class="L0S52">IS </span><span class="L0S52">NOT </span><span class="L0S52">INITIAL</span><span class="L0S55">.</span> < span class = "L0S52">CONCATENATE </span>s_id<span class="L0S70">-</span>low <span class="L0S33">'to' </span>s_id<span class="L0S70">-</span>high <span class="L0S52">INTO </span>lv_text <span class="L0S52">SEPARATED </span><span class="L0S52">BY </span>space<span class="L0S55">.</span> < span class = "L0S52">ELSE</span><span class="L0S55">.</span> lv _ text < span class = "L0S55">= </span>s_id<span class="L0S70">-</span>low<span class="L0S55">.</span> < span class = "L0S52">ENDIF</span><span class="L0S55">.</span> lr _ text < span class = "L0S55">= </span>lr_grid_1<span class="L0S70">-></span>create_text<span class="L0S55">(</span> row < span class = "L0S55">= </span><span class="L0S32">2</span> column < span class = "L0S55">= </span><span class="L0S32">2</span> < span class = "L0S52">text </span><span class="L0S55">= </span>lv_text <span class="L0S31">" s_id-low</span> tooltip < span class = "L0S55">= </span>lv_text <span class="L0S55">)</span><span class="L0S55">. </span><span class="L0S31">"s_id ).</span> < span class = "L0S52">ENDIF</span><span class="L0S55">.</span> < span class = "L0S52">CLEAR </span>lv_row<span class="L0S55">.</span> lv _ row < span class = "L0S55">= </span><span class="L0S32">2</span><span class="L0S55">.</span> < span class = "L0S52">LOOP </span><span class="L0S52">AT </span>it_error <span class="L0S52">INTO </span>lwa_error<span class="L0S55">.</span> lv _ row < span class = "L0S55">= </span>lv_row + <span class="L0S32">1</span><span class="L0S55">.</span> lr _ label < span class = "L0S55">= </span>lr_grid_1<span class="L0S70">-></span>create_label<span class="L0S55">(</span> row < span class = "L0S55">= </span>lv_row column < span class = "L0S55">= </span><span class="L0S32">1</span> < span class = "L0S52">text </span><span class="L0S55">= </span><span class="L0S33">'Dunning Lock Error:'</span><span class="L0S55">(</span><span class="L0S32">017</span><span class="L0S55">)</span> tooltip < span class = "L0S55">= </span><span class="L0S33">'Dunning Lock Error:'</span><span class="L0S55">(</span><span class="L0S32">017</span><span class="L0S55">) </span><span class="L0S55">)</span><span class="L0S55">.</span> lr _ text < span class = "L0S55">= </span>lr_grid_1<span class="L0S70">-></span>create_text<span class="L0S55">(</span> row < span class = "L0S55">= </span>lv_row column < span class = "L0S55">= </span><span class="L0S32">2</span> < span class = "L0S52">text </span><span class="L0S55">= </span>lwa_error<span class="L0S70">-</span><span class="L0S52">message</span> tooltip < span class = "L0S55">= </span>lwa_error<span class="L0S70">-</span><span class="L0S52">message </span><span class="L0S55">)</span><span class="L0S55">.</span> < span class = "L0S52">ENDLOOP</span><span class="L0S55">.</span> lr _ label < span class = "L0S70">-></span>set_label_for<span class="L0S55">( </span>lr_text <span class="L0S55">)</span><span class="L0S55">.</span> cr _ content < span class = "L0S55">= </span>lr_grid<span class="L0S55">.</span> < span class = "L0S52">ENDFORM</span><span class="L0S55">. </span><span class="L0S31">" built_header</span>

Please let us know, if we need to provide further clarification on this topic.

Also Read: How to convert an existing ABAP Report to OOPs Report.

Image source: via Free Stock Photos foter.com