Check date validity in LSMW

Jimbo's picture

All to often dates in source data are in a format that is incompatible with a client's user settings or has been corrupted when handled in Excel. An easy way to ensure that the source date formats can be loaded into SAP is by ratifying data standards at the beginning of the project. Still, poorly formatted data will slip through the hands of the team transforming the data and the best way to catch formatting problems is by finding a validating function and using it to validate the source data prior to attempting to load.

A great function for validating dates in SAP is DATE_CHECK_PLAUSIBILITY. This function sets sy-subrc to zero when a tested date is in a valid format for SAP. The snippet of code below is part of a Material Master creation LSMW object. It can be modified easily to be used in any LSMW object.

Start be adding this variable definition to the Global Definitions section of the LSMW object. This is a date variable and is required for the function to work correctly; the function only accepts date variables, not text.

data: dTestDate like sy-datum.

Add this snippet to the field that needs to be validated. This can be done by double-clicking on the field and pasting the code into the ABAP editor. Because the function handles the date as a date field, it needs to be in the format YYYYMMDD. This may require some additional code if the source dates are not in SAP's internal date format.

move IMMH1-MSTDE to dTestDate.
CALL FUNCTION 'DATE_CHECK_PLAUSIBILITY'
     EXPORTING
          date                      = IMMH1-MSTDE
     EXCEPTIONS
          plausibility_check_failed = 1
          OTHERS                    = 2.
IF sy-subrc ne 0.
  write: 'skipping poorly formed date:', IMMH1-MSTDE color 6.
  skip_transaction.
else.
  BMMH1-MSTDE = IMMH1-MSTDE.
endif.
Programming Language: 
ABAP