Handling Disperate Material Masters in MM02 with a Recording

Jimbo's picture

The MM02 transaction is a tough one to produce macros for because the layout of the checkboxes (field KZSEL) on the Select View(s) screen shifts to accommodate additional views. This inconsistency makes a simple recording almost impossible to use for any large number of materials or for a small number of materials of different type. There is a way to predict the layout of the views on the Select View(s) screen and it is relatively simple.

Select ViewsThe MARA table has a field PSTAT that lists the views configured for a given material. In this 15-character field SAP stores a string of letters and each letter represents a different view and the letters are in the order that the views associated with each letter will appear. The details of how each letter relates to a view can be found in the T133A table. By looking at this table we can predict how these PSTAT values will manifest in the Select View(s) screen:

K
Basic Data 1 and Basic Data 2.
KD
Basic Data 1, Basic Data 2, MRP1, MPR2, MPR3, MPR4 and Plant Stock
VLSBGDKE
Basic Data 1, Basic data 2, Sales: Sales Org. Data 1, Sales: Sales Org. Data 2, Sales: General/Plant Data, Foreign Trade: Export Data, Sales and Distribution Text, Purchasing, Foreign Trade: Import Data, Purchase Order Text, MRP 1, MRP 2, MRP 3, MRP 4, General Plant Data / Storage 1, General Plant Data / Storage 2 and Warehouse Management 1

Keep it simple

Recordings, when handled like delicate snowflakes, work wonders where traditional programs dare not tread. Background processing often produces different results than foreground processing and this should be considered whenever planning the creation of a recording. Screens that rely on OLE objects (like the MS Word editor in the sales text view) will cause a well-made recording to fail. Just understanding that some recordings need to run foreground will prevent lost time trying to force a program to run in background processing mode. Resting a coffee cup on the "Enter" key allows for processing that is almost as good as background processing.

Programs in SAP react differently depending on what data is in a screen or any number of variables. A little logic goes a long way in preventing issues. Simple checks against SAP tables to determine what objects actually require a processing with a recording prevents unnecessary processing, saves time and minimizes risk.

Provided here are two examples that use recordings based on the MM02 transaction. The first is relatively simple as it requires no foreknowledge of how the views will be laid out on the Select View(s) screen. The second example uses a routine that allows for the prediction of the layout of the views on the Select View(s) screen.

Example 1

In the Recordings section of LSMW, create a recording calling the MM02 transaction. Start the recording by opening the "Basic Data 1" view from the select screen. In this view a button appears to the right of the available views that allows for the direct selection of the desired view. Click that button and then click on the desired view.
View choosing button

When the desired view appears, populate the fields with some data to ensure that they are available on the recording. Save the material at the end so that the recording ends with a save. The recording will have an BDC_OKCODE in the middle that is associated with the view selected from the Basic Data 1 screen. This BDC_OKCODE will always point to the same view regardless of what views are configured for the material.
Purchasing view recording

Click the "Default All" button and the program will add appropriate field names to the new fields including the KZSEL_01 field. The recording is ready to save and the LSMW object can be fleshed out with a little ABAP code.

Be sure that the KZSEL_01 field is defaulted to "X" . This will ensure that the Basic Data 1 view will be the first screen after the Select View(s) screen.

Example 2

This is a recording made to enter data in the Account 1, Accounting 2, Costing 1 and Costing 2 screens. These screens don't typically appear in the first 17 views in the sequence and SAP shows a maximum of 17 views on the Select View screen. When creating the recording, be sure to use the PgDn key to scroll down in the Select Views screen instead of using the mouse to click on the slider bar; this adds the appropriate code to the recording. Using the slider bar causes the recording to hang when executed. Add all of the unselected KZSEL selection checkboxes to the recording. For this example, the two Accounting views and the two Costing views where selected and the rest were added using Edit → Add Screen Field (Extended).

After adding the the remaining KZSEL fields to the recording after the page down, change the name of the fields to pick up where the first 17 left off.

Add an integer variable in the Global Definitions area. This will hold the number of the KZSEL selection box that represents the desired view.

data: lMARA like MARA, lMARC like MARC, nKZSEL type i.

Note: The code calls this snippet to predict where in the list the desired views will appear in the Select Views screen. This snippet must be included in order for this routine to work.

perform FixMATNR using MM02S-MATNR changing MM02_FINANCE-MATNR.
select single * from MARA into lMARA where MATNR eq MM02_FINANCE-MATNR.
if sy-subrc ne 0. "not in system
  write: / MM02S-MATNR color col_negative, 'not in system.'.
  skip_transaction.
