Validate Alternative Units of Measure in LSMW Before Loading

Jimbo's picture

Too often data is loaded into an SAP system only to fail. Documenting these failed records tends to be more difficult and less efficient than predicting what records will fail and, with that information, enriching a report based on the source data. A validation report with this information allows the team that extracts the data to apply corrective transformations before the data is loaded into an SAP system. This is the optimal solution regardless of whether the data is loaded for system testing, unit testing or go-live.

Some material master views and most transactional documents allow for material to be accounted for in a unit of measure that is not the material's base unit of measure. This allows for the production, purchase and sale of a material in feet that is, for example, normally handled in meters.

The base unit of measure is stored in the MEINS field in the MARA table for every material. SAP has built-in conversions where the two units of measure are obviously related. Meters can be converted not only back and forth to centimeters, but to feet and yards. Pounds are automatically calculated when the alternative is kilograms and so on.
Base Unit of Measure on Basic View

An alternate unit of measure is required when handling a material with a base unit of measure that does not have an obvious relation. An example is trying to purchase 100 pounds of a chemical that is stored in one-liter units. Without knowing how many pounds each liter weighs SAP cannot determine how much is to be purchased (or received).

An alternate unit of measure can be added to the material with a serious of clicks and is always related to the base unit of measure of the material. The path to add is transaction MM02 → Basic View → Additional Data → Units of Measure.
How to find the alternative units of measure.

In order to ensure that a transaction doesn't fail during the load process it must be determined beforehand whether or not the material can be handled in the specified unit of measure. One way is to check the unit of measure against the MARM table, but that doesn't necessarily include those that are automatically calculated within SAP like the ones mentioned above.

The alternate unit of measure can be validated against a material before loading with this very simple snippet of code. The first part is to create a few variables in the Mapping and Conversion rules under the Global Data Definitions and Declarations section.

data: nMGVGW like PLFH-MGVGW.
data: lMARA like MARA, lT006A like T006A.

With these variables and the CF_UT_UNIT_CONVERSION function it is possible to test SAP's ability to convert between two different units of measure. This example comes from the Sales Org view of the material master. In order to prevent the transaction from failing because the delivery unit is the same as the base unit of measure the delivery unit will be left blank when it is the same as the base unit of measure.

if IMMH1-MATNR co '1234567890 '.
  shift IMMH1-MATNR right deleting trailing space.
  overlay IMMH1-MATNR with '000000000000000000'.
translate IMMH1-SCHME to UPPER CASE.
select single MEINS into BMMH1-SCHME
 from MARA join T006A on ( MARA~MEINS eq T006A~MSEHI )
   and T006A~SPRAS eq 'E'
   and T006A~MSEH3 eq IMMH1-SCHME.
if sy-subrc eq 0. "Base unit of measure!  Leave blank!
  BMMH1-SCHME = ''.
  "Test to see if a conversion is required.
  nMGVGW = 1.
  select single * from T006A into lT006A
   where SPRAS eq 'E' and MSEH3 eq IMMH1-SCHME.
  select single * from MARA into lMARA where MATNR eq BMM00-MATNR.
      matnr_imp     = BMM00-MATNR
      meins_imp     = lMARA-MEINS
      unit_new_imp  = lT006A-MSEHI
      unit_old_imp  = lMARA-MEINS
      value_old_imp = nMGVGW
      value_new_exp = nMGVGW
      overflow      = 1
      OTHERS        = 2.
  if nMGVGW eq 0.  "Function returns zero when conversion fails.
     'Missing conversion for Alt UoM.'.

The code assumes that the conversion failed when the return value from the function is zero. The PLFH-MGVGW field is a floating point and does not go to zero for values less than one like an integer. For example, when converting from 1 meter to feet the result is 3.281.
The CF_UT_UNIT_CONVERSION function in action

Programming Language: