Monday, March 16, 2015

ALV Tree Refresh (Delete all Nodes) - The Node Key Problem

Recently i stumbled across a problem in the ALV Tree. When you refresh the Tree you are supposed to delete all nodes

CALL METHOD REF_TREE->DELETE_ALL_NODES.

and add the new Nodes after. The problem here is, that the Tree implementation does not reset the  Node Key Counter. So when you had 248 Elements before the new Tree will start at Key 249(-1?).

The thing is, i wanted to have the same UI after the refresh, meaning all the expanded Nodes should be expanded again. You can read expanded Nodes with REF_TREE->GET_EXPANDED_NODES but it will only give you a list of Keys, which are pretty worthless after the Refresh if you dont want to do complicated calculations, which might fail due to complexity.

You can later Expand the Nodes in the new tree with REF_TREE->EXPAND_NODES. But now we need a connection between the old node keys and the new ones.

Given that you have an itab it_data with unique keys that builds up the tree ist is possible by using that table and an extra itab l_it_expanded with the same structure that holds the expanded data lines instead of only the node keys.


  1. First you read all the expanded nodes GET_EXPANDED_NODES 
  2. Then you read the lines of your data structure into it_expanded using the node keys
    • You need to save the Node Keys of the Tree in the it_data structure when you build it, so you can now find the corresponding line
    • The mt_outtab of the tree would also work, but its protected in the class and i dont know how to access it. Comment if you know how
  3. You do your refresh now
    • Your it_data contains the new data and the new nodekeys now. 
    • The l_it_expanded contains the old expanded data with the worthless node keys
  4. You can now read the new data itab using the real key tuple of your it_expanded
  5. Because you have the node keys saved inside the it_data after the read you kann append to a node key table to add all expanded
    • Also you are able to react on changes like missing lines 
  6. You can now use EXPAND_NODES with you node key table.
here is a code sample how i worked it out. It might be more difficult to read since we are using a framework between ALV Tree and our code.



      "Tabelle für Expandierte Knoten
      DATAL_WA_OUTLINEOLD TYPE TP_T_TREE_TAB.

........

Inside a function for e.g. Text updates

   "Speichern der Expandierten Knoten für Refresh
          CLEAR L_WA_OUTLINEOLD.
          PERFORM CUST_TREE_SAVE_EXPANDED CHANGING L_WA_OUTLINEOLD.

          "Texte über Lagernummer ermitteln
          PERFORM CUST_GET_TXTS_FOR_TRANSPORT USING C_WA_TREE_TAB-TKNUM.

          "LayoutRefresh und Laden der Expandierten Knoten
          PERFORM STD_REFRESH_SELECTION.
          PERFORM CUST_TREE_EXPAND_SAVED_NODES USING L_WA_OUTLINEOLD.



The important functions here are:

*&---------------------------------------------------------------------*
*&      Form  CUST_TREE_SAVE_EXPANDED
*&---------------------------------------------------------------------*
*       Ausgeklappte Knoten für den Refresh speichern
*       Das Problem ist, dass ein Refresh alle Knoten löscht und neu
*       anlegt aber die Schlüssel der Knoten einfach fortzählt.
*       Deshalb sind die gespeicherten Schlüssel unbrauchbar und
*       eine Verbindung muss über die Wertetupel des eigentlichen
*       Tabelleninhalte geschaffen werden um die Zeilen nach dem
*       neuaufbau wieder zu finden.
*----------------------------------------------------------------------*
*      <--C_IT_OUTLINEOLD  Tabelle mit den Realen ausgeklappten zeilen
*----------------------------------------------------------------------*
FORM CUST_TREE_SAVE_EXPANDED
          CHANGING C_IT_OUTLINEOLD TYPE TP_T_TREE_TAB.

  DATA:       L_IT_NODES      TYPE LVC_T_NKEY,
              L_WA_NODE       TYPE LVC_NKEY,
              L_WA_OUTLINEOLD TYPE TP_TREE_TAB.

  "Expandierte Knoten lesen
  CALL METHOD REF_TREE->GET_EXPANDED_NODES
    CHANGING
      CT_EXPANDED_NODES L_IT_NODES
    EXCEPTIONS
      CNTL_SYSTEM_ERROR 1
      DP_ERROR          2
      FAILED            3
      OTHERS            4.
  IF SY-SUBRC <> 0.
    MESSAGE ID SY-MSGID TYPE 'S' NUMBER SY-MSGNO
                       WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4
                         DISPLAY LIKE SY-MSGTY.
  ENDIF.

  "Knoten in Inbhaltstabelle Übersetzen
  LOOP AT L_IT_NODES INTO L_WA_NODE.
    REF_TREE->GET_OUTTAB_LINE(
      EXPORTING
        I_NODE_KEY     =   L_WA_NODE  " node key
      IMPORTING
        E_OUTTAB_LINE  L_WA_OUTLINEOLD
      EXCEPTIONS
        NODE_NOT_FOUND 1
        OTHERS         2
    ).

    IF SY-SUBRC <> 0.
      MESSAGE ID SY-MSGID TYPE 'S' NUMBER SY-MSGNO
                 WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4
                 DISPLAY LIKE SY-MSGTY.
    ENDIF.

    "Durch die Inhaltstabelle mit Wertetupeln wird der Bezug zu den
    "neuen Knoten hergestellt
    APPEND L_WA_OUTLINEOLD TO C_IT_OUTLINEOLD.

  ENDLOOP.


