When importing identifiers, reject values that cannot fit the target field before they are silently shortened. A normal assignment from a string to a shorter character field truncates the content, potentially turning an invalid identifier into a different one.
The EXACT constructor operator, available from ABAP 7.40, checks whether the conversion can preserve the value. This self-contained report attempts to place a five-character identifier into a four-character field and catches the conversion exception. The # symbol derives the required type from the assignment target.
REPORT zcheck_identifier_length.
TYPES ty_identifier TYPE c LENGTH 4.
DATA lv_input TYPE string VALUE `A1234`.
DATA lv_identifier TYPE ty_identifier.
START-OF-SELECTION.
TRY.
lv_identifier = EXACT #( lv_input ).
WRITE: / 'Accepted:', lv_identifier.
CATCH cx_sy_conversion_error.
WRITE / 'Rejected: identifier would lose characters'.
ENDTRY.
Expected output: Rejected: identifier would lose characters. Changing the input to A123 permits the assignment and prints Accepted: A123. EXACT checks conversion without data loss; it does not establish whether the identifier exists or satisfies your business rules.
SAP Learning: Avoiding the Pitfalls of Type Conversions ยท SAP Help: ABAP 7.40 Language Enhancements
