1 1 1 1 1 Rating 0% (0 Votes)
 
In the first part I show how to create a structure on a base of class characteristics, now I will describe how to get the characteristics values for a object and how to fill the structure we've created in part 1. To get the characteristics values for an object I will use FM BAPI_OBJCL_GETDETAIL, then using field-symbols I will move the data from its result tables to our structure. I will use also cl_abap_typedescr to get the type of the field (needed for proper conversion) and FM KKEK_CONVERT_FLOAT_TO_CURR to convert currency values stored as float on database into currency fields.
 
Importing:

i_object type bapi1003_key-object -> key of object to be classified
i_class type klah-class -> class number
i_classtype type klah-klart -> class type
i_objecttable type bapi1003_key-objecttable optional -> name of database table for object
i_key_date type sy-datum  default sy-datum -> key date for classification
i_structure type ref to cl_abap_structdescr optional -> structure

Exporting:

et_objvaluesnum  type tt_bapi1003_alloc_values_num -> table with num characteristics
et_objvalueschar type tt_bapi1003_alloc_values_char -> table with char characteristics
et_objvaluescurr type tt_bapi1003_alloc_values_curr -> table with curr characteristics
e_data type ref to data -> filled structure for object with all characteristics

Exceptions:

fetch_values_error -> error while trying to receive object characteristics value
structure_error -> structure has wrong fields

Implementation:

method get_object_details.
*This is the code from http://abapblog.com.
  field-symbols<mt> type standard table,
                 <ms> type any,
                 <any> type any,
                 <p> type any,
                 <line> type any,
                 <bapiret> type bapiret2,
                 <table> type standard table,
                 <num>  type bapi1003_alloc_values_num,
                 <char> type bapi1003_alloc_values_char,
                 <curr> type bapi1003_alloc_values_curr.

  datams_data type ref to data.
  datam_objecttable type bapi1003_key-objecttable.
  datamt_return type standard table of bapiret2.
  datamo_str type ref to cl_abap_structdescr.
  datamo_type type ref to cl_abap_elemdescr.
  datam_p(16type p.
  datam_date type sy-datum.
  datam_time type sy-uzeit.

  "check if object table was supplied
  if i_objecttable is initial.
    select single obtab into m_objecttable from tcla where klart eq i_classtype.
  else.
    m_objecttable i_objecttable.
  endif.

"get object characteristics values
  call function 'BAPI_OBJCL_GETDETAIL'
    exporting
      objectkey              i_object
      objecttable            
m_objecttable
      classnum               
i_class
      classtype              
i_classtype
      keydate                
i_key_date
*     UNVALUATED_CHARS       = ' '
*     LANGUAGE               = SY-LANGU
*   IMPORTING
*     STATUS                 = STATUS
*     STANDARDCLASS          = STANDARDCLASS
    tables
      allocvaluesnum         et_objvaluesnum
      allocvalueschar        
et_objvalueschar
      allocvaluescurr        
et_objvaluescurr
      
return                 mt_return
            
.
  "check for errors
  loop at mt_return assigning <bapiret> where type ca 'EA'.
    raise fetch_values_error.
  endloop.


  if e_data is requested.
    "if we request data then check if the data structure object was passed
    if i_structure is not bound.
      "if not then we call method to create one for us
      zcl_abapblog_com_classific=>create_structure_for_class(
        exporting
          i_class     i_class
          i_classtype 
i_classtype
          i_key_date  
i_key_date
        
importing
          e_structure mo_str
*        e_data      = e_data
*        e_table     = e_table
             ).
    else.
      "if yes then we'll use it
      mo_str i_structure.
    endif.

    "create data container
    create data ms_data type handle mo_str.
    assign ms_data->to <ms>.
    if sy-subrc eq 0.
      "fill key fields
      assign component 'OBJECT_NUMBER' of structure <ms> to <any>.
      if sy-subrc eq 0.
        <any> i_object.
      else.
        raise structure_error.
        exit.
      endif.
      assign component 'CLASS' of structure <ms> to <any>.
      if sy-subrc eq 0.
        <any> i_class.
      endif.
      assign component 'CLASSTYPE' of structure <ms> to <any>.
      if sy-subrc eq 0.
        <any> i_classtype.
      endif.
      assign component 'OBJECT_TABLE' of structure <ms> to <any>.
      if sy-subrc eq 0.
        <any> m_objecttable.
      endif.

      assign m_p to <p>.
      "now loop through NUM characteristics
      loop at et_objvaluesnum assigning <num>.
        assign component <num>-charact of structure <ms> to <table>.
        if sy-subrc eq 0.
          insert initial line into table <table> assigning <line>.
          if sy-subrc eq 0.
            assign component 'LOW' of structure <line> to <any>.
            if sy-subrc eq 0.
              "check type
              free mo_type.
              mo_type ?= cl_abap_typedescr=>describe_by_datap_data <any> ).
              case mo_type->type_kind.
                when 'D' or 'T'.
                  <p> <num>-value_from.
                  unpack <p> to <any>.
                when others.
                  <any> <num>-value_from.
              endcase.
            endif.
            assign component 'HIGH' of structure <line> to <any>.
            if sy-subrc eq 0.
              "check type
              free mo_type.
              mo_type ?= cl_abap_typedescr=>describe_by_datap_data <any> ).
              case mo_type->type_kind.
                when 'D' or 'T'.
                  <p> <num>-value_to.
                  unpack <p> to <any>.
                when others.
                  <any> <num>-value_to.
              endcase.
            endif.
            assign component 'OPTLOW' of structure <line> to <any>.
            if sy-subrc eq 0.
              "convert relations
              case <num>-value_relation.
                when or or space.
                  <any> 'EQ'.
                when or or 9.
                  <any> 'GE'.
                when or or 8.
                  <any> 'GT'.
                when 6.
                  <any> 'LT'.
                when 7.
                  <any> 'LE'.
                when others.
                  <any> 'EQ'.
              endcase.
            endif.
            assign component 'OPTHIGH' of structure <line> to <any>.
            if sy-subrc eq 0.
              "convert relations
              case <num>-value_relation.
                when or .
                  <any> 'LT'.
                when or 5.
                  <any> 'LE'.
              endcase.
            endif.
            assign component 'UNIT_FROM' of structure <line> to <any>.
            if sy-subrc eq 0.
              <any> <num>-unit_from.
            endif.
            assign component 'UNIT_TO' of structure <line> to <any>.
            if sy-subrc eq 0.
              <any> <num>-unit_to.
            endif.
          endif.
        endif.
      endloop.

      "now loop throug CHAR characteristics
      loop at et_objvalueschar assigning <char>.
        assign component <char>-charact of structure <ms> to <table>.
        if sy-subrc eq 0.
          insert initial line into table <table> assigning <line>.
          if sy-subrc eq 0.
            assign component 'LOW' of structure <line> to <any>.
            if sy-subrc eq 0.
              <any> <char>-value_neutral.
            endif.
            assign component 'OPTLOW' of structure <line> to <any>.
            if sy-subrc eq 0.
              <any> 'EQ'.
            endif.
          endif.
        endif.
      endloop.

      "now loop throug CURRENCY characteristics
      loop at et_objvaluescurr assigning <curr>.
        assign component <curr>-charact of structure <ms> to <table>.
        if sy-subrc eq 0.
          insert initial line into table <table> assigning <line>.
          if sy-subrc eq 0.
            assign component 'LOW' of structure <line> to <any>.
            if sy-subrc eq 0.
              "we have to convert value from FLOAT
              call function 'KKEK_CONVERT_FLOAT_TO_CURR'
                exporting
                  float_imp      <curr>-value_from
                  curr_imp       