ENDFORM.                    " CUST_TREE_SAVE_EXPANDED
*&---------------------------------------------------------------------*
*&      Form  CUST_TREE_EXPAND_SAVED_NODES
*&---------------------------------------------------------------------*
*       Ausklappen der gespeicherten Tree Zeilen. Ausfühliche besch-
*       reibung des Problem unter CUST_TREE_SAVE_EXPANDED.
*----------------------------------------------------------------------*
*      -->F_IT_OUTLINEOLD  Zeilen zum Ausklappen
*----------------------------------------------------------------------*
FORM CUST_TREE_EXPAND_SAVED_NODES
              USING    F_IT_OUTLINEOLD TYPE TP_T_TREE_TAB.


  DATA:       L_IT_NODES      TYPE LVC_T_NKEY,
              L_WA_NODE       TYPE LVC_NKEY,
              L_WA_OUTLINEOLD TYPE TP_TREE_TAB,
              L_WA_TREETABNEW TYPE TP_TREE_TAB.

  "Inhaltstabelle lesen und die neuen entsprechenden Knoten ausklappen
  LOOP AT F_IT_OUTLINEOLD INTO L_WA_OUTLINEOLD.

    READ TABLE  IT_TREE_TAB
                INTO L_WA_TREETABNEW
                WITH KEY  TKNUM L_WA_OUTLINEOLD-TKNUM
                          VENUM L_WA_OUTLINEOLD-VENUM
                          EXIDV L_WA_OUTLINEOLD-EXIDV
                          LINETYPE L_WA_OUTLINEOLD-LINETYPE.

    IF SY-SUBRC 0.
      APPEND L_WA_TREETABNEW-NODE_KEY TO L_IT_NODES.
    ENDIF.

  ENDLOOP.

  CALL METHOD REF_TREE->EXPAND_NODES
    EXPORTING
      IT_NODE_KEY             L_IT_NODES
    EXCEPTIONS
      FAILED                  1
      CNTL_SYSTEM_ERROR       2
      ERROR_IN_NODE_KEY_TABLE 3
      DP_ERROR                4
      NODE_NOT_FOUND          5
      OTHERS                  6.
  IF SY-SUBRC <> 0.
    MESSAGE ID SY-MSGID TYPE 'S' NUMBER SY-MSGNO
                   WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4
                         DISPLAY LIKE SY-MSGTY.
  ENDIF.

  "Update der Darstellung
  REF_TREE->FRONTEND_UPDATE).

ENDFORM.                    " CUST_TREE_EXPAND_SAVED_NODES


Feel free to comment, especially for better solutions for this problem. I need to add, since the framework is in between, we are not able to use other refresh functions, but i've heard that there is a different solution where you don't use DELETE_ALL_NODES but i think any solutions using the keys might be faulty due to possible changes in the structure resulting in wrong node keys for a line.


Tuesday, January 27, 2015

Dynamic Method Calls and Parameters Tables

I recently needed to call a method dynamically where the name was stored in a table. The call itself is pretty easy. Define a variable to store the name and just use the value in the call method.

