Identify Malformed Postal Codes in LSMW
Staring at a spreadsheet of ten thousand customers looking for invalid data is like looking for defective hay in a haystack. SAP can cut the time required for validation to a small fraction of what it would normally take by identifying problem records in the source data. The resource that is responsible for correcting invalid data (potentially the same person performing the ETL) will be grateful for the help.
SAP has strict, country specific validation on the postal code that many legacy systems cannot perform. In many legacy system the postal code is a free-text field that is often formatted incorrectly. Some examples of poorly formatted postal codes include (but are not limited to) country codes added to the front or end, too many or too few digits, missing or misplaced dashes and postal codes that don't match the ISO country code. Even if the postal code comes directly from a vendor invoice, SAP will reject it if it is malformed. If mail has been delivered to a customer for years with the postal code on file in the legacy, SAP will reject it if it is malformed.
This snippet of code relies on the same function used internally by SAP to validate postal codes. The results provided by LSMW using this code will be the same as the results from any SAP transaction that validates postal codes. It is specific to Vendor Master, but it can be modified easily for any other object. This same functionality is available in SAP Business Objects Data Services, but LSMW is provided for free with SAP.
CALL FUNCTION 'POSTAL_CODE_CHECK' EXPORTING country = BLFA1-LAND1 postal_code = BLFA1-PSTLZ EXCEPTIONS not_valid = 1 OTHERS = 2. if SY-SUBRC ne 0. write: / BLFA1-LIFNR, 'Postal code', BLFA1-PSTLZ, 'not valid for country', BLFA1-LAND1. endif.