Friday, 12 October 2007

ABAP Obtaining an object's characteristics from the classification system

The easiest way to get a list of all the characteristics of an object is to use the BAPI_OBJCL_GETCLASSES BAPI. The function requires that you supply an object key, the database table where the object is stored, the class type of the obect to look for -- e.g. '001' for material or '023' for batch -- and finally whether or not to return the entire list of characteristics.

The return value consists of a table containing all the classes the object is currently assigned and if requested three additional tables with values of the object characteristics, one for string valued, one for numeric and one for currency.

Information about objects, db tables and corresponding class types is stored in the inob table. So one way of getting all this would be to write code like :

  DATA : 
    wa_inob TYPE inob, 
    my_object LIKE inob-objek.

  CLEAR wa_inob.
  CONCATENATE my_matnr my_batch INTO my_object.
    FROM inob 
    INTO wa_inob
    WHERE objek = my_object.

So calling the BAPI would be as straight forward as ....

  DATA :
    class_list TYPE STANDARD TABLE of bapi1003_alloc_list.
    valueschar TYPE STANDARD TABLE OF bapi1003_alloc_values_char, 
    valuescurr TYPE STANDARD TABLE OF bapi1003_alloc_values_curr, 
    valuesnum  TYPE STANDARD TABLE OF bapi1003_alloc_values_num,
    return     TYPE STANDARD TABLE OF bapiret2.

      objectkey_imp         = wa_inob-objek
      objecttable_imp       = wa_inob-obtab
      classtype_imp         = wa_inob-klart
      read_valuations       = 'X'
      keydate               = sy-datum
*     language              = 'E'
      alloclist             = class_list
      allocvalueschar       = valueschar
      allocvaluescurr       = valuescurr
      allocvaluesnum        = valuesnum
      return                = return.

Determining if an object has indeed been assigned classification information is performed by checking the return table value. Correct classification means that the return table contains one line with field "TYPE" set to 'S', "ID" set to 'CL' and NUMBER set to 741. (When no allocations are found number is 740). So before trying to read any characteristic values is safer to test like this.

        <ret> TYPE bapiret2.

     READ TABLE return INDEX 1 ASSIGNING <ret>.

*   Check that object allocations exist
    IF  sy-subrc = 0 AND
        <ret>-id = 'CL' AND
        <ret>-number = 741.


Now getting a specific value from let's say the string valued characteristics can be accomplished by code similar to following, which gets the value of a characteristic named QUALITY:

    <fs> TYPE bapi1003_alloc_values_char.

  READ TABLE valueschar WITH KEY charact = 'QUALITY' ASSIGNING <fs>.

  if <fs> IS ASSIGNED.
*   The language independent value of the characteristic is 
    my_quality_id = <fs>-value_neutral.
*   The language dependent i.e. the one that will appear in the
*   result table of CL30N is 
    my_quality_descr = <fs>-value_char.

NOTE: The following approach is generic and is supposed to work fine on all systems. In our system however (Release 4.6C) the inob table does not have an index on inob-objek, so the SELECT SINGLE statement is executed by performing sequential reads. One way to overcome this would be to manually create the index. The other, as far as I am concerned, is to know beforehand the type of objects and classes that your program deals with, and therefore hard code the database table to 'MARA' or MCH1' and the class type to '001' and '023' for materials and batches respectively.

No comments :