You cant simply add a string lo_objekt->(lv_string_method) cause the compiler does not know what its gonna be at runtime and prevents it.

However a CALL METHOD takes a char format anyway and thus is able to work with it.

DATALV_METHOD TYPE STRING,
      LO_OBJECT TYPE REF TO YCL_SYSCPY_AUTOMATION.

CREATE OBJECT LO_OBJECT.

LV_METHOD 'ENTF_SDRUCK_NACHSCHUB'.

CALL METHOD LO_OBJECT->(LV_METHOD).

If you want to pass parameters dynamically, this is also possible by filling a local parameter table.

DATALV_METHOD TYPE STRING,
      LO_OBJECT TYPE REF TO YCL_SYSCPY_AUTOMATION,
      LD_STRING TYPE STRING,
      LS_PARAM TYPE ABAP_PARMBIND,
      LT_PARAMS TYPE ABAP_PARMBIND_TAB.

CREATE OBJECT LO_OBJECT.

LV_METHOD 'ENTF_SDRUCK_NACHSCHUB'.

LS_PARAM-NAME 'IV_PATTERN'.
LS_PARAM-KIND 'E'"Exporting
LD_STRING '*'.
GET REFERENCE OF LD_STRING INTO LS_PARAM-VALUE.
INSERT LS_PARAM INTO TABLE LT_PARAMS.

CALL METHOD LO_OBJECT->(LV_METHOD)
  PARAMETER-TABLE
  LT_PARAMS.


See that the parambind takes references to data as value. So you have to get that reference from any given variable.

To consider are the CX_SY_DYN_CALL_ILLEGAL_METHOD and CX_SY_DYN_CALL_PARAM_NOT_FOUND Exceptions when the Methodname or Params do not match.


So you might end up like this:

DATALV_METHOD TYPE STRING,
      LO_OBJECT TYPE REF TO YCL_SYSCPY_AUTOMATION,
      LD_STRING TYPE STRING,
      LS_PARAM TYPE ABAP_PARMBIND,
      LT_PARAMS TYPE ABAP_PARMBIND_TAB,
      LO_EX_METH TYPE REF TO CX_SY_DYN_CALL_ILLEGAL_METHOD,
      LO_EX_PARAM TYPE REF TO CX_SY_DYN_CALL_PARAM_NOT_FOUND.

CREATE OBJECT LO_OBJECT.

LV_METHOD 'YIF_SYSCPY_AUTO_METHODS~ENTF_SDRUCK_NACHSCHUB'.

LS_PARAM-NAME 'IV_PATTERN'.
LS_PARAM-KIND 'E'"Exporting
LD_STRING '*'.
GET REFERENCE OF LD_STRING INTO LS_PARAM-VALUE.
INSERT LS_PARAM INTO TABLE LT_PARAMS.

