Catch Carriage Returns and Line Feeds in Source Data

Jimbo's picture

One of the great features of Excel is the ability to add carriage returns to the contents of a cell in order to represent multiple lines of content in the cell. This can wreak havoc in source data as carriage returns and line feeds can be read in as ASCII or Unicode characters and then corrupt data in LSMW's proprietary table storage formats.

The "BDC_INSERT, Transaction code .. is invalid" message (number 00338) does not give much advice on how to resolve the problem except to "Contact the person in charge." The solution might be even simpler than that.

If a carriage return or line feed character slips into the data without being together then LSMW will read those characters in as data instead of a marker for the end of the line of text. Checking to see if this has happened is just a matter of looking for the characters in the source data.

In this example, the address had a line feed character in the address so that there was a street address and a note that the company was a DBA or "Doing Business As". The source data represented as two lines in Excel, one line in the LSMW .read file with a line feed character in the middle and then as a truncated line and a new line in the .conv file.

Notice how the "structure" of the new lines is second line of the street address. Also, notice how the "BP" (in the blue ellipse) is missing as the transaction code which is what caused LSMW to throw the error above.

A short snippet of code is enough to catch this and prevent it from causing catastrophic results during the batch creation. It checks for both carriage return and line feed along with CRLF for good measure.

*** GLOBAL_DATA ***
DATA:
 V_CRLF TYPE ABAP_CR_LF,
 V_CR TYPE ABAP_CHAR1,
 V_LF TYPE ABAP_CHAR1.
*** BEGIN_OF_PROCESSING ***
V_CRLF = CL_ABAP_CHAR_UTILITIES=>CR_LF.
V_CR = V_CRLF+0(1).
V_LF = v_CRLF+1(1). "CL_ABAP_CHAR_UTILITIES=>NEWLINE.
if BPVENDOR1-STREET cs V_CRLF
or BPVENDOR1-STREET cs V_CR
or BPVENDOR1-STREET cs V_LF.
  write: / BPVENDOR1-NAME_ORG1, BPVENDOR1-STREET color col_negative,
   'Street contains a carriage return character.'.
  skip_transaction.
endif.

The conversion step will now check each street address for these special characters. The results should look like this.

The solution now is to simply remove the line feeds from the source data. Putting the second line of the address into the STREET2 field is a great way to ensure that it is loaded along with the vendor.

Programming Language: 
ABAP