<curr>-currency_from
                
importing
                  curr_field_exp <any>
                
exceptions
                  overflow       1
                  others         2.
              if sy-subrc <> 0.

              endif.
            endif.
            assign component 'HIGH' of structure <line> to <any>.
            if sy-subrc eq 0.
              "we have to convert value from FLOAT
              call function 'KKEK_CONVERT_FLOAT_TO_CURR'
                exporting
                  float_imp      <curr>-value_to
                  curr_imp       
<curr>-currency_to
                
importing
                  curr_field_exp <any>
                
exceptions
                  overflow       1
                  others         2.
              if sy-subrc <> 0.
              endif.
            endif.
            assign component 'OPTLOW' of structure <line> to <any>.
            if sy-subrc eq 0.
              "convert relations
              case <curr>-value_relation.
                when or or space.
                  <any> 'EQ'.
                when or or 9.
                  <any> 'GE'.
                when or or 8.
                  <any> 'GT'.
                when 6.
                  <any> 'LT'.
                when 7.
                  <any> 'LE'.
                when others.
                  <any> 'EQ'.
              endcase.
            endif.
            assign component 'OPTHIGH' of structure <line> to <any>.
            if sy-subrc eq 0.
              "convert relations
              case <curr>-value_relation.
                when or .
                  <any> 'LT'.
                when or 5.
                  <any> 'LE'.
              endcase.
            endif.
            assign component 'CURRENCY_FROM' of structure <line> to <any>.
            if sy-subrc eq 0.
              <any> <curr>-currency_from.
            endif.
            assign component 'CURRENCY_TO' of structure <line> to <any>.
            if sy-subrc eq 0.
              <any> <curr>-currency_to.
            endif.
          endif.
        endif.
      endloop.
    endif.
    "export data
    e_data ms_data.
  endif.

endmethod.