TRY.
    CALL METHOD LO_OBJECT->(LV_METHOD)
      PARAMETER-TABLE
      LT_PARAMS.

  CATCH CX_SY_DYN_CALL_ILLEGAL_METHOD  INTO LO_EX_METH.
    MESSAGE E000(YIWITH TEXT-002
                          LO_EX_METH->METHODNAME .

  CATCH CX_SY_DYN_CALL_PARAM_NOT_FOUND INTO LO_EX_PARAM.
    MESSAGE E000(YIWITH TEXT-001
                          LO_EX_PARAM->PARAMETER
                          LO_EX_PARAM->METHODNAME .
ENDTRY.


Monday, November 10, 2014

Use the application log

If you ever wanted to use the Application Log:

Generate the LOG:

 DATAL_VAR_LOGHNDL TYPE BALLOGHNDL,
          L_VAR_MESSAGE_DUMMY TYPE CHAR255,
          L_WA_MSG            TYPE BAL_S_MSG.

    "Generate the log
    DATAL_WA_LOG          TYPE BAL_S_LOG.
    "Basisdaten
    L_WA_LOG-EXTNUMBER  SY-CPROG.
    L_WA_LOG-ALUSER     SY-UNAME.
    L_WA_LOG-ALPROG     SY-REPID.
    L_WA_LOG-ALDATE     SY-DATUM.
    L_WA_LOG-ALTIME     SY-UZEIT.
    L_WA_LOG-ALTCODE    SY-TCODE.
    L_WA_LOG-OBJECT     'YARTSTAMM'.
    L_WA_LOG-SUBOBJECT  'STMMDWL'.
    "Modus setzen
    IF SY-BATCH EQ 'X'.
      L_WA_LOG-ALMODE 'B'.
    ELSEIF SY-BINPT EQ 'X'.
      L_WA_LOG-ALMODE 'I'.
    ELSE.
      L_WA_LOG-ALMODE 'D'.
    ENDIF.
    "Create
    CALL FUNCTION 'BAL_LOG_CREATE'
      EXPORTING
        I_S_LOG      L_WA_LOG
      IMPORTING
        E_LOG_HANDLE L_VAR_LOGHNDL
      EXCEPTIONS
        OTHERS       1.
    IF SY-SUBRC <> 0.
      " Ignore Error
    ENDIF.

Add Messages like this:

 "Insert Message
    MESSAGE E180(YIWITH I_VAR_MATNR I_VAR_MATKL
                        INTO L_VAR_MESSAGE_DUMMY.

    CLEAR L_WA_MSG.
    L_WA_MSG-MSGTY 'E'.
    L_WA_MSG-MSGID SY-MSGID.
    L_WA_MSG-MSGNO SY-MSGNO.
    L_WA_MSG-MSGV1 SY-MSGV1.
    L_WA_MSG-MSGV2 SY-MSGV2.
    L_WA_MSG-MSGV3 SY-MSGV3.
    L_WA_MSG-PROBCLASS '1'.

    CALL FUNCTION 'BAL_LOG_MSG_ADD'
      EXPORTING
        I_S_MSG          L_WA_MSG
      EXCEPTIONS
        LOG_NOT_FOUND    1
        MSG_INCONSISTENT 2
        LOG_IS_FULL      3
        OTHERS           4.
    IF SY-SUBRC <> 0.
      "Ignore Errors
    ENDIF.

Don't forget to save the Log in the End:

*   SAVE LOG
    DATA L_IT_LOG_HANDEL TYPE BAL_T_LOGH.
    APPEND L_VAR_LOGHNDL TO L_IT_LOG_HANDEL.
    CALL FUNCTION 'BAL_DB_SAVE'
      EXPORTING
        I_T_LOG_HANDLE L_IT_LOG_HANDEL
      EXCEPTIONS
        OTHERS         1.
    IF SY-SUBRC <> 0.
      "Ignore Errors
    ENDIF.

In Programms you can easily encapsulate everything inside Forms. Inside functions i would recommend creating a helper class and instanciate it.

We also are using function pools as Log Helper. The problem with them might be, that only instance exists. So if you use any kind of reset method and keep a handle inside the pool it might get reset by nested calls inside different functions.

QRFC - Background task Queues

A quick HowTo on Background-Task queues for functions.

**Background Queue variables
  DATAL_TCODE_ID   TYPE ARFCTID,
        L_QNAME      TYPE TRFCQOUT-QNAME..

* Generate Name
  CONCATENATE SY-TCODE '_' SY-DATUM '_' SY-UZEIT INTO L_QNAME.
  CONDENSE L_QNAME NO-GAPS.

* Set Queue name in Programm
  CALL FUNCTION 'TRFC_SET_QUEUE_NAME'
    EXPORTING
      QNAME L_QNAME.

* Start your queue Transaction
  CALL FUNCTION 'TRANSACTION_BEGIN'
    IMPORTING
      TRANSACTION_ID L_TCODE_ID.

...... Do some Codng in your programm, and somewhere start your function in Background Task:

 CALL FUNCTION 'ARTICLE_RECLASSIFY_LITE_RETAIL' IN BACKGROUND TASK
      EXPORTING
        I_MATNR              MARA-MATNR
        I_NEWWG              SET_MATKL
        I_TEST               P_TEST
        I_NEWPROFIL          DMY_NEWPROF

....

        CALL FUNCTION 'Y_ISR_SET_REKLA_YPOSDWL' IN BACKGROUND TASK
          EXPORTING
            I_VAR_MATNR    MARA-MATNR
            I_VAR_MATKL    SET_MATKL.


..... Do some more Coding in the programm and close the Transaction somewhere

  CALL FUNCTION 'TRANSACTION_END'
    EXPORTING
      TRANSACTION_ID L_TCODE_ID.


Call SMQ1 to se open Queues, for e.g. if some error occured.

Friday, November 7, 2014

ALV Grids in Reports

Sometimes you want to use an ALV Grid directly as output in a report (not a Dynpro).

The magic function to this is:

**ALVGrid 
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      IT_FIELDCAT   L_IT_FIELDCAT
      I_GRID_TITLE  L_VAR_TITLE
    TABLES
      T_OUTTAB      <L_VAR_OUTTAB>
    EXCEPTIONS
      PROGRAM_ERROR 1
      OTHERS        2.
  IF SY-SUBRC <> 0.
    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.

You can generate your Fieldcat wth another Helpful function:
**Generate Fieldcat
  CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
    EXPORTING
      I_PROGRAM_NAME         SY-CPROG
      I_INTERNAL_TABNAME     L_VAR_TABNAME
      I_INCLNAME             SY-CPROG
    CHANGING
      CT_FIELDCAT            L_IT_FIELDCAT
    EXCEPTIONS
      INCONSISTENT_INTERFACE 1
      PROGRAM_ERROR          2
      OTHERS                 3.
  IF SY-SUBRC <> 0.
    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.

As you can see here,the Fieldcat is generated from a local internam table. It is mandatory to give the include the where the table is defined. If the data structure is not in DDIC but local as well you need to give the include where it is defined, or include it in TOP. Very important is, that local structures are only read correctly when defined with LIKE and INCLUDE structure as Datatypes. TYPES and normalINCLUDE wont work, since the ddic information is not read there. Its also written in the Manual of the function. So always RTFM cause i did it way too late ;)

Here a sample as an MBEWH Extension is locally defined:

DATABEGIN OF TP_MBEWH_EXTENDED.
" MBEWH
        INCLUDE STRUCTURE MBEWH.
" MARA with Key MATNR
DATA:     YYBEZBE   LIKE  MARA-YYBEZBE,
          MTART     LIKE  MARA-MTART,
          MATKL     LIKE  MARA-MATKL,
          MEINS     LIKE  MARA-MEINS,
"T001 with Key BUKRS from T001K  with Key BWKEY
          WAERS     LIKE  T001-WAERS,
"T023T with Key MATKL
          WGBEZ     LIKE  T023T-WGBEZ.
DATAEND OF TP_MBEWH_EXTENDED.

I also discussed reordering the Catalog from the selection screen here:
http://abapify.blogspot.de/2014/10/alv-sorting-with-reordering-of-fieldcat.html



As Update here is a small piece of Code to dynamically alocate Tables and Fieldcats to 3 different Tables:

FORM BUILD_ALV.

  CHECK P_TABLE IS NOT INITIAL.

  DATAL_IT_FIELDCAT       TYPE SLIS_T_FIELDCAT_ALV,
        L_VAR_TABNAME       TYPE SLIS_TABNAME,
        L_VAR_TITLE         TYPE LVC_TITLE,
        L_VAR_LINE_CNT_OUT  TYPE LENGTH 20.

  FIELD-SYMBOLS<L_VAR_OUTTAB> TYPE STANDARD TABLE.

**Je nach Auwahl der Tabelle wird die Struktur für den Feldkatalog und
* die zugehörige Ergebnis iTab  für das ALV gewählt.
  CASE P_TABLE.
    WHEN 'S31'.
      L_VAR_TABNAME 'TP_S031_EXTENDED'.
      ASSIGN IT_S031_EXTENDED  TO <L_VAR_OUTTAB>.
    WHEN 'S32'.
      L_VAR_TABNAME 'TP_S032_EXTENDED'.
      ASSIGN IT_S032_EXTENDED  TO <L_VAR_OUTTAB>.
    WHEN 'MBH'.
      L_VAR_TABNAME 'TP_MBEWH_EXTENDED'.
      ASSIGN IT_MBEWH_EXTENDED TO <L_VAR_OUTTAB>.
  ENDCASE.

**Feldkatalog schlicht nach Tabelle erstellen
  CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
    EXPORTING
      I_PROGRAM_NAME         SY-CPROG
      I_INTERNAL_TABNAME     L_VAR_TABNAME
      I_INCLNAME             SY-CPROG
    CHANGING
      CT_FIELDCAT            L_IT_FIELDCAT
    EXCEPTIONS
      INCONSISTENT_INTERFACE 1
      PROGRAM_ERROR          2
      OTHERS                 3.
  IF SY-SUBRC <> 0.
    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.

  "Feldkatalog individuelle Sortierung
  CASE P_TABLE.
    WHEN 'S31'.
      PERFORM REORDER_FIELDCAT_S031 CHANGING L_IT_FIELDCAT.
    WHEN 'S32'.
      PERFORM REORDER_FIELDCAT_S032 CHANGING L_IT_FIELDCAT.
    WHEN 'MBH'.
      PERFORM REORDER_FIELDCAT_MBEWH CHANGING L_IT_FIELDCAT.
  ENDCASE.

*Anzahl der Zeilen
  DESCRIBE TABLE <L_VAR_OUTTAB> LINES L_VAR_LINE_CNT_OUT.

***Tabellenname in den Titel nehmen
*  CONCATENATE TEXT-100 L_VAR_TABNAME L_VAR_LINE_CNT_OUT TEXT-101
*      INTO L_VAR_TITLE RESPECTING BLANKS.


**ALVGrid generieren.
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      IT_FIELDCAT   L_IT_FIELDCAT
      I_GRID_TITLE  L_VAR_TITLE
    TABLES
      T_OUTTAB      <L_VAR_OUTTAB>
    EXCEPTIONS
      PROGRAM_ERROR 1
      OTHERS        2.
  IF SY-SUBRC <> 0.
    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.

ENDFORM.                    "DISPLAY_ALV_REPORT



Then at last you might want to reorder the Fieldcat positions in your Output afterwards. Therem ight be better ways to do this (comments welcome), but most of what i tried before was very complicated an not error prone. The problem are some Entry which are also hidden an not seen and mostly have empty Tabnames. So i just went and took the First entry i knew, reordered everything from this Sy-Tabix on forward and renumerated everythin in the end.

*&---------------------------------------------------------------------*
*&      Form  REORDER_FIELDCAT_MBEWH
*&---------------------------------------------------------------------*
* Feldkatalog anpassungen:
*----------------------------------------------------------------------*
*      <--P_L_IT_FIELDCAT  Feldcatalog zu ändern
*----------------------------------------------------------------------*
FORM REORDER_FIELDCAT_MBEWH CHANGING C_IT_FIELDCAT
                                    TYPE SLIS_T_FIELDCAT_ALV.


  DATAL_WA_FIELDCAT_TMP TYPE SLIS_FIELDCAT_ALV,
        L_VAR_TABIX       TYPE SY-INDEX,
        L_VAR_COL_POS_CNT TYPE VALUE 1.

  FIELD-SYMBOLS <FS_WA_FIELDCAT_POS> TYPE SLIS_FIELDCAT_ALV.

  "Index der Material Spalte lesen (MATNR)
  READ TABLE  C_IT_FIELDCAT
           WITH KEY FIELDNAME 'MATNR' TRANSPORTING NO FIELDS.
  L_VAR_TABIX SY-TABIX + .

  "WGBEZ
  READ TABLE  C_IT_FIELDCAT
            INTO L_WA_FIELDCAT_TMP
            WITH KEY FIELDNAME 'WGBEZ'.
  IF SY-SUBRC 0.
    "Löschen und neu einfügen
    DELETE C_IT_FIELDCAT
        WHERE FIELDNAME 'WGBEZ'.
    INSERT L_WA_FIELDCAT_TMP  INTO C_IT_FIELDCAT INDEX L_VAR_TABIX.
  ENDIF.

  "MATKL verschieben
  READ TABLE  C_IT_FIELDCAT
              INTO L_WA_FIELDCAT_TMP
              WITH KEY FIELDNAME 'MATKL'.
  IF SY-SUBRC 0.
    "Löschen und neu einfügen
    DELETE C_IT_FIELDCAT
        WHERE FIELDNAME 'MATKL'.
    INSERT L_WA_FIELDCAT_TMP  INTO C_IT_FIELDCAT INDEX L_VAR_TABIX.
  ENDIF.

  "MTART
  READ TABLE  C_IT_FIELDCAT
            INTO L_WA_FIELDCAT_TMP
            WITH KEY FIELDNAME 'MTART'.
  IF SY-SUBRC 0.
    "Löschen und neu einfügen
    DELETE C_IT_FIELDCAT
        WHERE FIELDNAME 'MTART'.
    INSERT L_WA_FIELDCAT_TMP  INTO C_IT_FIELDCAT INDEX L_VAR_TABIX.
  ENDIF.

  "MAKTX
  READ TABLE  C_IT_FIELDCAT
            INTO  L_WA_FIELDCAT_TMP
            WITH KEY FIELDNAME 'MAKTX'.
  IF SY-SUBRC 0.
    "Löschen und neu einfügen
    DELETE C_IT_FIELDCAT
        WHERE FIELDNAME 'MAKTX'.
    INSERT L_WA_FIELDCAT_TMP  INTO C_IT_FIELDCAT INDEX L_VAR_TABIX.
  ENDIF.



  "Index der Periode Spalte lesen (LFMON)
  READ TABLE  C_IT_FIELDCAT
           WITH KEY FIELDNAME 'LFMON' TRANSPORTING NO FIELDS.
  L_VAR_TABIX SY-TABIX + .

  "WAERS
  READ TABLE  C_IT_FIELDCAT
            INTO L_WA_FIELDCAT_TMP
            WITH KEY FIELDNAME 'WAERS'.
  IF SY-SUBRC 0.
    "Löschen und neu einfügen
    DELETE C_IT_FIELDCAT
        WHERE FIELDNAME 'WAERS'.
    INSERT L_WA_FIELDCAT_TMP  INTO C_IT_FIELDCAT INDEX L_VAR_TABIX.
  ENDIF.

  "MEINS
  READ TABLE  C_IT_FIELDCAT
            INTO  L_WA_FIELDCAT_TMP
            WITH KEY FIELDNAME 'MEINS'.
  IF SY-SUBRC 0.
    "Löschen und neu einfügen
    DELETE C_IT_FIELDCAT
        WHERE FIELDNAME 'MEINS'.
    INSERT L_WA_FIELDCAT_TMP  INTO C_IT_FIELDCAT INDEX L_VAR_TABIX.
  ENDIF.

  "Positionen neu schreiben
  LOOP AT C_IT_FIELDCAT ASSIGNING <FS_WA_FIELDCAT_POS>
     WHERE TABNAME NE ''.
    <FS_WA_FIELDCAT_POS>-COL_POS L_VAR_COL_POS_CNT.
    L_VAR_COL_POS_CNT L_VAR_COL_POS_CNT + 1.
  ENDLOOP.

ENDFORM.                    " REORDER_FIELDCAT_MBEWH

Monday, November 3, 2014

Smartforms: Asterisk in Page numbering

I recently had the Problem that when used page numbering in Smartform like this

&SFSY-FORMPAGES& / &SAPSCRIPT-FORMPAGES&

in long documents it showed:

1/*
2/*

but later

10/25
11/25

It seems that the length of the second part FORMPAGES is related to the length of the first part, and that why there is not enough space anymore for the 25. * is typically used as a starting char, when there is content longer than the space available.

So i found an override for this with formatting options.

&SFSY-FORMPAGES(3ZC)& / &SAPSCRIPT-FORMPAGES(3ZC)&

where :
3 = output length of data
Z = suppress leading zeroes
C = compress blank spaces

Of course i want to credit the source where i found it:
http://searchsap.techtarget.com/answer/When-asterisks-attack-in-the-page-numbering-window


Thursday, October 16, 2014

ALV Sorting with reordering of Fieldcat

We recently had a requests in a report to offer sorting options at selection time (not in the ALV Grid later).

So basically what you do is offer Parameters for Sortorder or even better a restricted Select-Option if you have a Domain of the Columns.

Then you have a iTab representing the Sort Order in the ata Definition.

TYPESBEGIN  OF SORT_HELP,
          POS     TYPE N LENGTH 2,
          TABNAME TYPE YI_BDSCOLS.
TYPESEND    OF SORT_HELP.


DATA: IT_SORTLIST          TYPE TABLE OF SORT_HELP,
     VAR_SORTITEM         TYPE SORT_HELP.


You can write a Function to add Columns to the sorting

*&---------------------------------------------------------------------*
*&      Form  ADD_SORTITEM
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->F_POS  Position of Col in Sorting
*      -->F_TABNAME Col Name
*----------------------------------------------------------------------*
FORM CUST_ADD_SORTITEM  USING    F_POS TYPE N
                            F_TABNAME TYPE CHAR20.

  IF F_POS > .
    CLEAR VAR_SORTITEM.
    READ TABLE IT_SORTLIST WITH KEY POS F_POS TRANSPORTING NO FIELDS.
    "Wenn Schlüssel schon vorhanden haben wir eine doppelte Zeile
    IF SY-SUBRC 0.
        MESSAGE E685(yb).
    ELSE.
      VAR_SORTITEM-TABNAME F_TABNAME.
      VAR_SORTITEM-POS F_POS.
      APPEND VAR_SORTITEM TO IT_SORTLIST.
      SORT IT_SORTLIST BY POS ASCENDING.
    ENDIF.

  ENDIF.

ENDFORM.                    " ADD_SORTITEM


And wherever you fill your ALV Grid you generate the Sorting upfront:

*# Sort
  DATA L_WA_SORT                  TYPE LVC_S_SORT.          "#EC *

  CLEAR C_T_SORTREFRESH C_T_SORT.

  LOOP AT IT_SORTLIST INTO VAR_SORTITEM.

    CLEARL_WA_SORT.
    L_WA_SORT-SPOS        SY-TABIX.           "Level of Sortierung
    L_WA_SORT-FIELDNAME   VAR_SORTITEM-TABNAME.  "Sortcolumn
    L_WA_SORT-UP          'X'.         "Sortierung ascending
    L_WA_SORT-DOWN        ' '.         "Sortierung descending
    APPEND L_WA_SORT TO C_T_SORT.

  ENDLOOP.

The neat thing now is, that you also are able to redo the Fieldcat of the ALV Grid in Order of the Sorting upfront.

I did it like this, there may be a better way. Comments are welcome.



  DATAL_IT_FIELDCAT_NEW TYPE LVC_T_FCAT,
        L_VAR_COL_POS_CNT TYPE VALUE 1.

  FIELD-SYMBOLS <L_WA_FIELDCAT_COL> TYPE LVC_S_FCAT.

  "Copy all FieldCat Fields not associated with viewable Cols
  LOOP AT C_T_FIELDCAT ASSIGNING <L_WA_FIELDCAT_COL>
      WHERE TABNAME EQ ''.
    APPEND <L_WA_FIELDCAT_COL> TO L_IT_FIELDCAT_NEW.
    DELETE C_T_FIELDCAT.
  ENDLOOP.

  "Insert Cols from the SortList and remove them from the initial Fieldcat
  LOOP AT IT_SORTLIST INTO VAR_SORTITEM.
    READ TABLE C_T_FIELDCAT
                WITH KEY FIELDNAME VAR_SORTITEM-TABNAME
                INTO L_WA_FIELDCAT.
    IF SY-SUBRC 0.
      DELETE C_T_FIELDCAT
        WHERE FIELDNAME VAR_SORTITEM-TABNAME.
    ENDIF.
    APPEND L_WA_FIELDCAT TO L_IT_FIELDCAT_NEW.
  ENDLOOP.

  "Now add all Cols from the Initial Fieldcat after Sorted Ones
  LOOP AT C_T_FIELDCAT ASSIGNING <L_WA_FIELDCAT_COL> .
    APPEND <L_WA_FIELDCAT_COL> TO L_IT_FIELDCAT_NEW.
  ENDLOOP.

  "At last you have to assign new Col_Positions for all Entriew in the FieldCat
  LOOP AT L_IT_FIELDCAT_NEW ASSIGNING <L_WA_FIELDCAT_COL>
    WHERE TABNAME NE ''.
    <L_WA_FIELDCAT_COL>-COL_POS L_VAR_COL_POS_CNT.
    L_VAR_COL_POS_CNT L_VAR_COL_POS_CNT + 1.
  ENDLOOP.

  C_T_FIELDCAT L_IT_FIELDCAT_NEW.


There you go :)