Skip to Content
Technical Articles

Sort Table Maintenance Generator records after every new insert

Dear Community,

Through this blog post, I will handle a trivial yet common requirement with a reusable code which can sort the table maintenance generator records after every new entry inserted based on a non-key field.

The solution demonstrates this feature for both View Maintenance (SM30) and View Cluster Maintenance (SM34). The challenge here was that the runtime artifacts are dynamically typed so no static coding will achieve the result for both cases simultaneously.

I borrowed ideas fromĀ https://blogs.sap.com/2016/03/10/to-sort-table-maintenance-before-display/ and would like to thank OP and other participants.

Also, I would like to hear your experiences and suggestions on how to improve below implementation.

First, create a Maintenance view for the underlying table and then go to the screen program of the maintenance view (SE11 -> <Maintenance View> -> Utilities -> Table Maintenance Generator -> Environment -> Modification -> Maintenance Screens -> <Choose Screen Number>).

In the Process Before Output section, add a module as below –

PROCESS BEFORE OUTPUT.
  MODULE sort_extract.       

Entire code –

MODULE sort_extract OUTPUT.

  " Required Structure
  TYPES: BEGIN OF ts_atank_it.
      INCLUDE STRUCTURE zv_atank_it.
  TYPES :  index TYPE i.
  TYPES: END OF ts_atank_it.

  " Extract structure
  TYPES : BEGIN OF ts_extract,
            extract TYPE char512,
            index   TYPE i,
          END OF ts_extract.
  
  "  Data Declaration
  DATA : gv_newline TYPE char1.
  FIELD-SYMBOLS: <fs_rec_from> TYPE x, "Hexadecimal value of from record
                 <fs_rec_to>   TYPE x. "Hexadecimal value of to record
  DATA :
    lt_atank_it_tmp_view_maint TYPE STANDARD TABLE OF ts_atank_it,
    lt_extract                 TYPE STANDARD TABLE OF ts_extract,
    lt_atank_it_tmp            TYPE STANDARD TABLE OF ts_atank_it.

  DATA : ls_atank_it_tmp_sm30 TYPE  ts_atank_it,
         ls_extract           TYPE ts_extract.

  FIELD-SYMBOLS <fs_tank_it_view_maint> TYPE ts_atank_it.

  " Logic branched based on the invoking call
  IF   sy-tcode = 'SM30'. " Create constant here

    " Check if the button pressed by user in not the 'New Entries' button
    IF NOT sy-ucomm = 'NEWL'.

      IF sy-ucomm = 'BACK'
      OR sy-ucomm = 'SAVE'.
        " Refresh the new line addition flag
        CLEAR gv_newline.
      ENDIF.

      " Prepare the table with values and the indexes
      CLEAR lt_atank_it_tmp_view_maint.
      LOOP AT extract.
        ls_atank_it_tmp_sm30 = CORRESPONDING #( extract ).
        ls_atank_it_tmp_sm30-index = sy-tabix.
        APPEND ls_atank_it_tmp_sm30 TO lt_atank_it_tmp_view_maint.
        CLEAR : ls_atank_it_tmp_sm30.
      ENDLOOP.

      " Initialize the extract table after copying it's data
      CLEAR extract[].

      " Sort the content by desired non-key field
      SORT lt_atank_it_tmp_view_maint BY <desired non-key field> ASCENDING.

      " Remove all the blank lines which are shown on screen
      IF gv_newline IS NOT INITIAL.
        " For a valid line, all the key fields can never be blank
        DELETE lt_atank_it_tmp_view_maint WHERE <key field(s)>   IS INITIAL.
      ENDIF.

      "  Move back the sorted contents to Extract table
      LOOP AT lt_atank_it_tmp_view_maint
        ASSIGNING <fs_tank_it_view_maint>.

        APPEND INITIAL LINE TO extract ASSIGNING FIELD-SYMBOL(<fs_extract_view_maint_sort>).

        <fs_extract_view_maint_sort> = CORRESPONDING #( <fs_tank_it_view_maint> ).

      ENDLOOP.

    ELSE.

      " User pressed the 'New Entries' button, record the same action
      gv_newline = abap_true.

    ENDIF.

  ELSE.

    IF NOT sy-ucomm = 'NEWL'.

      IF sy-ucomm = 'BACK'
      OR sy-ucomm = 'SAVE'.
        CLEAR gv_newline.
      ENDIF.

   "  Make table with Extract data and Index
      CLEAR lt_extract.
      LOOP AT extract.
        ls_extract-extract = extract.
        ls_extract-index = sy-tabix.
        APPEND ls_extract TO lt_extract.
      ENDLOOP.

   "  Store Extract data into Tank Item structure
      CLEAR lt_atank_it_tmp.
      LOOP AT extract.
        APPEND INITIAL LINE TO lt_atank_it_tmp ASSIGNING <fs_rec_to> CASTING.
        ASSIGN extract TO <fs_rec_from> CASTING.
        <fs_rec_to> = <fs_rec_from>.
      ENDLOOP.

   "  Store Index in Tank item structure
      LOOP AT lt_atank_it_tmp
          ASSIGNING FIELD-SYMBOL(<fs_atank>).
        <fs_atank>-index = sy-tabix.
      ENDLOOP.

      CLEAR extract[].

     " Sort the content by desired non-key field
      SORT lt_atank_it_tmp BY <desired non-key field> ASCENDING.
      IF gv_newline IS NOT INITIAL.
        " For a valid line, all the key fields can never be blank
        DELETE lt_atank_it_tmp_view_maint WHERE <key field(s)>   IS INITIAL.
      ENDIF.

"  Move back the sorted contents to Extract table
      LOOP AT lt_atank_it_tmp
        ASSIGNING FIELD-SYMBOL(<fs_tank_it>).

"    Read Table with same index
        READ TABLE lt_extract WITH KEY index = <fs_tank_it>-index
              INTO ls_extract.
        IF sy-subrc = 0.
          APPEND ls_extract-extract TO extract.
        ENDIF.

      ENDLOOP.

    ELSE.
      gv_newline = abap_true.
    ENDIF.
  ENDIF. 

ENDMODULE.
2 Comments
You must be Logged on to comment or reply to a post.
    • Hi Gaurav,

      Thanks a lot for the appreciation.

      Yes, that’s a constraint with all the TMG programming, may be events programming might be helpful but I could not explore much on a suitable event for this. Any experiences/tips ?

      Best Regards,

      Ankit Maskara.