Validate Characteristics in a Class
Classification data is often provided in a hierarchical structure with characteristic names and values linked to a combination of material number and classification. This hierarchical format is the same for anything that needs to be classified including materials and documents.
It is never convenient to discover during the loading phase that a characteristic doesn't exist in the system or isn't assigned to the classification. This causes the CL20 transaction to fail and the only way to know that it failed is to comb through the error log.
Understanding how SAP links Classifications to Characteristics is the best place to start. Classifications and Characteristics have internal numbers assigned that are hidden to the user and are independent of the names.
In the KLAH for table Classifications the internal number is stored in the CLINT field. In the CABN table for Characteristics the internal number is stored in the ATINN field. Links between Classifications and the Materials to which they are assigned is even more complex. The link between the Classifications and Characteristics is stored in the KSML table and can be visualized like this:
- Characteristic assignment
- KLAH-CLINT → KSML-CLINT
- KSML-IMERK → CABN-ATINN
A better is shown below. The first image is the Classification with Characteristics shown in the CL02 transaction. The second is three SE16 screen with the relevant links.
Start by defining a few work area structures in the Global Data Definitions area. This is where the values will be stored when looping through the SAP tables.
data: lCABN like CABN, lKLAH like KLAH, lKSML like KSML.
In the field where assigning the class, add this snippet of ABAP to validate the Classification. This populates the lKLAH
variable with the record from the KLAH table related to this Classification for later use.
translate CL20H-CLASS to UPPER CASE. select single * from KLAH into lKLAH where CLASS eq CL20H-CLASS and KLART eq CL20H-KLART. if sy-subrc ne 0. write: / CL20H-MATNR, CL20H-KLART, CL20H-CLASS, 'Missing Class.'. skip_transaction. else. BIKSSK-CLASS = lKLAH-CLASS. endif.
Farther down there is a field for populating the name of the Characteristic to be populated with data. Add this snippet of code. It ensures that the Characterisitic actually exists in the system and is assigned to the Classification to which the object is being assigned.
condense CL20I-ATNAM no-gaps. translate CL20I-ATNAM to UPPER CASE. shift CL20I-ATNAM left deleting leading space. select single * from CABN into lCABN where ATNAM eq CL20I-ATNAM. if sy-subrc ne 0. write: / CL20H-MATNR, CL20H-KLART, CL20H-CLASS, CL20I-ATNAM, 'Characteristic does not exist.'. skip_transaction. else. select single * from KSML into lKSML where CLINT eq lKLAH-CLINT and KLART eq lKLAH-KLART and IMERK eq lCABN-ATINN. if sy-subrc ne 0. write: / CL20H-MATNR, CL20H-KLART, CL20H-CLASS, CL20I-ATNAM, 'Characteristic not assigned to Classification.'. skip_transaction. else. BIAUSP-ATNAM = lCABN-ATNAM. endif. endif.
A complete LSMW object for assigning Classifications to Materials is available here. This tool performs additional validations that are not shown above including the validation of characteristic values.