Generate a Report with Material Master Long Texts

Jimbo's picture

http://www.apogeephoto.com/oct2013/Long_photos/rubbing-off-goat-mtn-goats_5392.jpg|http://www.wakeolda.com/Photos/mtevans99/goat_climbing_2.jpg|http://bloximages.chicago2.vip.townnews.com/missoulian.com/content/tncms/assets/v3/editorial/2/95/295319a2-dd10-11e2-aaa0-0019bb2963f4/51c8b1d9b08cf.image.jpg|http://ngm.nationalgeographic.com/u/TvyamNb-BivtNwcoxtkc5xGBuGkIMh_nj4UJHQKuorjj2yrzzTsBkwgD8q53LHYCkF7IMbQbjmJgWA/|http://www.maxingout.com/images/100%20update/big-horn-goat.jpg|http://cache2.allpostersimages.com/p/LRG/28/2806/OUCOD00Z/posters/edwards-walter-meayers-close-view-of-a-goat-with-long-horns.jpgBecause of the cumbersome way that SAP stores long texts it is not possible to access them with a custom query, with nested select statements or even with complex joins. The only way to access the long texts for the purpose of a report is through the READ_TEXT function.

The three most frequently-requested long texts are Sales Text, Purchase Order Text and Basic View Text. The usage of the READ_TEXT function is almost identical for each of these types of text. The parameters are:

READ_TEXT Sales Purchase Order Basic View
ID 0001 BEST GRUN
OBJECT MVKE MATERIAL MATERIAL



The remainder of the formula is the NAME (the actual object) and the SPRAS (language). When collecting the Sales Text for a Material Master the function expects the concatenated Material number, the Sales Organization number and the Distribution Channel as a single text value.

The first step is to create a selection screen that allows for the selection of what Materials should appear in the report and the type long text to be included in the report. In this case the screen will be relatively complex with SELECT-OPTIONS, RADIOBUTTON and CHECKBOX parameters. It is important to note that the tables MARA, MVKE and T002 are declared and cannot be used in User Defined Routines.

tables: mara, mvke, T002.
SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME TITLE btext1.
  SELECT-OPTIONS: s_matnr FOR mara-matnr,     "Material Number
                  s_vkorg FOR mvke-vkorg,     "Sales Organization
                  s_vtweg FOR mvke-vtweg,     "Distribution Channel
                  s_SPRAS for T002-SPRAS.
  selection-screen begin of line.
    PARAMETERS: rSales radiobutton group r1 default 'X'.
    selection-screen comment 3(50) comms.
  selection-screen end of line.
  selection-screen begin of line.
    PARAMETERS: rPurch radiobutton group r1.
    selection-screen comment 3(50) commp.
  selection-screen end of line.
  selection-screen begin of line.
    PARAMETERS: rBasic radiobutton group r1.
    selection-screen comment 3(50) commb.
  selection-screen end of line.
SELECTION-SCREEN END OF BLOCK B1.
SELECTION-SCREEN BEGIN OF BLOCK B2 WITH FRAME TITLE btext2.
  parameters p_break as checkbox.
SELECTION-SCREEN END OF BLOCK B2.
selection-screen begin of block bfile with frame title bfile1.
  selection-screen begin of line.
    selection-screen comment 1(10) commf.
    PARAMETERS: p_filenm type string.
  selection-screen end of line.
  selection-screen begin of line.
    selection-screen comment 1(60) commf2.
  selection-screen end of line.
selection-screen end of block bfile.

initialization.
btext1 = 'Report parameters'.
comms = 'Sales Texts'.
commp = 'Purchase Order Texts'.
commb = 'Long text (Basic View Text).'.
btext2 = 'Testing tools'.
bfile1 = 'Export to file'.
commf = 'Filename:'.
commf2 = 'The filename is optional.'.

http://www.animalspot.net/wp-content/uploads/2012/03/Nubian-Ibex.jpg|http://album.udn.com/community/img/PSN_PHOTO/debby927/f_3764285_1.jpg|https://upload.wikimedia.org/wikipedia/commons/thumb/e/ed/Capra_ibex_ibex_%E2%80%93_03.jpg/400px-Capra_ibex_ibex_%E2%80%93_03.jpg|https://upload.wikimedia.org/wikipedia/commons/thumb/6/6a/Bouquetin_adulte_couch%C3%A9.jpg/800px-Bouquetin_adulte_couch%C3%A9.jpg|https://pixabay.com/static/uploads/photo/2015/12/11/16/12/animals-1088374_960_720.jpgNow the variables to be used while generating the report are declared. The most significant one is the it_Report internal table that will hold all of the long texts that come from the READ_TEXT function.

Declaring structures to hold data from queried tables makes it easy to manipulate the data without declaring a lot of custom variables and structures. Using a select * from statement would have been frowned upon decades ago, but with modern computing, it means the difference between 1 millisecond and 2 milliseconds.

data: it_MVKE type standard table of MVKE initial size 0.
data: lMVKE like MVKE, lMAKT like MAKT, lT002 like T002,
 lTDNAME like THEAD-TDNAME,
 it_TLINE type standard table of TLINE,
 wa_TLINE type TLINE, lText type string.

types: begin of i_Report,
        MATNR(18) type c,
        MAKTX like MAKT-MAKTX,
        VKORG(5) type c,
        VTWEG(5) type c,
        SPRAS(2) type c,
        Text type string,
       end of i_Report.
data: wa_Report type i_Report,
      it_Report type standard table of i_Report initial size 0.
