SQL Server administration and T-SQL development, Web Programming with ASP.NET, HTML5 and Javascript, Windows Phone 8 app development, SAP Smartforms and ABAP Programming, Windows 7, Visual Studio and MS Office software
Development resources, articles, tutorials, code samples, tools and downloads for SAP HANA and ABAP, HANA Database, SQLScript, SAP UI5, Screen Personas, Web Dynpro, Workflow

Convert Units of Measurement using ABAP Function Module for Weight

Using ABAP function modules, SAP programmers can convert measurement unit values into other units of measurements. Weight, currency, lengths, and such measurement units are stored in SAP tables as value and corresponding measurement unit. ABAP programmer can easily convert a value in a given measurement unit into an other unit of measurement using ABAP MC_UNIT_CONVERSION function module.

In the following ABAP code, as SAP developers can easily understand a delivery document is read from SAP LIKP table. The delivery header mass measurements, the total weight and net weight of all delivery items is read in the document measurement unit.
These values are stored in btgew, ntgew and gewei fields of LIKP table in SAP.
Then I display these weight amounts and unit of measurement on the screen.

DATA lv_vbeln TYPE vbeln VALUE '8310000010'.

SELECT SINGLE vbeln, btgew, ntgew, gewei
 FROM likp INTO @DATA(ls_likp)
 WHERE vbeln = @lv_vbeln.

WRITE:/ ls_likp-btgew, ls_likp-gewei, 'in source delivery document'.

In the second part of the sample ABAP program, I read all mass related measurement units into an internal table temporarily.

The units of measurements used in SAP system are stored in T006 transparent ABAP table.
For the unit texts in desired languages, ABAP programmer will find the translations or measurement unit texts in T006A SAP transparent table.

DATA lv_source_gewei TYPE gewei.
DATA lv_faktor TYPE f.
DATA lv_weight_in_target_unit TYPE gsgew . " total weight
lv_source_gewei = ls_likp-gewei.

* SAP Transparent Table: Units of Measurement
SELECT t006~msehi, t006a~msehl
 INTO TABLE @DATA(lt_t006)
 FROM t006 " Units of Measurement
 INNER JOIN t006a " Assign Internal to Language-Dependent Unit
  ON t006a~msehi = t006~msehi " Unit of Measurement
 WHERE dimid = 'MASS' " Dimension key
  AND spras = 'E'.

The last part of the sample ABAP code contains the code block where unit conversion takes place.

As programmers can see, in an ABAP Loop command I use ABAP function module MC_UNIT_CONVERSION to convert the source unit of measurement with the target measurement unit I got in the referenced Loop structure.

MC_UNIT_CONVERSION ABAP function module returns a multiplication factor which is used to calculate the new weight amount in the target unit of measurement.
To summarize:
target amount (in target measurement unit) = source amount (in source unit) * factor (returned in umref parameter)


* MATNR = ' '
   nach_meins = lr_t006->msehi " target unit
   von_meins = lv_source_gewei " source unit, ls_likp-gewei
   umref = lv_faktor
   conversion_not_found = 1
   material_not_found = 2
   nach_meins_missing = 3
   overflow = 4
   von_meins_missing = 5
   OTHERS = 6.
 IF sy-subrc <> 0.
* Implement suitable error handling here
  " convert initial weight to target unit of weight
  lv_weight_in_target_unit = ls_likp-btgew * lv_faktor.

  WRITE:/ lv_weight_in_target_unit, lr_t006->msehi, lr_t006->msehl.


Below is the overall representation of fragmented ABAP source codes to see the SAP program easily.

ABAP codes for sample program to convert measurement units

And the above example ABAP program generates following output.

convert units in ABAP using function module

As you see, the initial line has the source values (mass in KG) read from the delivery document header. And in the ABAP Loop statement using ABAP MC_UNIT_CONVERSION function module, the source weight is converted into every possible mass unit defined in the SAP system.


Install SAP Free
CRM Companies List
Web Based CRM Software

Copyright © 2004 - 2021 Eralper YILMAZ. All rights reserved.