Understanding the SAVE_TEXT Function to Effectively Load Long Texts

Jimbo's picture

Little documentation exists on the internet explaining how to populate Long Texts in Master Data objects and the little that does is very specific and covers only standard SAP values like Material Basic Data Texts and Sales Texts. This whitepaper explains how to find the values used to populate the parameters of the SAVE_TEXT function in order to populate any Long Text value.

For this example, a value is provided by a client to populate a custom text type in Customer Masters. In this case it was the "Internal Customer Notes Pop-up" which, through text determination, causes a popup window with instructions to appear when creating a Sales Order associated with the Customer Master.

In order to determine how the Long Text is stored in SAP, a sample is created in the system. This Customer Master was picked at random in a sandbox and the desired Long Text is populated with some sample text.

Once saved, the value exists in a form that cannot be read through a query or in SE16, but it does exist in tables. The values that will be passed to the SAVE_TEXT function as parameters can be found in the STXH table with a little intuitive searching.

One option is to use the number of the object to find the newly-created Long Text and takes a little intuiting to find the correct record. Another option is to populate the TDFUSER and TDFDATE with the values used when the Long Text was created; if only one Long Text was created on this day by this user then only one record will appear and the parameters for the SAVE_TEXT function will be obvious.

This single record contains the bulk of the parameters needed coax the SAVE_TEXT function into producing Long Texts. Notice that the fields of the STXH table conveniently match the structure of the HEADER parameter viewed in SE37.

The TDOBJECT in this case is the Customer Master number concatenated with the Sales Org, Division and Distribution channel. This is important later as the value will have to be passed into the function exactly the same way.

The final step is to write some simple code to call the SAVE_TEXT function using these parameter values. As this example uses LSMW as a wrapper for a report, start by declaring variables in GLOBAL_DATA to hold the parameters that will be passed to the function.

data: lHEADER like THEAD,
      wa_TLINE like TLINE,
      it_TLINE type standard table of TLINE.

Next, add code to handle the incoming source data in BEGIN_OF_TRANSACTION. The lines passed in for long text have a 132-character limit, so the 250 character source field will be broken into two lines if it is longer than 132 characters.

clear: lHEADER, wa_TLINE.
refresh: it_TLINE.
"Populate lHEADER with the appropriate values.
lHEADER-TDOBJECT = 'KNVV'.
concatenate XD02S-KUNNR XD02S-VKORG XD02S-VTWEG XD02S-SPART
 into lHEADER-TDNAME.
lHEADER-TDID = 'ZAR'.
lHEADER-TDSPRAS = sy-langu. "Default to user's language.
"Populate the 132-character lines from the 250-char source
wa_TLINE-TDLINE = XD02S-TEXT+0(132).
append wa_TLINE to it_TLINE.
shift XD02S-TEXT by 132 places left.
if XD02S-TEXT ne ''. "Need another line for the remainder.
  wa_TLINE-TDLINE = XD02S-TEXT+0(132).
  append wa_TLINE to it_TLINE.
endif.
  CALL FUNCTION 'SAVE_TEXT'
    EXPORTING
      CLIENT                = SY-MANDT
      HEADER                = lHEADER
*       INSERT                = ' '
      SAVEMODE_DIRECT       = 'X'  "Commit the value now.
*       OWNER_SPECIFIED       = ' '
*       LOCAL_CAT             = ' '
*     IMPORTING
*       FUNCTION              =
*       NEWHEADER             =
    TABLES
      LINES                 = IT_TLINE
     EXCEPTIONS
       ID                    = 1
       LANGUAGE              = 2
       NAME                  = 3
       OBJECT                = 4
       OTHERS                = 5.
skip_transaction.



Consider validating.

Test, smartie!!!For brevity, no validation against the KNVV table is performed to verify that the Customer Master has been extended to the combination of Sales Org, Division and Distribution Channel specified in the source file. Additionally, no checks are performed to ensure that the leading zeros for Customer Master numbers are included with the source file, but it is highly recommended that these validations are in place before using this program.

No error handling is performed here, but it is highly recommended as errors do occur. If a language other than the user's is required then it should be included in the source data and passed in as a 1-character value in the HEADER parameter as TDSPRAS.

Work smarter, not harder.

There is a Direct Input method built into LSMW for long texts and the structures are identical to the structures for SAVE_TEXT. It is highly recommended to use that method in LSMW instead of using LSMW as a wrapper for the SAVE_TEXT function; this white paper is meant to explain how to find the values used to effectively load long texts.

Dilbert testing