else.
  perform FindKZSEL using MM02_FINANCE-MATNR 'Accounting 1'
   changing nKZSEL.
  case nKZSEL.
    when 10.
       MM02_FINANCE-KZSEL_10 = 'X'. MM02_FINANCE-KZSEL_11 = 'X'.
       MM02_FINANCE-KZSEL_12 = 'X'. MM02_FINANCE-KZSEL_13 = 'X'.
    when 11.
       MM02_FINANCE-KZSEL_11 = 'X'. MM02_FINANCE-KZSEL_12 = 'X'.
       MM02_FINANCE-KZSEL_13 = 'X'. MM02_FINANCE-KZSEL_14 = 'X'.
    when 12.
      MM02_FINANCE-KZSEL_12 = 'X'. MM02_FINANCE-KZSEL_13 = 'X'.
      MM02_FINANCE-KZSEL_14 = 'X'. MM02_FINANCE-KZSEL_15 = 'X'.
    when 13.
      MM02_FINANCE-KZSEL_13 = 'X'. MM02_FINANCE-KZSEL_14 = 'X'.
      MM02_FINANCE-KZSEL_15 = 'X'. MM02_FINANCE-KZSEL_16 = 'X'.
    when 14.
      MM02_FINANCE-KZSEL_14 = 'X'. MM02_FINANCE-KZSEL_15 = 'X'.
      MM02_FINANCE-KZSEL_16 = 'X'. MM02_FINANCE-KZSEL_17 = 'X'.
    when 15.
      MM02_FINANCE-KZSEL_15 = 'X'. MM02_FINANCE-KZSEL_16 = 'X'.
      MM02_FINANCE-KZSEL_17 = 'X'. MM02_FINANCE-KZSEL_18 = 'X'.
    when 16.
      MM02_FINANCE-KZSEL_16 = 'X'. MM02_FINANCE-KZSEL_17 = 'X'.
      MM02_FINANCE-KZSEL_18 = 'X'. MM02_FINANCE-KZSEL_19 = 'X'.
    when 17.
      MM02_FINANCE-KZSEL_17 = 'X'. MM02_FINANCE-KZSEL_18 = 'X'.
      MM02_FINANCE-KZSEL_19 = 'X'. MM02_FINANCE-KZSEL_20 = 'X'.
    when 18.
      MM02_FINANCE-KZSEL_18 = 'X'. MM02_FINANCE-KZSEL_19 = 'X'.
      MM02_FINANCE-KZSEL_20 = 'X'. MM02_FINANCE-KZSEL_21 = 'X'.
    when 19.
      MM02_FINANCE-KZSEL_19 = 'X'. MM02_FINANCE-KZSEL_20 = 'X'.
      MM02_FINANCE-KZSEL_21 = 'X'. MM02_FINANCE-KZSEL_22 = 'X'.
    when 20.
      MM02_FINANCE-KZSEL_20 = 'X'. MM02_FINANCE-KZSEL_21 = 'X'.
      MM02_FINANCE-KZSEL_22 = 'X'. MM02_FINANCE-KZSEL_23 = 'X'.
    when 21.
      MM02_FINANCE-KZSEL_21 = 'X'. MM02_FINANCE-KZSEL_22 = 'X'.
      MM02_FINANCE-KZSEL_23 = 'X'. MM02_FINANCE-KZSEL_24 = 'X'.
    when 22.
      MM02_FINANCE-KZSEL_22 = 'X'. MM02_FINANCE-KZSEL_23 = 'X'.
      MM02_FINANCE-KZSEL_24 = 'X'. MM02_FINANCE-KZSEL_25 = 'X'.
    when 23.
      MM02_FINANCE-KZSEL_23 = 'X'. MM02_FINANCE-KZSEL_24 = 'X'.
      MM02_FINANCE-KZSEL_25 = 'X'. MM02_FINANCE-KZSEL_26 = 'X'.
    when 24.
      MM02_FINANCE-KZSEL_24 = 'X'. MM02_FINANCE-KZSEL_25 = 'X'.
      MM02_FINANCE-KZSEL_26 = 'X'. MM02_FINANCE-KZSEL_27 = 'X'.
    when 25.
      MM02_FINANCE-KZSEL_25 = 'X'. MM02_FINANCE-KZSEL_26 = 'X'.
    when 26.
      MM02_FINANCE-KZSEL_26 = 'X'. MM02_FINANCE-KZSEL_27 = 'X'.
      MM02_FINANCE-KZSEL_28 = 'X'. MM02_FINANCE-KZSEL_29 = 'X'.
    when 27.
      MM02_FINANCE-KZSEL_27 = 'X'. MM02_FINANCE-KZSEL_28 = 'X'.
      MM02_FINANCE-KZSEL_29 = 'X'. MM02_FINANCE-KZSEL_30 = 'X'.
    when others.
      write: / MM02S-MATNR color col_negative,
        'material has no Accounting view.'.
      skip_transaction.
  endcase.
endif.

This is the simplest solution for complicated materials and can be used for any material with any view configuration. Be sure to add the source code FindKZSEL form to the FORM_ROUTINES section of the LSMW source code.

Related articles:
The Workarounds section of SAPLSMW.com
Simulate MM17 transaction using LSMW