data: cID(4) type c, cObject(10) type c.

The next step is to populate the parameters for the desired long texts. The table above lists the values that must be passed into the function.

if rSales eq 'X'.
  cID = '0001'. cObject = 'MVKE'.
  select * from MVKE into table it_MVKE
   where MATNR in s_MATNR and VKORG in s_VKORG and VTWEG in s_VTWEG
   order by MATNR VKORG VTWEG.
elseif rPurch eq 'X'.
  select MATNR from MARA into corresponding fields of table it_MVKE
   where MATNR in s_MATNR order by MATNR.
  cID = 'BEST'. cObject = 'MATERIAL'.
else. "Basic View Text . . .
  select MATNR from MARA into corresponding fields of table it_MVKE
   where MATNR in s_MATNR order by MATNR.
  cID = 'GRUN'. cObject = 'MATERIAL'.
endif.

If the first line of the it_Report table is populated with the field names then it looks like column headers. This makes the report much easier to read.

wa_Report-MATNR = 'MATNR'.
wa_Report-MAKTX = 'MAKTX'.
wa_Report-VKORG = 'VKORG'.
wa_Report-VTWEG = 'VTWEG'.
wa_Report-SPRAS = 'SPRAS'.
wa_Report-Text = 'Text'.
append wa_Report to it_Report.

The task of populating the parameters of the READ_TEXT function for each Material is trivial at this point. When the material has the desired long text (SY-SUBRC eq 0) the returned values are moved to the it_Report internal table.

loop at it_MVKE into lMVKE.
  if rSales eq 'X'.
    concatenate lMVKE-MATNR lMVKE-VKORG lMVKE-VTWEG into lTDNAME.
  else.
    lTDNAME = lMVKE-MATNR.
  endif.
  select * from T002 into lT002 where SPRAS in s_SPRAS.

    CALL FUNCTION 'READ_TEXT'
      EXPORTING
        CLIENT                  = SY-MANDT
        ID                      = cID
        LANGUAGE                = lT002-SPRAS  "p_SPRAS
        NAME                    = lTDNAME
        OBJECT                  = cObject
      TABLES
        LINES                   = it_TLINE
      EXCEPTIONS
        ID                      = 1
        OTHERS                  = 8.

    IF SY-SUBRC EQ 0.
      select single * from MAKT into lMAKT where MATNR eq lMVKE-MATNR.
      select single * from MAKT into lMAKT where MATNR eq lMVKE-MATNR
       and SPRAS eq lT002-SPRAS.  "p_SPRAS.
      wa_Report-MATNR = lMVKE-MATNR.
      wa_Report-MAKTX = lMAKT-MAKTX.
      wa_Report-VKORG = lMVKE-VKORG.
      wa_Report-VTWEG = lMVKE-VTWEG.
      wa_Report-SPRAS = lT002-SPRAS.  "p_SPRAS.
      LOOP AT it_TLINE INTO wa_TLINE.
        wa_Report-TEXT = wa_TLINE-TDLINE.
        append wa_Report to it_Report.
      ENDLOOP.
    ENDIF.
  endselect.
endloop.

Writing the report to the screen is as easy as looping through the it_Report table and writing out the values. The first row is the column headers that were added above.

loop at it_Report into wa_Report.
  write: / wa_Report-MATNR, wa_Report-MAKTX,
   wa_Report-VKORG, wa_Report-VTWEG, wa_Report-SPRAS, wa_Report-Text.
endloop.

Exporting an internal table to a tab-delimited file is very simple. Because of the first row that was added at the beginning of the internal table there is no reason to add a header row to the exported table.

if p_Filenm ne ''.  "Export to file!
  CALL FUNCTION 'GUI_DOWNLOAD'
    EXPORTING
      FILENAME                        = p_filenm
      FILETYPE                        = 'ASC'
      APPEND                          = ''
*      CODEPAGE                        = 'IBM'
      WRITE_FIELD_SEPARATOR = 'X'
      HEADER = '00'
      TRUNC_TRAILING_BLANKS = 'X'
    TABLES
      DATA_TAB                        = it_Report
    EXCEPTIONS
      FILE_WRITE_ERROR                = 1
      OTHERS                          = 2.
endif.

Load Characteristic Values with a BAPIDownload this project

The LSMW object in its entirety is available here. No additional LSMW objects or source code is required to make this work.
LSMW_MaterialMasterReports.txt

http://www.animated-gifs.eu/category_graphics/perfect-loops/0046.gif|http://www.animated-gifs.eu/category_graphics/perfect-loops/0055.gif|http://www.animated-gifs.eu/category_graphics/perfect-loops/0057.gif|http://www.apogeephoto.com/oct2013/Long_photos/rubbing-off-goat-mtn-goats_5392.jpg|http://www.wakeolda.com/Photos/mtevans99/goat_climbing_2.jpg|http://bloximages.chicago2.vip.townnews.com/missoulian.com/content/tncms/assets/v3/editorial/2/95/295319a2-dd10-11e2-aaa0-0019bb2963f4/51c8b1d9b08cf.image.jpg|http://ngm.nationalgeographic.com/u/TvyamNb-BivtNwcoxtkc5xGBuGkIMh_nj4UJHQKuorjj2yrzzTsBkwgD8q53LHYCkF7IMbQbjmJgWA/|http://www.maxingout.com/images/100%20update/big-horn-goat.jpg|http://cache2.allpostersimages.com/p/LRG/28/2806/OUCOD00Z/posters/edwards-walter-meayers-close-view-of-a-goat-with-long-horns.jpg