Predetermine the order of selectable views in MM02 transaction.

Jimbo's picture

Making changes to materials using a recording can be difficult because of the Select Views screen that pops up providing a list of available views. SAP's method for determining the order of selectable views is difficult to understand even after hours spent examining all the tables involved. In the end, the simplest and therefore (according to Occam) best solution is to call SAP's functions for the purpose.

Select View(s)SAP's function returns an internal table with the selectable views ordered just as they appear in the MM02 transaction. There is a lot going on under the hood; the order doesn't match assumptions based on the selection sequences in the T133A table. The selection sequence chosen is based on the Material Type and the material's Industry Sector, but that still doesn't explain the sequence of selectable views.

Passing the Material number and the view description from the Select Views screen causes the form to return the number of the KZSEL selection that represents that view. When a material does not contain the specified view the from returns a zero value. This is also the case when the view description is misspelled.

First, declare an integer in the Global Definitions to hold the numeric value representing the desired view.

data: nKZSEL type i.

Now, call the form to determine where in the sequence the desired view will fall.

perform FindKZSEL using lMATNR 'Accounting 1' changing nKZSEL.

Note: the description is user-language specific. Keep this in mind when incorporating this snippet in LSMW object that will be distributed to other colleagues who might be using SAP in a different language.

*
*  This function finds the number of a view for a material based
*  on the material number and the description of the view on the
*  "Select Views" screen.  If the view doesn't exist for the material
*  then the function returns a zero value.
*
form FindKZSEL
  using cMATNR like MARA-MATNR
        cViewDesc like T133B-DYTXT
        changing nViewKZSEL.
  tables: T133A, MARA, T133B.
  data: cFound(1) type c, lBILDTAB like MBILDTAB, KZ_BILDS_CHANGED,
        BILDSEQUENZ like T133K-BILDS, lvMATNR(18) type c.
  DATA: BEGIN OF BILDTAB OCCURS 30.
          INCLUDE STRUCTURE MBILDTAB.
  DATA: END OF BILDTAB.
*
*  Find the specific material in the MARA table.
*
  move cMATNR to lvMATNR.
  if lvMATNR co ' 1234567890'. "Numeric material needs leading zeros.
    shift lvMATNR right deleting trailing space.
    overlay lvMATNR with '000000000000000000'.
  endif.

  select * from MARA where MATNR eq lvMATNR.
*
*  We default to the screen reference (TRREF) '01' as that's the
*  reference for MM02 and most other transactions.  This is easier than
*  pulling the value from the T130M table for each transaction code.
*  Using the industry and material type from the material we get the
*  "Screen Sequence Number".
*
    CALL FUNCTION 'BILDSEQUENZ_IDENTIFY'
         EXPORTING
              KZRFB            = ' '
              BRANCHE          = MARA-MBRSH
              MATERIALART      = MARA-MTART
              TCODE_REF        = '01'
         IMPORTING
              BILDSEQUENZ      = BILDSEQUENZ
              KZ_BILDS_CHANGED = KZ_BILDS_CHANGED.
*
*  Now we use the "Screen Sequence Number" and the Pflegestatus
*  (Maintenance Status) from the material to build an internal table
*  with the views ordered just like on MM02 "Select Views" screen.
*
    CALL FUNCTION 'SELECTION_VIEWS_FIND'
         EXPORTING
              BILDSEQUENZ  = BILDSEQUENZ
              PFLEGESTATUS = MARA-PSTAT
         TABLES
              BILDTAB      = BILDTAB.

    nViewKZSEL = 0.
*
*  Now, loop through the internal table to find the desired view.
*
   loop at BILDTAB into lBILDTAB.
      if cFound ne 'X'.
        add 1 to nViewKZSEL.
        if lBILDTAB-DYTXT eq cViewDesc.
          cFound = 'X'.  "Stop counting, we have the KZSEL.
        endif.
      endif.
    endloop.
  endselect. "MARA
  if cFound ne 'X'. "View not found!
    nViewKZSEL = 0.
  endif.
endform.
Programming Language: 
ABAP